Moving and Renaming Files Using VBScript
As it turns out I am now being asked to modify the script so it delivers each respective file into its own folder. All files will reside in one folder on a AS400 and will move to different folders on a separate computer. My question is can I get away with something like:
Dim fso,f,strPathBuild
Set fso=CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Z:\SOURCE")
For Each file In f.Files
if left(UCase(file.Name),1) = "A" then strPathBuild = Replace(file, "Z:\SOURCE", "Y:\DESTINATION1")
if left(UCase(file.Name),1) = "A" then strPathBuild = Replace(strPathBuild,"A","ACKNOWLEDGE")
if left(UCase(file.Name),1) = "S" then strPathBuild = Replace(file, "Z:\SOURCE", "Y:\DEST开发者_如何转开发INATION2")
if left(UCase(file.Name),1) = "S" then strPathBuild = Replace(strPathBuild,"S","SHIPMENT")
fso.MoveFile file, strPathBuild
Next
Set f = Nothing
Set fso = Nothing
I apologize but it seems the PM likes to refrain from giving me all the requirements at once.
In answer to your commented question:
Dim fso,f,strPathBuild
Set fso=CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Z:\SOURCE")
For Each file In f.Files
strPathBuild = Replace(file, "Z:\SOURCE", "Y:\DESTINATION")
if left(UCase(file.Name),1) = "A" then strPathBuild = Replace(strPathBuild,"ACK","ACKNOWLEDGE")
fso.MoveFile file, strPathBuild
Next
Set f = Nothing
Set fso = Nothing
Also, as Alex K says, you want to start watching out for case sensitivity on your file names. (That's why I included the UCase function)
The case of the path in the enumeration probably does not match the Replace
search criteria.
Case insensitive replace (with start at char 1, replace 1 time);
strPathBuild = Replace(file, "Z:\SOURCE", "Y:\DESTINATION", 1, 1, vbTextCompare)
精彩评论