![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 52
Rep Power: 4
![]() |
copying files
Ok so i'm trying to copy a file from one folder to another and i'm running into a problem. currently i know that in order to copy something you use the File.Copy( , ) method (assuming you import system.io.*) problem being the dest file can't be an exsisting file or directory. fine. I used a a loop to pull the file up and then tried the copy method. now my problem being that i'm getting a full path extension and not just a file name. Any one know how to cut the file name path apart without knowing how long the file is or it's name?
ie dim user as string
user = system.environment.username.tostring
directory.createDirectory("C:\Documents and Settings\" & user & "\Desktop\My Backup")
dim backupDest as string = "C:\Documents and Settings\" & user & "\Desktop\My Backup"
dim myFiles as String() = Directory.GetFiles("C:\tmp", "*.doc")
dim i as integer
for i = 0 to myFiles.GetUpperBound(i)
File.Copy(myFiles(i), backupDest & "\" & myFile(i)) //problem occurs here
nextproblem i'm having is that myFile(i) where the comment is, i actually the value "C:\tmp\testing.doc" and the two paths are colliding. I need to get the myFile(i) to be just testing.doc The only thing i can think of is indexOfLast? but doesn't that return an integer? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Location: PA, USA
Posts: 254
Rep Power: 4
![]() |
there are several ways to do this, here is one.
vbnet Syntax (Toggle Plain Text)
__________________
I have never let my schooling interfere with my education. -Mark Twain- Xbox live gamertag: melbolt |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
This is pretty much identical to yours with only a slight difference; but try it, it should work. I dont have .net right here so I cant test it for you.
Dim user as String = _
System.Enviroment.Username.ToString
Dim backupdest as string = _
"C:\Documents and Settings\" & user & "\Desktop\My Backup\")
Directory.CreateDirectory(backupdest)
Dim myFiles = Directory.GetFiles("C:\tmp", "*.doc")
For each file as string in myFiles
File.Copy(file, backupdest & file)
next
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2005
Posts: 52
Rep Power: 4
![]() |
Alright so i tried it and at first i got it to work within the same folder (searching c:\tmp for whatever i needed) then when i knew it work i changed it so that it wouls search all of C:\ as oppsed to just C:\tmp....now it doesn't work here is what i have:
dim user as string
user = System.environment.username.tostring
dim mydest as string = "C:\documents and settings\" & user & "\desktop\mybackup\"
try
dim myfiles as string() = Directory.getfiles("C:\", "*.doc", searchoption.alldirectories) //comment here
dim i as integer
for i = 0 to myfiles.getupperbound(i)
dim filesbeingcopied as string
dim r as intger = myfiles(i).lastindexof("\")
filesbeingcopied = myfiles(i).substring(r +1)
file.copy(myfiles(i), mydest & filesbeingcopied)
next
catch ex as exception
end trywhere i have the comment that's the only line i've changed where the program stops working as intended i went from C:\tmp to C:\ and it doesn't find anyfiles. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Dim user As String = System.Environment.Username.ToString()
Dim dest As String = "C:\Documents and Settings\" & user & "\Desktop\MyBackup\"
Try
Dim files As String() = Directory.GetFiles("C:\","*.doc", SearchOption.AllDirectories)
Dim enum As Collections.IEnumerator = files.GetEnumerator()
While enum.MoveNext
Dim fi As New FileInfo(enum.Current)
File.Copy(fi.Directory & "\" & fi.Name, dest & fi.Name))
End While
Catch ex As Exception
MsgBox(ex.Message)
End TryTry that. I have never used this method before and have just written it, unfortunately I am not at a PC with VS so this is untested.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
Not to bring up an old post, but I was digging around and searching for Copy method and directory searching method. I noticed on the previous codes there is a statement as such:
Dim files As String() = Directory.GetFiles("C:\","*.doc", SearchOption.AllDirectories![]()
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
As long as you can honestly assure me you are not blind then I guess it is safe to say the parameter is not there. But wether this has to do with your version being 2003 im not sure, my presumption is that it is the version of the framework you are using that matters. Although 2003 would come packaged with .NET1.1 an upgrade to .NET2.0 would be recognised by your IDE I beleive, though this is another presumption.
So maybe its just a newer version of the framework that you need...?
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
Quote:
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
Yep, seems our company is still running 1.0 framework. That stinks. This app need to run on more computers then my own. I guess I have to live w/o that option, damnit.
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unix commands compatible with Windows? | titaniumdecoy | Bash / Shell Scripting | 7 | Oct 5th, 2006 8:25 AM |
| copying files first before deleting them | harjit | Visual Basic | 1 | May 15th, 2005 8:05 AM |
| Checking source codes of image, audio and video files | on_auc | C++ | 3 | Feb 21st, 2005 9:36 PM |
| shutil.copy corrupting files? | TimL | Python | 0 | Feb 20th, 2005 4:32 PM |
| Bash script to access all files in a directory | shinni | Bash / Shell Scripting | 4 | Feb 18th, 2005 4:26 PM |