比较文件名并将其复制到 Vbscript 后结束循环
End loop after comparing filename and copying it in Vbscript
我正在尝试根据数字列表在目录中查找文件。每个文件都以 6 位数字开头,但可以以随机字母结尾。
- 所以我设法写了一个脚本,只比较文件名的前六个字母。
我也只想复制 .pdf 和 .dxf 文件
-这就是为什么我还要检查文件类型的原因。
只是为了使它更复杂,"Sourcefolder" 与文件不同。
它由数字的前 3 位数字和 2x“_”组成。例如。
C:\files8__8956.pdf
这可以通过在源路径中添加前三位数字和 __ 轻松解决。 (但尚未实施)
问题是我的脚本会无限地 运行 因为它永远不会离开循环。
它也只检查文件列表中的第一个数字。
这个问题之前真的在网上搜过。此外,我想为 Whosebug 社区提供支持,因为其他问题直到现在才真正有所帮助。
- 我一般只在vba(Excel)写脚本,这就是我对vbs不熟悉的原因。
按照我的文件列表的一部分,它定义了必须找到哪些文件
127287
257391
257605
258956
261648
261880
261886
262284
这是我已有的脚本
dim fso, folder, sourcefolder, destfolder, searchname1,fileList
set fso = createobject("scripting.filesystemobject")
sourcefolder = "C:\Users\Admin\Desktop\Source"
destfolder = "C:\Users\Admin\Desktop\output\"
inputFile = "C:\Users\Admin\Desktop\filelist.txt"
Set fileList = fso.OpenTextFile(inputFile, forReading)
' Read line after line the next number which has to be found
searchname1 = fileList.ReadLine()
' But stop reading lines if the end is reached
Do until fileList.AtEndOfStream
set folder = fso.getfolder(sourcefolder)
'Compare each file
for each file in folder.files
wholefilename = fso.getbasename(file)
filename = left(wholefilename,6)
extension = LCase(fso.getextensionName(file))
'first compare the name with the list ¦ then check the fileextenstion
if (filename = searchname1) and ((extension = "pdf") or (extension ="dxf")) then
'it both statements are true, copy the file to the destinationfolder "Ouptut"
fso.copyfile sourcefolder & "\" & file.name, destfolder
else
end if
next
Loop
我的问题的解决方案可能非常简单,但我真的卡住了。所以任何帮助表示赞赏。
总而言之,我的大问题是脚本永远不会退出循环。
但我不知道如何以及何时结束它,在 10'000 个循环之后将是一个愚蠢的解决方案。
您需要像这样在 Do 循环中移动 ReadLine:
Dim fso, folder, sourcefolder, destfolder, searchname1, fileList
Set fso = CreateObject("scripting.filesystemobject")
sourcefolder = "C:\Users\Admin\Desktop\Source"
destfolder = "C:\Users\Admin\Desktop\output\"
inputFile = "C:\Users\Admin\Desktop\filelist.txt"
Set fileList = fso.OpenTextFile(inputFile, forReading)
' But stop reading lines if the end is reached
Do Until fileList.AtEndOfStream
' Read line after line the next number which has to be found
searchname1 = fileList.ReadLine()
Set folder = fso.getfolder(sourcefolder)
'Compare each file
For Each File In folder.Files
wholefilename = fso.getbasename(File)
FileName = Left(wholefilename, 6)
extension = LCase(fso.getextensionName(File))
'first compare the name with the list ¦ then check the fileextenstion
If (FileName = searchname1) And ((extension = "pdf") Or (extension = "dxf")) Then
'it both statements are true, copy the file to the destinationfolder "Ouptut"
fso.copyfile sourcefolder & "\" & File.name, destfolder
End If
Next
Loop
我正在尝试根据数字列表在目录中查找文件。每个文件都以 6 位数字开头,但可以以随机字母结尾。 - 所以我设法写了一个脚本,只比较文件名的前六个字母。 我也只想复制 .pdf 和 .dxf 文件 -这就是为什么我还要检查文件类型的原因。
只是为了使它更复杂,"Sourcefolder" 与文件不同。 它由数字的前 3 位数字和 2x“_”组成。例如。
C:\files8__8956.pdf
这可以通过在源路径中添加前三位数字和 __ 轻松解决。 (但尚未实施)
问题是我的脚本会无限地 运行 因为它永远不会离开循环。 它也只检查文件列表中的第一个数字。
这个问题之前真的在网上搜过。此外,我想为 Whosebug 社区提供支持,因为其他问题直到现在才真正有所帮助。
- 我一般只在vba(Excel)写脚本,这就是我对vbs不熟悉的原因。
按照我的文件列表的一部分,它定义了必须找到哪些文件
127287
257391
257605
258956
261648
261880
261886
262284
这是我已有的脚本
dim fso, folder, sourcefolder, destfolder, searchname1,fileList
set fso = createobject("scripting.filesystemobject")
sourcefolder = "C:\Users\Admin\Desktop\Source"
destfolder = "C:\Users\Admin\Desktop\output\"
inputFile = "C:\Users\Admin\Desktop\filelist.txt"
Set fileList = fso.OpenTextFile(inputFile, forReading)
' Read line after line the next number which has to be found
searchname1 = fileList.ReadLine()
' But stop reading lines if the end is reached
Do until fileList.AtEndOfStream
set folder = fso.getfolder(sourcefolder)
'Compare each file
for each file in folder.files
wholefilename = fso.getbasename(file)
filename = left(wholefilename,6)
extension = LCase(fso.getextensionName(file))
'first compare the name with the list ¦ then check the fileextenstion
if (filename = searchname1) and ((extension = "pdf") or (extension ="dxf")) then
'it both statements are true, copy the file to the destinationfolder "Ouptut"
fso.copyfile sourcefolder & "\" & file.name, destfolder
else
end if
next
Loop
我的问题的解决方案可能非常简单,但我真的卡住了。所以任何帮助表示赞赏。 总而言之,我的大问题是脚本永远不会退出循环。 但我不知道如何以及何时结束它,在 10'000 个循环之后将是一个愚蠢的解决方案。
您需要像这样在 Do 循环中移动 ReadLine:
Dim fso, folder, sourcefolder, destfolder, searchname1, fileList
Set fso = CreateObject("scripting.filesystemobject")
sourcefolder = "C:\Users\Admin\Desktop\Source"
destfolder = "C:\Users\Admin\Desktop\output\"
inputFile = "C:\Users\Admin\Desktop\filelist.txt"
Set fileList = fso.OpenTextFile(inputFile, forReading)
' But stop reading lines if the end is reached
Do Until fileList.AtEndOfStream
' Read line after line the next number which has to be found
searchname1 = fileList.ReadLine()
Set folder = fso.getfolder(sourcefolder)
'Compare each file
For Each File In folder.Files
wholefilename = fso.getbasename(File)
FileName = Left(wholefilename, 6)
extension = LCase(fso.getextensionName(File))
'first compare the name with the list ¦ then check the fileextenstion
If (FileName = searchname1) And ((extension = "pdf") Or (extension = "dxf")) Then
'it both statements are true, copy the file to the destinationfolder "Ouptut"
fso.copyfile sourcefolder & "\" & File.name, destfolder
End If
Next
Loop