1
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
4

1 回答 1

3

问题:您File在 .Move 和 Replace 调用中使用,但您的For Each循环在名为 的变量中为您提供了当前文件对象folderIdx

您的 Replace 将只产生预期的文件名,并可能将文件移动到当前文件夹(如 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>

更新二:

.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
于 2013-06-18T10:17:37.453 回答