使用VBScript移动和重命名文件

时间:2011-06-07 15:55:46

标签: vbscript

事实证明,我现在被要求修改脚本,以便将每个相应的文件传送到自己的文件夹中。所有文件将驻留在AS400上的一个文件夹中,并将移动到单独计算机上的不同文件夹中。我的问题是,我可以通过以下方式逃脱:

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:\DESTINATION2")

if left(UCase(file.Name),1) = "S" then strPathBuild = Replace(strPathBuild,"S","SHIPMENT")

fso.MoveFile file, strPathBuild

Next

Set f = Nothing
Set fso = Nothing

我道歉但似乎PM希望不要立刻给我所有的要求。

2 个答案:

答案 0 :(得分:1)

回答您的评论问题:

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

此外,正如Alex K所说,您想开始关注文件名的区分大小写。 (这就是我加入UCase功能的原因)

答案 1 :(得分:0)

枚举中路径的大小写可能与Replace搜索条件不匹配。

不区分大小写的替换(从char 1开始,替换1次);

strPathBuild = Replace(file, "Z:\SOURCE", "Y:\DESTINATION", 1, 1, vbTextCompare)