Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "."
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
Set index = 1
For each folderIdx In files
File.Move Replace(File.Path,folderIdx ,index)
index = index + 1
Next
这不起作用..这个脚本中的问题是什么?
修改 工作脚本。我需要文件名为“001”,“002”,...等而不仅仅是“1”,“2”..
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sDir : sDir = "C:\Users\Computer\Desktop\icons\"
Dim nIdx : nIdx = 1
Dim oFile
For Each oFile In oFS.GetFolder(sDir).Files
If oFS.FileExists(oFS.BuildPath(sDir, nIdx&"."&oFile.Name)) Then
WScript.StdOut.WriteLine " already exists"
Else
oFile.Name = nIdx&"."&oFile.Name
End If
nIdx = nIdx + 1
Next
答案 0 :(得分:3)
问题:您使用File
作为.Move和Replace调用,但是您的For Each
循环为您提供了名为folderIdx
的变量中的当前文件对象。
您的替换只会产生预期的文件名,并可能将文件移动到当前文件夹(如FS所示)。 (我没有测试这种推测)
要根据增加的索引重命名/重新编号文件夹中的所有文件,我会使用:
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sDir : sDir = "..\testdata\17165630"
Dim nIdx : nIdx = 1
Dim oFile
For Each oFile In oFS.GetFolder(sDir).Files
WScript.Echo "bad idea:", Replace(oFile.Path, oFile, nIdx)
WScript.StdOut.Write oFile.Name
If oFS.FileExists(oFS.BuildPath(sDir, nIdx)) Then
WScript.StdOut.WriteLine " already exists"
Else
oFile.Name = nIdx
WScript.StdOut.WriteLine " => " & oFile.Name
End If
nIdx = nIdx + 1 ' Thanks, @Ansgar!
Next
输出:
bad idea: 1
5 => 1
bad idea: 2
6 => 2
bad idea: 3
8 => 3
bad idea: 4
7 => 4
应该让您对重新编号操作之前和之后的顺序保持谨慎。
<强>更新强>
要预先添加零,请使用像:
这样的思考>> For Each nIdx In Array(1, 5, 10, 99, 100, 999)
>> WScript.Echo Right(1000 + nIdx, 3)
>> Next
>>
001
005
010
099
100
999
要保留特定订单,您必须按该顺序处理文件。我首先要炮轰dir /o:<your choice>
。
更新II:
.FileExists检查无法避免重新编号已重新编号的文件。您必须查看当前文件的oFile.Name并跳过它,如果它已被处理。如果您在开头有非数字文件名,则可以使用IsNumeric()
:
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sDir : sDir = "..\testdata\17165630"
Dim nIdx : nIdx = 1
Dim oFile
WScript.Echo "----- Given:"
For Each oFile In oFS.GetFolder(sDir).Files
WScript.Echo oFile.Path
Next
WScript.Echo "----- Rename:"
For Each oFile In oFS.GetFolder(sDir).Files
WScript.StdOut.Write oFile.Name
If IsNumeric(Left(oFile.Name, 3)) Then
WScript.Stdout.WriteLine " is already numbered"
Else
Dim sNewName : sNewName = Right(1000 + nIdx, 3) & "." & oFile.Name
If oFS.FileExists(oFS.BuildPath(sDir, sNewName)) Then
WScript.StdOut.WriteLine " already exists"
Else
oFile.Name = sNewName
WScript.StdOut.WriteLine " => " & oFile.Name
End If
nIdx = nIdx + 1
End If
Next
WScript.Echo "----- Result:"
For Each oFile In oFS.GetFolder(sDir).Files
WScript.Echo oFile.Path
Next
输出两次:
----- Given:
M:\lib\kurs0705\testdata\17165630\c.png
M:\lib\kurs0705\testdata\17165630\a.png
M:\lib\kurs0705\testdata\17165630\b.png
M:\lib\kurs0705\testdata\17165630\d.png
----- Rename:
c.png => 001.c.png
a.png => 002.a.png
b.png => 003.b.png
d.png => 004.d.png
----- Result:
M:\lib\kurs0705\testdata\17165630\002.a.png
M:\lib\kurs0705\testdata\17165630\003.b.png
M:\lib\kurs0705\testdata\17165630\004.d.png
M:\lib\kurs0705\testdata\17165630\001.c.png
----- Given:
M:\lib\kurs0705\testdata\17165630\002.a.png
M:\lib\kurs0705\testdata\17165630\003.b.png
M:\lib\kurs0705\testdata\17165630\004.d.png
M:\lib\kurs0705\testdata\17165630\001.c.png
----- Rename:
002.a.png is already numbered
003.b.png is already numbered
004.d.png is already numbered
001.c.png is already numbered
----- Result:
M:\lib\kurs0705\testdata\17165630\002.a.png
M:\lib\kurs0705\testdata\17165630\003.b.png
M:\lib\kurs0705\testdata\17165630\004.d.png
M:\lib\kurs0705\testdata\17165630\001.c.png