VBScript - 按文件名中的字符串查找文件夹中的文件,将找到的文件移动到不同的文件夹
VBScript - Find files in folder by string in filename, move found to different folder
我仍在学习 VBS,不确定我是否以正确或最有效的方式进行此操作。测试场景如下,除了常规的桌面项目外,我还添加了三个.txt文件,命名为:
"Tool - YouTube"
"welcome to facebook"
"BBC news"
当我的代码(底部)运行时,它会按预期创建 "Sorted" 文件夹(如果它不存在),但只有 "BBC news" 文本文件被 'instr' 函数。此外,如果 "Sorted" 文件夹中已经存在 "BBC news" 文本文件,那么 运行 脚本将 return.
Line 20
Char 2
Error: File already exists
Code: 800A003A
运行时的脚本应根据搜索字符串在 'source' 文件夹中找到任何文件,并将它们移动到另一个 'destination' 文件夹。如果 'destination' 文件夹中存在重复文件,则应将其替换为 'source' 文件夹中的文件。请任何人解释我应该应用哪些更改才能使我的脚本正常工作?
dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3
sourcefolder = "C:\Users\...\Desktop"
destfolder = "C:\Users\...\Desktop\Sorted\"
searchname1 = "youtube"
searchname2 = "bbc"
searchname3 = "facebook"
set fso = createobject("scripting.filesystemobject")
set folder = fso.getfolder(sourcefolder)
if not fso.folderexists(destfolder) then
newfolder = fso.createfolder(destfolder)
wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
end if
for each file in folder.files
x = fso.getbasename(file)
if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) or instr(lcase(x), searchname3) then
fso.movefile sourcefolder & "\" & file.name, destfolder
wscript.echo"Files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
wscript.quit()
else
wscript.echo "No matches found"
wscript.quit()
end if
next
给你,我修复了你的 if/then 语句并添加了 if fso.fileexists
:
dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3
sourcefolder = "C:\Users\...\Desktop"
destfolder = "C:\Users\...\Desktop\Sorted\"
searchname1 = "youtube"
searchname2 = "bbc"
searchname3 = "facebook"
set fso = createobject("scripting.filesystemobject")
set folder = fso.getfolder(sourcefolder)
if not fso.folderexists(destfolder) then
newfolder = fso.createfolder(destfolder)
wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
end if
for each file in folder.files
x = fso.getbasename(file)
if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) > 0 or instr(lcase(x), searchname3) > 0 then
if fso.fileexists(destfolder & "\" & file.name) then
fso.deletefile destfolder & "\" & file.name, true
fso.movefile sourcefolder & "\" & file.name, destfolder
else
fso.movefile sourcefolder & "\" & file.name, destfolder
end if
end if
next
wscript.echo "Files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
任务 "move a given set of file to another folder" 的 计划 是:
Define the list L of file(name)s
For Each f in L
If f exists in SrcFolder
copy file(f) to DstFolder (with overwrite)
delete file(f)
End If
Next
(抱歉,对 Christophers/Nathan 的代码进行任何编辑都不会导致该计划的实施)
非常感谢 Nathan 的贡献,我稍微开发了代码以包含 "no matches" msgbox(如最初预期的那样)和一个计数器。我与自己争论过 :P 是否应该说明此评论中的后续内容,从接下来的冗长内容中您可以想象结果是肯定的。
自从我一直在使用这些论坛解决 VBS 问题以来,Ekkehard 一直是 swift 可靠的帮助和指导来源,我非常感谢他的所有建议,并且我学到了很多从他的答复。然而,在上述回复(请参阅我之前的帖子)中,我经常被提醒我在这个问题上完全无能、无知和混乱——我承认这是一个需要帮助的微不足道的学习者。为此,我将并且已经避免通过任何该死的判断或严厉的批评,因为我没有资格这样做,关于他的帮助的性质,即他看似粗暴的风格和态度(毕竟我在这里得到答案、解释并向你们中有更多经验的人学习),所以我将继续假定他的理由是积极的。我想,毕竟,一个在任何学科上都非常精通的人,如果愿意的话,可以对他人表现出迂腐、居高临下甚至侮辱他人的态度,因为这可能在主观上被解释为一种激励或刺激像我这样的新手的手段,推动他们自己找到解决方案并更深入地了解主题,相反,我认为这是一种说教警告,它也可以用来挫败、混淆或阻止个人想要学习...
不管怎样,这都是题外话,即我的 OP,已经回答了,谢谢两位好心的先生,你们帮了大忙。我修改后的代码如下。
dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3, i
sourcefolder = "C:\Users\...\Desktop"
destfolder = "C:\Users\...\Desktop\Sorted\"
searchname1 = "youtube"
searchname2 = "bbc"
searchname3 = "facebook"
i = 0
set fso = createobject("scripting.filesystemobject")
set folder = fso.getfolder(sourcefolder)
if not fso.folderexists(destfolder) then
newfolder = fso.createfolder(destfolder)
wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
end if
for each file in folder.files
x = fso.getbasename(file)
if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) > 0 or instr(lcase(x), searchname3) > 0 then
i = i+1
if fso.fileexists(destfolder & "\" & file.name) then
fso.deletefile destfolder & "\" & file.name, true
end if
fso.movefile sourcefolder & "\" & file.name, destfolder
end if
next
if i>0 then
wscript.echo i&" files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
wscript.quit()
end if
wscript.echo "No matches found"
我仍在学习 VBS,不确定我是否以正确或最有效的方式进行此操作。测试场景如下,除了常规的桌面项目外,我还添加了三个.txt文件,命名为:
"Tool - YouTube"
"welcome to facebook"
"BBC news"
当我的代码(底部)运行时,它会按预期创建 "Sorted" 文件夹(如果它不存在),但只有 "BBC news" 文本文件被 'instr' 函数。此外,如果 "Sorted" 文件夹中已经存在 "BBC news" 文本文件,那么 运行 脚本将 return.
Line 20
Char 2
Error: File already exists
Code: 800A003A
运行时的脚本应根据搜索字符串在 'source' 文件夹中找到任何文件,并将它们移动到另一个 'destination' 文件夹。如果 'destination' 文件夹中存在重复文件,则应将其替换为 'source' 文件夹中的文件。请任何人解释我应该应用哪些更改才能使我的脚本正常工作?
dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3
sourcefolder = "C:\Users\...\Desktop"
destfolder = "C:\Users\...\Desktop\Sorted\"
searchname1 = "youtube"
searchname2 = "bbc"
searchname3 = "facebook"
set fso = createobject("scripting.filesystemobject")
set folder = fso.getfolder(sourcefolder)
if not fso.folderexists(destfolder) then
newfolder = fso.createfolder(destfolder)
wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
end if
for each file in folder.files
x = fso.getbasename(file)
if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) or instr(lcase(x), searchname3) then
fso.movefile sourcefolder & "\" & file.name, destfolder
wscript.echo"Files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
wscript.quit()
else
wscript.echo "No matches found"
wscript.quit()
end if
next
给你,我修复了你的 if/then 语句并添加了 if fso.fileexists
:
dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3
sourcefolder = "C:\Users\...\Desktop"
destfolder = "C:\Users\...\Desktop\Sorted\"
searchname1 = "youtube"
searchname2 = "bbc"
searchname3 = "facebook"
set fso = createobject("scripting.filesystemobject")
set folder = fso.getfolder(sourcefolder)
if not fso.folderexists(destfolder) then
newfolder = fso.createfolder(destfolder)
wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
end if
for each file in folder.files
x = fso.getbasename(file)
if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) > 0 or instr(lcase(x), searchname3) > 0 then
if fso.fileexists(destfolder & "\" & file.name) then
fso.deletefile destfolder & "\" & file.name, true
fso.movefile sourcefolder & "\" & file.name, destfolder
else
fso.movefile sourcefolder & "\" & file.name, destfolder
end if
end if
next
wscript.echo "Files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
任务 "move a given set of file to another folder" 的 计划 是:
Define the list L of file(name)s
For Each f in L
If f exists in SrcFolder
copy file(f) to DstFolder (with overwrite)
delete file(f)
End If
Next
(抱歉,对 Christophers/Nathan 的代码进行任何编辑都不会导致该计划的实施)
非常感谢 Nathan 的贡献,我稍微开发了代码以包含 "no matches" msgbox(如最初预期的那样)和一个计数器。我与自己争论过 :P 是否应该说明此评论中的后续内容,从接下来的冗长内容中您可以想象结果是肯定的。
自从我一直在使用这些论坛解决 VBS 问题以来,Ekkehard 一直是 swift 可靠的帮助和指导来源,我非常感谢他的所有建议,并且我学到了很多从他的答复。然而,在上述回复(请参阅我之前的帖子)中,我经常被提醒我在这个问题上完全无能、无知和混乱——我承认这是一个需要帮助的微不足道的学习者。为此,我将并且已经避免通过任何该死的判断或严厉的批评,因为我没有资格这样做,关于他的帮助的性质,即他看似粗暴的风格和态度(毕竟我在这里得到答案、解释并向你们中有更多经验的人学习),所以我将继续假定他的理由是积极的。我想,毕竟,一个在任何学科上都非常精通的人,如果愿意的话,可以对他人表现出迂腐、居高临下甚至侮辱他人的态度,因为这可能在主观上被解释为一种激励或刺激像我这样的新手的手段,推动他们自己找到解决方案并更深入地了解主题,相反,我认为这是一种说教警告,它也可以用来挫败、混淆或阻止个人想要学习...
不管怎样,这都是题外话,即我的 OP,已经回答了,谢谢两位好心的先生,你们帮了大忙。我修改后的代码如下。
dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3, i
sourcefolder = "C:\Users\...\Desktop"
destfolder = "C:\Users\...\Desktop\Sorted\"
searchname1 = "youtube"
searchname2 = "bbc"
searchname3 = "facebook"
i = 0
set fso = createobject("scripting.filesystemobject")
set folder = fso.getfolder(sourcefolder)
if not fso.folderexists(destfolder) then
newfolder = fso.createfolder(destfolder)
wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
end if
for each file in folder.files
x = fso.getbasename(file)
if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) > 0 or instr(lcase(x), searchname3) > 0 then
i = i+1
if fso.fileexists(destfolder & "\" & file.name) then
fso.deletefile destfolder & "\" & file.name, true
end if
fso.movefile sourcefolder & "\" & file.name, destfolder
end if
next
if i>0 then
wscript.echo i&" files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
wscript.quit()
end if
wscript.echo "No matches found"