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

发布于 2024-09-25 07:15:40 字数 278 浏览 3 评论 0原文

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

这些文件目前的命名方式如下:

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

我想将它们更改为:

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

我该如何执行此操作?

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

The files are currently named like this:

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

And I'd like to change them to:

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

How can I perform this operation??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

執念 2024-10-02 07:15:40
@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!"
)

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

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

蓝天白云 2024-10-02 07:15:40
dir /b *.jpg >file.bat

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

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

在您最喜欢的 Windows 中编辑 file.bat 文本编辑器,相当于:

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

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

结果行将如下所示:

重命名“Vacation2010 001.jpg”“December 001.jpg”
重命名“Vacation2010 002.jpg”“December 002.jpg”
重命名“Vacation2010 003.jpg”“December 003.jpg”

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

dir /b *.jpg >file.bat

This will give you lines such as:

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

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

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

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.

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"

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

寄人书 2024-10-02 07:15:40

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

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 

另存为 myscript.vbs 并

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

you can do this easily without manual editing or using fancy text editors. Here's a 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

C:\test> cscript //nologo myscript.vbs 
听你说爱我 2024-10-02 07:15:40
@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

此脚本允许您更改包含具有相同名称的 Vacation2010 的所有文件的名称,但使用 December 而不是 Vacation2010

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

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

@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 Vacation2010 with the same name, but with December instead of Vacation2010.

If you copy and paste the code, you have to save the .bat in 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 _dir with the path of the photos.

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

年少掌心 2024-10-02 07:15:40

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

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

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

powershell -C "gci | % {rni $_.Name ($_.Name -replace 'Vacation2010', 'December')}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文