+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
Scripts Thread, VBS - Moving Files in Coding and Web Development; Morning Folks, Currently fondling with VBS trying to get a script together to move files based on file name. I ...
  1. #1
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30

    VBS - Moving Files

    Morning Folks,

    Currently fondling with VBS trying to get a script together to move files based on file name. I have the following code;

    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set pyramid = fso.GetFolder("\\server\share\invoices")
    For Each file In pyramid.Files
    if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage" & file.Name, "\\server\share\invoices\Processed" & file.name
    Next 
    
    For Each subdir in pyramid.SubFolders 
          Set pyramid = pyramid.GetFolder("\\server\share\invoices" & subdir.name)
                For Each file In pyramid.Files
                      if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage" & subdir.name & "\" & file.Name, "\\server\share\invoices\Processed" & file.name
                Next       
          Recurse(subdir) 
    Next
    As you can see I'm, trying to match files with file names beginning with 00, and move them to the Sage folder to be imported into Sage, and another to move them into a processed folder for storage, however, currently getting and error on Line 5, Char 35 stating "File not found"

    Anyone have any ideas, or alterations to the script? I'm not even sure Left(file.name, 2) is valid, only ever used it for extensions as if Right(file.name, 4) = ".ext" then fso.MoveFile ....

  2. #2
    TheLibrarian
    Guest
    Use a message box to check the filename and path you are generating in line 5.

    Personally I wouldn't have bothered with vbs, I'd have used command shell scripting, but then I would, I don't know enough vbs.

  3. #3
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by TheLibrarian View Post
    Use a message box to check the filename and path you are generating in line 5.

    Personally I wouldn't have bothered with vbs, I'd have used command shell scripting, but then I would, I don't know enough vbs.
    How would the similar code look in command shell? I guess, it's always easy to fall back on whats easiest for the user

  4. #4

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Are you looking in the wrong folder? The first line looks for files beginning with "00" in \\server\share\invoices but then the move line tries to do files called "SageXXXX"???

    Are the files actually called "sage00" <whatever> or are they called 00<whatever> but in a folder called \\server\share\invoices\sage\??

  5. #5
    fawkers's Avatar
    Join Date
    Jun 2007
    Location
    Southend
    Posts
    193
    Thank Post
    32
    Thanked 22 Times in 21 Posts
    Blog Entries
    2
    Rep Power
    31
    I'm no good with vbs but here is a copy of a powershell script i have which does the same sort of thing

    Code:
    #Where to put the files once moved
    $Target = "u:\test"
    $Path = read-host "Please Enter Source Path: "
    $files = Get-ChildItem $path
    
    foreach($f in $files){
        # in this case check for the letter a in the files name and a .txt ext.
        if($f.FullName.Contains("a") -and $f.FullName.Contains(".txt") ){
            Move-Item $F.PSPath $target
        }
    }

  6. Thanks to fawkers from:

    ahuxham (16th November 2009)

  7. #6
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by srochford View Post
    Are you looking in the wrong folder? The first line looks for files beginning with "00" in \\server\share\invoices but then the move line tries to do files called "SageXXXX"???

    Are the files actually called "sage00" <whatever> or are they called 00<whatever> but in a folder called \\server\share\invoices\sage\??
    Files are 00 sequenced, and should be moved into the Sage folder, for Sage to import them.

  8. #7
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    I believe this line is wrong

    Code:
    if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage" & file.Name, "\\server\share\invoices\Processed" & file.name
    Code:
    if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage\" & file.Name, "\\server\share\invoices\Processed" & file.name

    Basically the path is ending up
    Code:
    \\server\share\invoices\Sage00filename.txt
    instead of
    Code:
    \\server\share\invoices\Sage\00filename.txt
    Last edited by ChrisH; 16th November 2009 at 10:47 AM.

  9. #8
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by jinnantonnix View Post
    You've probably got a space in a file name somewhere.

    Try enclosing your path with a double-quote (") which is charater 34 in ASCII.

    e.g.

    if Left(file.name, 2) = "00" then fso.MoveFile chr(34) & "\\server\share\invoices\Sage" & file.Name & chr(34), chr(34) & "\\server\share\invoices\Processed" & file.name & chr(34)
    Well, an improvement on the error message. Bad Filename or Number now.

  10. #9
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    42
    If you're using:

    Code:
    if Left(file.name, 2) = "00" then fso.MoveFile chr(34) & "\\server\share\invoices\Sage" & file.Name & chr(34), chr(34) & "\\server\share\invoices\Processed" & file.name & chr(34)
    As Chris said you need:

    Code:
    if Left(file.name, 2) = "00" then fso.MoveFile chr(34) & "\\server\share\invoices\Sage\" & file.Name & chr(34), chr(34) & "\\server\share\invoices\Processed\" & file.name & chr(34)

  11. Thanks to apeo from:

    ahuxham (16th November 2009)

  12. #10
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Quote Originally Posted by ChrisH View Post
    I believe this line is wrong

    Code:
    if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage" & file.Name, "\\server\share\invoices\Processed" & file.name
    Code:
    if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage\" & file.Name, "\\server\share\invoices\Processed" & file.name

    Basically the path is ending up
    Code:
    \\server\share\invoices\Sage00filename.txt
    instead of

    Code:
    \\server\share\invoices\Sage\00filename.txt
    I've enclosed the end path with a \ showing, "\\server\share\invoices\sage\" and still the same. The double quotes have changed the error message from not found, to bad filename or number now.
    Last edited by ChrisH; 16th November 2009 at 10:51 AM.

  13. #11
    TheLibrarian
    Guest
    Move processed files:
    Code:
    for /f "delims=/" %%i in ('dir /b "\\server\share\directory\00*"') do echo move /y "\\server\share\directory\%i" "\\server\share\directory"
    Move files to be processed:
    Code:
    for /f "delims=/" %%i in ('dir /b /s "\\server\share\directory\00*"') do echo move /y "%%i" "\\server\share\directory"
    Remember to remove the echo statements if the command lines output look right.

  14. Thanks to TheLibrarian from:

    ahuxham (16th November 2009)

  15. #12
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    You need to see what things are being assigned what as mentioned above use an echo to find what the variables are getting assigned;


    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set pyramid = fso.GetFolder("\\server\share\invoices")
    For Each file In pyramid.Files
    
    
    
    Wscript .echo "\\server\share\invoices\Sage\" & File.Name"
    
    
    
    
    
    if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage\" & file.Name, "\\server\share\invoices\Processed" & file.name
    Next 
    
    For Each subdir in pyramid.SubFolders 
          Set pyramid = pyramid.GetFolder("\\server\share\invoices" & subdir.name)
                For Each file In pyramid.Files
                      if Left(file.name, 2) = "00" then fso.MoveFile "\\server\share\invoices\Sage" & subdir.name & "\" & file.Name, "\\server\share\invoices\Processed" & file.name
                Next       
          Recurse(subdir) 
    Next

  16. Thanks to ChrisH from:

    ahuxham (16th November 2009)

  17. #13
    ahuxham's Avatar
    Join Date
    Apr 2008
    Posts
    1,122
    Thank Post
    76
    Thanked 138 Times in 109 Posts
    Rep Power
    30
    Code as shown on my screen;

    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set pyramid = fso.GetFolder("\\Pghwms1\Pyramid\Report\Test\")
    For Each file In pyramid.Files
    if Left(file.name, 2) = "00" then fso.MoveFile chr(34) & "\\Pghwms1\Pyramid\Report\Sage\" & file.Name & chr(34), chr(34) & "\\Pghwms1\Pyramid\Report\Processed\" & file.name &chr(34)
    Next
    The scripts intent is to move all PDF's starting with 00 to the Sage folder for importing, and the Processed folder for backup. I've removed the subdir statement for now, as this will be added at a later stage.

  18. #14
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    Code:
    & chr(34), chr(34)
    Not sure that comma should be there.

  19. #15
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    Ah its that whole long line, you start to sport allsorts

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Encrypting VBS files
    By Newton in forum Scripts
    Replies: 2
    Last Post: 28th April 2009, 02:37 PM
  2. Kids Running VBS files
    By tomscaper in forum Scripts
    Replies: 8
    Last Post: 9th January 2008, 12:36 PM
  3. Covert CMD/BAT files to vbs help
    By tosca925 in forum Scripts
    Replies: 12
    Last Post: 8th November 2007, 02:58 PM
  4. Home drive not mapping when moving to VBS
    By mullet_man in forum Windows
    Replies: 6
    Last Post: 17th June 2007, 02:16 PM
  5. Moving Address Books (.WAB files!)
    By Mintsoft in forum Windows
    Replies: 0
    Last Post: 29th June 2006, 01:18 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •