Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a program that moves files that is older than a choosen date. My problem is that i need to keep/make the same path the files had before they where moved. i.e

File to be moved is in "c:\backup\" and i choose that files that are found should be moved to "f:\gigastorage\" the program now need to create "f:\gigastorage\backup\" before moving the file.

I get the files moved but they don't keep their original paths. Anyone got a solution for this?
Posted
Comments
bbthemean 29-Sep-11 4:34am    
Forgot to say that i'm fairly new to vb so please explain to me as basic as possible.

1 solution

You have to specify the path when you call the File.Move method.
So, you want to strip off the "C:" part (find the first instance of '\' or '/' in the source path, and use that as the start of a Substring operation). Then append that to the destination path.
 
Share this answer
 
Comments
bbthemean 29-Sep-11 4:31am    
Thanks for reply.

This is what i got that doesn't work as wanted.
aFile.MoveTo(nameToDirectory.FullName & "\" & aFile.Name)
OriginalGriff 29-Sep-11 4:36am    
Show us slightly more code - so we can tell what "aFile" is, and so forth.
OriginalGriff 29-Sep-11 4:37am    
Oh, and do you get any error?
bbthemean 29-Sep-11 4:38am    
Public Shared Function MoveWithFilesInDir(ByVal aDir As DirectoryInfo, ByVal nameToDirectory As DirectoryInfo) As Long
Dim aFile As FileInfo
Dim n As Long = 0

For Each aFile In aDir.GetFiles()
If aFile.LastWriteTime < Form1.DateTimePicker1.Value Then
Try
aFile.MoveTo(nameToDirectory.FullName & "\" & aFile.Name)

n = n + 1
Write2Log(aFile.FullName & " _ " & aFile.LastWriteTime)

Catch ex As Exception

MsgBox(ex.Message)

End Try
End If
Next

MoveWithFilesInDir = n

End Function

no errors running the program
OriginalGriff 29-Sep-11 5:24am    
Curious - it worked fine for me:

MoveWithFilesInDir(New DirectoryInfo("D:\Temp\MoveMe"), New DirectoryInfo("D:\Temp\MovedTo"))

Or at least, it did once I created both folders. Until then I got a message box complaining it "couldn't find part of the file path"

What is happening for you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900