65.9K
CodeProject 正在变化。 阅读更多。
Home

如何使用 VBS 重命名多个文件

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (8投票s)

2006年10月31日

CPOL

3分钟阅读

viewsIcon

196295

downloadIcon

1642

这段VBScript代码可以批量重命名文件夹中的多个文件,替换特定的模式和扩展名。

og-bit rename files

引言

我经常需要重命名特定文件夹中的多个文件。这些文件可能包含我想删除的特定字符串模式和/或我想更改的扩展名。当我将数据库中的对象脚本化到文本文件时,我经常会遇到这种情况。当我提取表和存储过程时,我通常会创建许多文件,这些文件可能具有需要更改的特定模式和扩展名。例如,我喜欢对表脚本使用TAB扩展名而不是SQL扩展名。

我编写了这个脚本,以便于批量重命名文件。

背景

此脚本使用Scripting.FileSystemObject COM 对象。此对象用于访问计算机的文件系统。这允许您创建和更改现有文件。

使用代码

此脚本迭代特定文件夹中的所有文件。它在每个文件中搜索字符串和扩展名模式。如果找到模式,则将其替换为提供的新的字符串和扩展名。

运行脚本的命令行语法如下:

cscript rename.vbs [folder path] [string to replace] 
        [new string] [old extension] [new extension]
[文件夹路径] 文件夹位置
[要替换的字符串] 要查找的模式
[新字符串] 用于替换模式的新字符串
[旧扩展名] 要查找的扩展名(可选)
[新扩展名] 新的扩展名(可选)

使用方法:

cscript rename.vbs . test ozkar txt tab

如果只需要替换所有文件的扩展名,请将要替换的扩展名模式输入为[要替换的字符串]参数。脚本编写允许您同时替换两种模式,仅当找到第一种模式时才进行替换。

代码分为三个主要部分。入口点Main处理参数验证。对FileSystemObject的引用被赋值给fso变量。参数由objArgs变量引用,该变量保存一组值。前三个参数是必需的。最后两个参数是可选的。验证参数后,调用fso.GetFolder来获取该文件夹的句柄。发出警告以允许用户确认任务。然后调用ProcessFolder函数来重命名文件。

Option Explicit

Dim StdIn:  Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript
Dim fso:    Set fso = CreateObject("Scripting.FileSystemObject")

Dim FilesRenamed:   FilesRenamed = 0
Dim FilesSkipped:   FilesSkipped = 0

Main

set fso = nothing

Sub Main
      
   'get the parameter list   
   dim objArgs: Set objArgs = WScript.Arguments

   if objArgs.Count > 2 then
      
    dim path: path = objArgs(0)  'path
    dim olds: olds = objArgs(1)  'string to replace
    dim news: news = objArgs(2)  'new string
    
    dim ext1: ext1 = ""
    dim ext2: ext2 = ""
    if objArgs.Count > 3 then ext1 = objArgs(3)  'old extension
    if objArgs.Count > 4 then ext2 = objArgs(4)  'new extension            

    dim CurrentFolder: Set CurrentFolder = fso.GetFolder(path)
    StdOut.Echo "Warning: All files within the directory """ & _
                CurrentFolder.Path & """ will be renamed."
    If Not Confirm("Continue?") Then Exit Sub
    
    ProcessSubFolders CurrentFolder , olds, news, ext1,ext2
    
    StdOut.Echo "Files renamed :" & FilesRenamed
    StdOut.Echo "Files Skipped :" & FilesSkipped 
    
   else
       StdOut.Echo "Usage: rename.vbs [folder path] [string to replace]" & _ 
                   " [new string] [old extension] [new extension] "
   end if
   
End Sub

Confirm函数会回显一条消息并等待标准输入返回。用户必须输入Y才能继续。

Function Confirm (ByVal promptText)   
      StdOut.Echo promptText & " (y/n) "
      Dim s: s =  StdIn.ReadLine()      
      Select Case LCase(Trim(s))         
         Case "y"         
            Confirm = True
            exit function 
         Case else            
             Confirm = False
             exit function            
      End Select      
         
End Function

ProcessSubFolder函数迭代当前目录中的所有子文件夹,并为找到的每个子文件夹递归调用相同的函数。这允许它向下搜索文件夹树。对于找到的每个文件夹,它都会调用ProcessFolder函数。

Sub ProcessSubFolders (ByVal crfolder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
  Dim Folders: Set Folders = crfolder.SubFolders
  Dim Folder 'process the current folder 
  ProcessFolder crfolder , oldTag, newTag, extOld,extNew
  For Each Folder in FoldersNext 
      ProcessSubFolders Folder , oldTag, newTag, extOld,extNew
  End
Sub 

ProcessFolder函数完成所有繁重的工作。它迭代文件夹中的所有文件。如果它在文件名中找到模式,它会检查是否也提供了扩展名模式。然后,它替换所有字符串模式并调用File.Move

Sub ProcessFolder (ByVal folder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
   Dim Files: Set Files = folder.Files
   
   Dim File
   For Each File In Files
   
      If inStr(1,File.Name,oldTag) > 0 Then
      
         if (extOld <> "" and extNew <> "") then
            StdOut.Echo Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
            File.Move Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
         else
            StdOut.Echo Replace(File.Path,oldTag,newTag)
            File.Move Replace(File.Path,oldTag,newTag)
         end if
                  
         FilesRenamed = FilesRenamed + 1
      Else
         FilesSkipped = FilesSkipped + 1
       End If
   Next
End Sub

关注点

我希望这个简单的脚本对你们中的一些人有用,就像它对我一样有用。

历史

  • 2006年10月1日 - 初始版本。
  • 2007年10月23日 - 添加了处理子文件夹的功能。
© . All rights reserved.