如何使用vbscript检查文件是否存在 [英] how to check if a file exists with vbscript

查看:42
本文介绍了如何使用vbscript检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,里面有很多文件.看起来像这样 file1.txt, newFile1.txt, file2.txt, newFile2.txt, file3.txt, newFile3.txt, file4.txt, newFile4.txt, ....

I have a folder with many files. It looks like this file1.txt, newFile1.txt, file2.txt, newFile2.txt, file3.txt, newFile3.txt, file4.txt, newFile4.txt, ....

我有一个生成 newFilei.txt 的代码.我想编写一个 vbscript 来检查此文件夹中是否存在 newFile.我试过这个

I have a code that generates the newFilei.txt . I want to write a vbscript that checks if a newFile exists in this folder or not. I tried this

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 

但这没有用.有什么想法吗?

but this didnt work. any ideas ?

推荐答案

COM 对象使这变得非常简单.

The COM object made this very simple.

Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")

If fso.FileExists("C:\myFolder\newFile.txt") Then
    'Perform Code
End If

<小时>

或者,如果您希望代码正常运行


Or, if you wanted your code to work

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 

<小时>而且,把这一切都拉在一起.


And, pulling it all together.

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 

这篇关于如何使用vbscript检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆