vbscript - 如果存在任何文件,则复制并重命名

时间:2016-05-24 12:28:53

标签: vbscript

我有一个vb脚本文件 - main.vbs。其内容如下:

Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
If fileSystemObject.FileExists("D:\a\source.doc") Then
fileSystemObject.CopyFile "D:\b\template.doc", "D:\c\source.doc"
fileSystemObject.MoveFile "D:\a\source.doc" , "D:\d\"
End If

这个脚本的作用是:它检查文件夹'D:\ a'中是否存在名为'source.doc'的文件。如果是,那么脚本,

(i)复制文件'd:\ b'中保存的'template.doc'文件 (ii)将'template.doc'重命名为'source.doc' (iii)并将此重命名的文件移动到文件夹'D:\ c'。 (iv)并将文件'source.doc'从'D:\ a'移动到'D:\ d'

脚本运行正常。但我希望脚本应该在文件名'source'变化时运行(但扩展名为.doc)。

文件名'template.doc'保持不变。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

这更像是dir使用FSO对象。对名称进行测试以决定是否处理它。 fso.copyfile与使用集合和thing.copy循环的For Each相同。

'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
ProcessFolder DirName

Sub ProcessFolder(FolderPath)
'    On Error Resume Next
Set fldr = fso.GetFolder(FolderPath)

Set Fls = fldr.files

For Each thing in Fls
     msgbox Thing.Name & " " & Thing.DateLastModified 
Next

'Uncomment below lines to do sub folders
'    Set fldrs = fldr.subfolders
'    For Each thing in fldrs
'        ProcessFolder thing.path
'    Next

End Sub
相关问题