我发现了一个 vbscript,它允许我更新文件名,用“ ”(空格)替换“_”,这非常有用。我希望对这个脚本进行一些小修改,为相同的文件添加前缀。然而,我所做的每一次尝试都只是让我陷入无限循环,超时,或多或少地添加了无数的前缀。我无法找出在每个文件上运行一次后退出循环的正确条件。下面是脚本。任何帮助都非常感谢。
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("C:test")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile, "_", "")
If (sNewFile <> File.Name) Then
File.Move (File.ParentFolder + "\" + sNewFile)
End If
Next
答案1
怎么样:
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")
dim objFolder : Set objFolder = objFSO.GetFolder(".")
For Each File In objFolder.Files
if instr(File.Name, "_") > 0 then
objFSO.MoveFile File.Name, "PREFIX-" & replace(File.Name, "_", " ")
end if
Next
对于与脚本位于同一目录下的每个文件,它将检查文件名是否带有下划线:如果是,则它将该文件重命名为具有“PREFIX-”字符串,并将下划线替换为空格。
可能值得注释掉 MoveFile 行并创建一个新行:
wscript.echo "Renaming: " & File.Name & " to: PREFIX-" & replace(File.Name, "_", " ")
例如
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")
dim objFolder : Set objFolder = objFSO.GetFolder(".")
For Each File In objFolder.Files
if instr(File.Name, "_") > 0 then
'objFSO.MoveFile File.Name, "PREFIX-" & replace(File.Name, "_", " ")
wscript.echo "Renaming: " & File.Name & " to: PREFIX-" & replace(File.Name, "_", " ")
end if
Next
这将向您展示如果运行以下命令它将执行的操作:
cscript script.vbs > out.txt