windows 如何创建批处理文件以重命名文件夹中的大量文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3808001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:38:09  来源:igfitidea点击:

How do I create batch file to rename large number of files in a folder?

windowsbatch-filerenamecmd

提问by user459962

I'd like to rename a large number of files within a folder on a WinXP system, preferably using a batch file.

我想在 WinXP 系统上的文件夹中重命名大量文件,最好使用批处理文件。

The files are currently named like this:

这些文件目前命名如下:

Vacation2010 001.jpg
Vacation2010 002.jpg
Vacation2010 003.jpg

假期2010 001.jpg
假期2010 002.jpg
假期2010 003.jpg

And I'd like to change them to:

我想将它们更改为:

December 001.jpg
December 002.jpg
December 003.jpg

十二月 001.jpg
十二月 002.jpg
十二月 003.jpg

How can I perform this operation??

我怎样才能执行这个操作??

回答by RoXX

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=Vacation2010
SET new=December
for /f "tokens=*" %%f in ('dir /b *.jpg') do (
  SET newname=%%f
  SET newname=!newname:%old%=%new%!
  move "%%f" "!newname!"
)

What this does is it loops over all .jpg files in the folder where the batch file is located and replaces the Vacation2010 with December inside the filenames.

它的作用是遍历批处理文件所在文件夹中的所有 .jpg 文件,并将 Vacation2010 替换为文件名中的 December 。

回答by user459962

dir /b *.jpg >file.bat

This will give you lines such as:

这将为您提供诸如以下内容的行:

Vacation2010 001.jpg
Vacation2010 002.jpg
Vacation2010 003.jpg

假期2010 001.jpg
假期2010 002.jpg
假期2010 003.jpg

Edit file.bat in your favorite Windowstext-editor, doing the equivalent of:

在您最喜欢的 Windows文本编辑器中编辑 file.bat ,执行等效于:

s/Vacation2010(.+)/rename "&" "December "/

That's a regex; many editors support them, but none that come default with Windows (as far as I know). You can also get a command line tool such as sed or perl which can take the exact syntax I have above, after escaping for the command line.

那是一个正则表达式;许多编辑器支持它们,但没有一个是 Windows 默认的(据我所知)。在转义命令行后,您还可以获得命令行工具,例如 sed 或 perl,它们可以采用我上面的确切语法。

The resulting lines will look like:

结果行将如下所示:

rename "Vacation2010 001.jpg" "December 001.jpg"
rename "Vacation2010 002.jpg" "December 002.jpg"
rename "Vacation2010 003.jpg" "December 003.jpg"

重命名 "Vacation2010 001.jpg" "December 001.jpg"
重命名 "Vacation2010 002.jpg" "December 002.jpg"
重命名 "Vacation2010 003.jpg" "December 003.jpg"

You may recognize these lines as rename commands, one per file from the original listing. ;) Run that batch file in cmd.exe.

您可以将这些行识别为重命名命令,来自原始列表的每个文件一个。;) 在 cmd.exe 中运行该批处理文件。

回答by ghostdog74

you can do this easily without manual editing or using fancy text editors. Here's a vbscript.

您无需手动编辑或使用精美的文本编辑器即可轻松完成此操作。这是一个vbscript。

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
    If objFS.GetExtensionName(strFile) = "jpg" Then    
        strFileName = strFile.Name
        If InStr(strFileName,"Vacation2010") > 0 Then           
            strNewFileName = Replace(strFileName,"Vacation2010","December")
            strFile.Name = strNewFileName
        End If 
    End If  
Next 

save as myscript.vbs and

另存为 myscript.vbs 和

C:\test> cscript //nologo myscript.vbs 

回答by ingroxd

@ECHO off & SETLOCAL EnableDelayedExpansion

SET "_dir=" REM Must finish with '\'
SET "_ext=jpg"
SET "_toEdit=Vacation2010"
SET "_with=December"
FOR %%f IN ("%_dir%*.%_ext%") DO (
    CALL :modifyString "%_toEdit%" "%_with%" "%%~Nf" fileName 
    RENAME "%%f" "!fileName!%%~Xf"
)
GOTO end

:modifyString what with in toReturn
    SET "__in=%~3"
    SET "__in=!__in:%~1=%~2!"
    IF NOT "%~4" == "" (
        SET %~4=%__in%
    ) ELSE (
        ECHO %__in%
    )
    EXIT /B

:end

This script allows you to change the name of all the files that contain Vacation2010with the same name, but with Decemberinstead of Vacation2010.

该脚本可以让你改变所有包含文件名Vacation2010具有相同的名称,但December不是Vacation2010

If you copy and paste the code, you have to save the .batin the same folder of the photos. If you want to save the script in another directory [E.G. you have a favorite folder for the utilities] you have to change the value of _dirwith the path of the photos.

如果您复制并粘贴代码,则必须将其保存.bat在照片的同一文件夹中。如果您想将脚本保存在另一个目录中 [例如,您有一个收藏实用程序的文件夹],您必须_dir使用照片的路径更改 的值。

If you have to do the same work for other photos [or others files changig _ext] you have to change the value of _toEditwith the string you want to change [or erase] and the value of _withwith the string you want to put instead of _toEdit[SET "_with="if you simply want to erase the string specified in _toEdit].

如果您必须对其他照片 [或其他文件 changig _ext]执行相同的工作,则必须使用要更改_toEdit的字符串 [或擦除] 更改 的值,_with并使用要放置的字符串代替_toEdit[SET "_with="如果您只想删除_toEdit] 中指定的字符串。

回答by Ani Menon

You don't need a batch file, just do this from powershell :

您不需要批处理文件,只需从 powershell 执行此操作:

powershell -C "gci | % {rni $_.Name ($_.Name -replace 'Vacation2010', 'December')}"