VB脚本重命名目录中的所有文件以从索引开始 [英] VB script to rename all files in dir to start with an index

查看:26
本文介绍了VB脚本重命名目录中的所有文件以从索引开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这不起作用..这个脚本有什么问题?

This doesnt work.. Whats the problem in this script?

工作脚本.我需要文件名是001"、002"、……等等,而不仅仅是1"、2"……

Working script. I need the filenames to be "001", "002", ... etc instead of just "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

推荐答案

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

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.

您的替换只会产生预期的文件名,并且可能会将文件移动到当前文件夹(如 FS 所见).(我没有测试这个推测)

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

输出:

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.

更新:

要在前面加上零,请使用以下方法:

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

要保持特定顺序,您必须按该顺序处理文件.我会从炮击 dir/o: 开始.

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

更新二:

.FileExists 检查无法避免对已重新编号的文件进行重新编号.您必须查看当前文件的 oFile.Name 并跳过它,如果它已经被处理.如果开头有非数字文件名,可以使用IsNumeric():

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

两次运行的输出:

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

这篇关于VB脚本重命名目录中的所有文件以从索引开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆