File 比较文件名并在Vbscript中复制后结束循环
我正在尝试根据数字列表查找目录中的文件。每个文件都以6位数字开头,但也可以以随机字母结尾。 -所以我设法编写了一个脚本,只比较文件名的前六个字母。 另外,我只想复制.pdf和.dxf文件 -这就是为什么我还要检查文件类型 只是为了使它更复杂,“Sourcefolder”对于文件是不同的。 它由数字的前3位数字和2x“\u1”组成。比如说 C:\files\258\u\258956.pdf 这可以通过在源路径中添加前三位数字和u u来轻松解决。(但尚未实施) 问题是我的脚本确实无限运行,因为它从不离开循环。 而且它只检查文件列表中的第一个数字 在提出这个问题之前,我确实在网上搜索过。此外,我还想为stackoverflow社区提供道具,因为到目前为止,其他问题确实有所帮助File 比较文件名并在Vbscript中复制后结束循环,file,loops,vbscript,readline,fso,File,Loops,Vbscript,Readline,Fso,我正在尝试根据数字列表查找目录中的文件。每个文件都以6位数字开头,但也可以以随机字母结尾。 -所以我设法编写了一个脚本,只比较文件名的前六个字母。 另外,我只想复制.pdf和.dxf文件 -这就是为什么我还要检查文件类型 只是为了使它更复杂,“Sourcefolder”对于文件是不同的。 它由数字的前3位数字和2x“\u1”组成。比如说 C:\files\258\u\258956.pdf 这可以通过在源路径中添加前三位数字和u u来轻松解决。(但尚未实施) 问题是我的脚本确实无限运行,因为它从不
- 我通常只使用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
我的问题的解决方案可能非常简单,但我真的被卡住了。因此,我们非常感谢您的帮助。
总而言之,我最大的问题是,脚本永远不会退出循环。
但我不知道如何以及何时结束它,10000个循环之后将是一个愚蠢的解决方案。您需要将您的读线移动到Do循环中,如下所示:
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
一个问题是Do循环。它将永远不会是一个Endofstream,因此永远不会完成运行。解决方法是将读线移动到循环中。没错,但是我应该把它放在哪里?难道我不需要先检查文件夹中的所有文件的文件名,然后再转到文件列表中的下一个文件名吗?循环已经出现在“copy命令”之后。ReadLine命令将是Do-Until下面的第一条语句。请看下面我的答案。非常感谢你的回答,我一定会尝试的。现在我知道把读线放在哪里了!谢谢