copying files

NightShade01

Member
Joined
Jul 7, 2006
Messages
5
Programming Experience
1-3
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

VB.NET:
[LEFT]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 integerfor i = 0 to myFiles.GetUpperBound(i)
File.Copy(myFiles(i), backupDest & "\" & myFile(i)) //problem occurs here
next[/LEFT]

problem 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?
 
nvm stupidity has overcome my mind.....i used the LastIndexOf() combine with the substring to pull the file's path apart and re attach it as a string.
 
Make use of the Path class when manipulating paths:
VB.NET:
Dim destFolder As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "My Backup")

If Not IO.Directory.Exists(destFolder) Then
    IO.Directory.CreateDirectory(destFolder)
End If

Dim destPath As String

For Each filePath As String In IO.Directory.GetFiles("C:\tmp", "*.doc")
    destPath = IO.Path.Combine(destFolder, _
                               IO.Path.GetFileName(filePath))

    IO.File.Copy(filePath, destPath, True)
Next filePath
 
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:
VB.NET:
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 try
where 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.
 
Things don't just stop working. What happened? Did it give you an error message? If so it was intended as a diagnostic tool. If you're asking us to diagnode the problem then we should have the error message.
 
there is no error message to it. when the search was defined for C:\tmp it searched through the tmp folder for all the files (.doc .ppt. etc....) that i made up so it would have something to return. However when i changed the definition to search the whole C: drive it wasn't returning any files at all like it should have (the .doc .ppt etc...) it just created a blank folder and ended the search. I'm trying to understand why it would return all the files when defined to a specific folder but not when it searchs through C:. I was thinking that possibly it might have something to do with the fact that C is the drive as opposed to a 'folder'. would that change the way the code runs?
 
You've got your code in a Try...Catch block but you have an empty Catch block. I'll will wager any amount that it is throwing an exception but you're just ignoring it and carrying on. Put some code in your Catch block to tell you that an exception has been thrown, e.g.
VB.NET:
MessageBox.Show(ex.ToString())
 
Back
Top