VBscript to rename all files in dir to start with an index

前端 未结 1 1665
庸人自扰
庸人自扰 2021-01-15 19:58
Set fso = CreateObject(\"Scripting.FileSystemObject\")
sFolder = \".\"

Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
Set index = 1
For each folderIdx         


        
1条回答
  •  隐瞒了意图╮
    2021-01-15 20:53

    The problem: You use File for the .Move and in the Replace call, but your For Each loop gives you the current file object in the variable named folderIdx.

    Your Replace would result in just the intended file name, and probably move the file to the current folder (as seen by the FS). (I did not test this speculation)

    To rename/renumber all files in a folder according to an increasing index, I'd use:

      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
    

    The output:

    bad idea: 1
    5 => 1
    bad idea: 2
    6 => 2
    bad idea: 3
    8 => 3
    bad idea: 4
    7 => 4
    

    should make you cautious as to the order before and after the renumber action.

    Update:

    To prepend zeros, use somethink like:

    >> For Each nIdx In Array(1, 5, 10, 99, 100, 999)
    >>     WScript.Echo Right(1000 + nIdx, 3)
    >> Next
    >>
    001
    005
    010
    099
    100
    999
    

    To keep a specific order, you'll have to process the files in that order. I'd start with shelling out to dir /o:.

    Update II:

    A .FileExists check can't avoid renumbering an already renumbered file. You must look at the oFile.Name of the current file and skip it, if it was already processed. If you have non-numerical file names in the beginning, you can use 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
    

    Output of two runs:

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

    0 讨论(0)
提交回复
热议问题