Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


noob question about simple manipulation of files

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3131
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> noob question about simple manipulation of files
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 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
 
 
Post #: 1
 
 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


(in reply to P00PDOG)
 
 
Post #: 2
 
 Re: noob question about simple manipulation of files - 5/26/2005 8:59:00 AM   
  P00PDOG

 

Posts: 3
Score: 0
Joined: 5/26/2005
From:
Status: offline
quote:
Originally posted by kirrilian

have you tried this:
quote:

' Otherwise you might need:
' Fso.CopyFile f.Path, DestPath & init
' Fso.DeleteFile f.Path





No, the actual script works perfectly but when all files have been moved, I get a file not found error message. I'm just trying to get rid of the error message...
Thanks

(in reply to P00PDOG)
 
 
Post #: 3
 
 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

(in reply to P00PDOG)
 
 
Post #: 4
 
 Re: noob question about simple manipulation of files - 5/26/2005 1:23:48 PM   
  P00PDOG

 

Posts: 3
Score: 0
Joined: 5/26/2005
From:
Status: offline
JBird, Thank you I totally understood your explanation. The var didn't change to the next file before I was doing another move.
I have to say, This was my first VB script. I didn't write it but I did do a little tweaking to it so that it would work the way I wanted it to. I'm looking for a good vb scripting book to sink my teeth into anyone know of a really clear beginner/intermediate book that they would recommend? Thanks again to you and the forum.
P00PDOG

(in reply to P00PDOG)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> noob question about simple manipulation of files Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts