Jump to content

Recommended Posts

Posted

Morning Folks,

 

Currently fondling with VBS trying to get a script together to move files based on file name. I have the following 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 ....

Guest TheLibrarian
Posted

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.

Posted
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 :(

Posted

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" or are they called 00 but in a folder called \\server\share\invoices\sage\??

Posted

I'm no good with vbs but here is a copy of a powershell script i have which does the same sort of thing

 

#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
   }
}

  • Thanks 1
Posted
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" or are they called 00 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.

Posted (edited)

I believe this line is wrong

 

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

 

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

\\server\share\invoices\Sage00filename.txt

 

instead of

\\server\share\invoices\Sage\00filename.txt

Edited by ChrisH
Posted
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.

Posted

If you're using:

 

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:

 

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)

  • Thanks 1
Posted (edited)
I believe this line is wrong

 

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

 

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

\\server\share\invoices\Sage00filename.txt

 

instead of

 

\\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.

Edited by ChrisH
Guest TheLibrarian
Posted

Move processed files:

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:

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.

Posted

You need to see what things are being assigned what as mentioned above use an echo to find what the variables are getting assigned;

 

 

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

  • Thanks 1
Posted

Code as shown on my screen;

 

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.

Posted

Done.

 

if left(file.name, 2) = "00" then fso.movefile "server" & file.name, "server" & file.name

 

Removed the chr(34)'s

 

if Left(file.name, 2) = "00" then fso.MoveFile "\\Pghwms1\Pyramid\Report\Test\" & file.name, "\\Pghwms1\Pyramid\Report\Processed\" & file.name

Posted

When code gets long like this and you get funny errors I find it best to assign the file paths to variables instead of all that concatenation in the command.

 

 

Set fso = CreateObject("Scripting.FileSystemObject")
Const DestinationPath = "\\Pghwms1\Pyramid\Report\Processed\"
Set pyramid = fso.GetFolder("\\Pghwms1\Pyramid\Report\Test\")
For Each file In pyramid.Files
OriginalPath = chr(34) & "\\Pghwms1\Pyramid\Report\Sage\" & file.Name & chr(34)


if Left(file.name, 2) = "00" then fso.MoveFile OriginalPath, DestinationPath
Next

 

You can probably do away with the Chr(34) in the assignment as well.

  • Thanks 1
Posted

Hi Everyone

 

Just quick update to my previous post here's a 3 line solution in Powershell

 

#Where to put the files once moved
$Target = "u:\test"
$path = read-host "Please Enter Source Path: "
Get-ChildItem $path -Include *friends*.txt -Recurse | Move-Item -Destination $target

 

Just edit the -include to an expression of your choice.

 

.Adam

Posted

Personally I would rather do the following then have it all on the same line:

 

if Left(file.name, 2) = "00" then fso.MoveFile OriginalPath, DestinationPath

 

Change it to -

if Left(file.name, 2) = "00" then 
fso.MoveFile OriginalPath, DestinationPath
end if

 

Doesnt really change the way it works, both does the same thing but i find the later easier to read.

 

PS. are you still having problems or does it work now?

Posted
Personally I would rather do the following then have it all on the same line:

 

if Left(file.name, 2) = "00" then fso.MoveFile OriginalPath, DestinationPath

 

Change it to -

if Left(file.name, 2) = "00" then 
fso.MoveFile OriginalPath, DestinationPath
end if

 

Doesnt really change the way it works, both does the same thing but i find the later easier to read.

 

PS. are you still having problems or does it work now?

 

Works perfectly now thanks! Busy adding things to the script though to make it an all in one feature-set

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...