Login | |
|
 |
noob question about simple manipulation of files - 5/26/2005 6:16:12 AM
|
|
 |
|
| |
P00PDOG
Posts: 3
Score: 0
Joined: 5/26/2005
From:
Status: offline
|
I found this script on the net and am tweaking it a bit, my question is that the script works perfectly (copys files in one direrctory to another directory based on the first charactor in the name copies to a directory example if the file has an "a" as the first charactor it will copy it to a directory called "a".) the problem is that when the directory is finally empty it get the error file doesnt exist. I'm not sure how to say when source dir is empty exit. here is the script below...Thanks for any help private const SourcePath = "C:\mypath\1\" ' put your source directory here private const DestPath = "C:\my path\2\" ' put your destination directory here - Must already exist private sub HandleFile (byval f) Dim init ' Get the fist letter of the name (Doesn't matter what it is as long as it's printable) init = left(f.name, 1) 'check whether the folder exists if not fso.FolderExists(DestPath & init) then 'fso.createfolder(DestPath & "1miscfiles") fso.MoveFile f.Path, DestPath & "1miscfiles" & "\"'this is for anything that is not A-Z such as a ~ ect... end if 'If SourcePath and Destpath are on the same volume, or if the OS supports moving across volumes fso.MoveFile f.Path, DestPath & init & "\" & f.Name'<---here is my problem, this is where it fails ' Otherwise you might need: ' Fso.CopyFile f.Path, DestPath & init ' Fso.DeleteFile f.Path end sub Private Sub handlefolder(ByVal folder) ' Only need this to recurse subdirectories in the source path ' For Each f In folder.SubFolders ' may not be necessary if there are no subfoldrs to recurse ' handlefolder f ' '' '' ' Next ' '' '' for each f in folder.files handlefile f next End Sub ' Main Routine starts here Dim fso, fo, fw, fr, glub Set fso = CreateObject("Scripting.filesystemobject") ' Get handle to input folder Set fo = fso.GetFolder(SourcePath) handlefolder fo Set fso = Nothing Set fo = Nothing
|
|
| |
|
|
|
 |
Re: noob question about simple manipulation of files - 5/26/2005 8:48:58 AM
|
|
 |
|
| |
kirrilian
Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
|
have you tried this: quote: ' Otherwise you might need: ' Fso.CopyFile f.Path, DestPath & init ' Fso.DeleteFile f.Path
|
|
| |
|
|
|
 |
Re: noob question about simple manipulation of files - 5/26/2005 10:01:28 AM
|
|
 |
|
| |
jbird
Posts: 33
Score: 0
Joined: 6/25/2004
From: USA
Status: offline
|
PoopDog, Here is your problem. When this script encounters a file where the first letter does not match a directory you already have created it puts the file in the 1miscfiles directory. The problem is the way your if statement is setup to test for this condition. Your if statement checks to see if the folder exists that matches the first letter but once the if statement executes and the file is moved to the 1miscfiles folder then it trys to move the same file again because the next statement after the if is the move statement. You haven't cycled through the loop again to change the f. Try the following code. private const SourcePath = "C:\mypath\1\" ' put your source directory here private const DestPath = "C:\my path\2\" ' put your destination directory here - Must already exist private sub HandleFile (byval f) Dim init ' Get the fist letter of the name (Doesn't matter what it is as long as it's printable) init = left(f.name, 1) 'check whether the folder exists if not fso.FolderExists(DestPath & init) then 'fso.createfolder(DestPath & "1miscfiles") fso.MoveFile f.Path, DestPath & "1miscfiles" & "\"'this is for anything that is not A-Z such as a ~ ect... '**************************************************** 'Here is the change I made. I put the second Move command in the else clause. '**************************************************** else 'If SourcePath and Destpath are on the same volume, or if the OS supports moving across volumes fso.MoveFile f.Path, DestPath & init & "\" & f.Name '****************************************************** 'I moved the End If to the end of the function. '******************************************************* End If ' Otherwise you might need: ' Fso.CopyFile f.Path, DestPath & init ' Fso.DeleteFile f.Path end sub Private Sub handlefolder(ByVal folder) ' Only need this to recurse subdirectories in the source path ' For Each f In folder.SubFolders ' may not be necessary if there are no subfoldrs to recurse ' handlefolder f ' '' '' ' Next ' '' '' for each f in folder.files handlefile f next End Sub ' Main Routine starts here Dim fso, fo, fw, fr, glub Set fso = CreateObject("Scripting.filesystemobject") ' Get handle to input folder Set fo = fso.GetFolder(SourcePath) handlefolder fo Set fso = Nothing Set fo = Nothing
|
|
| |
|
|
|
|
|