我有一个包含许多文件的文件夹。看起来像file1.txt,newFile1.txt,file2.txt,newFile2.txt,file3.txt,newFile3.txt,file4.txt,newFile4.txt,....
我有一个生成newFilei.txt的代码。 我想写一个vbscript来检查这个文件夹中是否存在newFile。 我试过这个
Set objFolder = FSO.GetFolder("C:\myFolder\")
For Each objFile In objFolder.Files
fileName=objFile.name
If instr(fileName,"newFile*") =1 Then
WScript.Echo "new File exist"
End If
Next
但这没有用。任何想法?
答案 0 :(得分:11)
编辑:COM对象使这非常简单。
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\myFolder\newFile.txt") Then
'Perform Code
End If
或者,如果您希望代码正常工作
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder("C:\myFolder\")
For Each objFile In objFolder.Files
fileName=objFile.name
If instr(fileName,"newFile") Then
WScript.Echo "new File found"
End If
Next
<小时/> 而且,将它们全部拉到一起。
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder("C:\myFolder\")
Set objFiles = objFolder.Files
For i=0 to objFiles.Count
If FSO.FileExists("C:\myFolder\newFile" & i & ".txt") Then
WScript.Echo "new File found"
End If
Next