简体   繁体   English

是否可以使用批处理文件从Windows中的文件名中删除特定字符?

[英]Is there any way to remove specific characters from a filename in Windows, using a batch file?

I would like to rename every file in a directory that contains '[' or ']' characters by removing any instance of those two characters from those filenames. 我想通过从文件名中删除这两个字符的任何实例来重命名包含“ [”或“]”字符的目录中的每个文件。

I have yet to determine any way to read filenames character-by-character with a batch file. 我还没有确定使用批处理文件逐字符读取文件名的任何方法。

Thanks! 谢谢!

You don't need to read the filename character by character. 您无需逐个读取文件名。 In batch you can simply replace all occurrences of a substring subs with another substring repl in the value of a variable %var% with %var:subs=repl% . 在批处理中,您可以使用%var:subs=repl%将变量%var%的值替换为另一个子字符串repl来简单地替换所有出现的子字符串subs As removing is the same as replacing with an empty string (in batch at least), %var:h=% will remove all h s from the value of the var variable. 由于remove等同于用空字符串替换(至少是成批处理),因此%var:h=%将从var变量的值中删除所有h s。 So all you need is to store the filename in a variable and you can have the new filename without [ and/or ] . 因此,您所需要做的就是将文件名存储在变量中,并且可以使用不带[和/或]的新文件名。
To iterate over all files in a directory you'll just need a FOR loop. 要遍历目录中的所有文件,您只需要一个FOR循环。

So you can iterate over all files in the current directory and rename the ones with [ or ] in their name (or both) with the following script (see edit for version which also handles names with exclamation marks): 因此,您可以遍历当前目录中的所有文件,并使用以下脚本将其名称重命名为[]的文件(或同时使用两个名称重命名)(请参见edit for version,该版本也处理带有感叹号的名称):

@echo off
Setlocal EnableDelayedExpansion

FOR %%G IN (*) DO (
    set "filename=%%~G"
    set "filename=!filename:[=!"
    set "filename=!filename:]=!"
    REM Rename only if there is a difference between new filename and old filename in %%G
    IF NOT "!filename!" == "%%~G" ren "%%~G" "!filename!"
)
EndLocal
exit /b 0

Because the filename variable is set inside the FOR block and we want to read it inside the same block, we need delayed expansion . 因为filename变量是在FOR块内设置的,并且我们想在同一块内读取它,所以我们需要延迟扩展

EDIT : As @dbenham said in comment , delayed expansion can cause problems when you have filenames containing exclamation marks ! 编辑 :正如@dbenham在评论中所说, 如果文件名包含感叹号延迟扩展可能会导致问题! ( %%~G will then contain a ! which normally announces a variable being expanded with delayed expansion). %%~G将包含一个! ,通常表明变量在延迟扩展后被扩展)。 To solve that problem, @dbenham proposed a nice solution: enable delayed expansion only when we're about to use it ie inside the FOR block, just before using the filename variable. 为了解决这个问题,@dbenham提出了一个不错的解决方案:仅在我们要使用延迟扩展时才启用延迟扩展,即在FOR块内部,即在使用filename变量之前。 The ! ! won't poisin the value of a variable expanded with delayed expansion so !filename! 不会包含延迟扩展的扩展变量的值,所以!filename! can be used without a ! 可以不用使用! being misinterpreted in its value. 被误解为价值。 This also means we'll have to move EndLocal to after we're done using delayed expansion in the FOR block (ie at the end of the block) and avoid the use of %%~G in the ren command. 这也意味着我们必须在FOR块中使用延迟扩展(即,在该块的末尾)完成后,将EndLocal移至,并避免在ren命令中使用%%~G ren The latter can be done by using a variable to hold the original name (also before enabling delayed expansion) and use delayed expansion to retrieve the original name. 后者可以通过使用变量来保存原始名称(也可以在启用延迟扩展之前)并使用延迟扩展来检索原始名称来完成。 That variable can then actually be used to construct the new filename. 该变量然后可以实际用于构造新文件名。

@echo off

FOR %%G IN (*) DO (
    REM Set filename without delayed expansion, no misinterpreted '!' possible
    set "old_filename=%%~G"
    REM before using old_filename, enable delayed expansion
    SetLocal EnableDelayedExpansion
    set "new_filename=!old_filename:[=!"
    set "new_filename=!new_filename:]=!"
    REM Rename only if there is a difference between new filename and old filename in %%G
    ren "!old_filename!" "!new_filename!"
    REM No more delayed expansion needed in block now
    EndLocal
)

exit /b 0

Also, @KlitosKyriacou pointed out the IF was actually unnecessary: renaming a file with its original filename causes no harm. 另外, @ KlitosKyriacou指出, IF实际上是不必要的:使用其原始文件名重命名文件不会造成任何危害。 I've left it out in my 2nd version (the edit-version). 我已经在第二版(编辑版)中将其遗漏了。

If you are able to use PowerShell and have one folder where all files are located you can run a command like 如果您能够使用PowerShell并且在所有文件都位于一个文件夹中,则可以运行以下命令

Dir | Rename-Item -NewName { $_.Name -replace "[","" }

and

Dir | Rename-Item -NewName { $_.Name -replace "]","" }

to get rid of the characters. 摆脱角色。

You could use JREN.BAT - a regular expression renaming command line utility . 您可以使用JREN.BAT-正则表达式重命名命令行实用程序 JREN.BAT is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe required. JREN.BAT是纯脚本(混合批处理/ JScript),可从XP开始在任何Windows计算机上本机运行-无需第三方exe。

Using alternation, which requires escape of [ 使用交替,这需要转义[

jren "\[|]" ""

or a character set, with escaped ] 或字符集,并带有转义的]

jren "[[\]]" ""

or a character set, with ] as first member of set 或字符集,以]作为集合的第一位成员

jren "[][]" ""

Any of the above syntaxes can easily be extended to strip additional characters. 上面的任何语法都可以轻松扩展为剥离其他字符。

You must use call jren if you put the command within a batch script. 如果将命令放在批处理脚本中,则必须使用call jren

Regardless of what technique you use to solve this, the operation may fail if the renamed value conflicts with an already existing file name. 无论您使用哪种技术来解决此问题,如果重命名的值与现有文件名冲突,操作都可能失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于从文件名中删除特殊字符的批处理文件脚本(Windows) - Batch file script to remove special characters from filenames (Windows) Windows批处理文件从文件名中提取的编号 - Windows batch file extract number from filename 有没有办法从Windows批处理文件启动对话框? - Is there any way to launch dialogs from a windows batch file? Windows 批处理文件,如何重命名和删除部分文件名 - Windows Batch File, how to rename and remove part of filename 使用批处理删除文件的最后n个字符 - remove last n characters of a file using batch 使用 Windows 批处理从文件中删除尾随空格? - Remove trailing spaces from a file using Windows batch? Windows文件名批量复制文件 - Windows Batch Copy file by filename 使用Windows批处理文件将多个文件从具有相同文件名的不同文件夹复制到一个公用文件夹 - Copy multiple files from different folder with same filename to one common folder using windows batch file 使用 Windows 批处理文件根据文件名的一部分将文件分类到文件夹中 - Sorting files into folders, according to a part of filename using Windows Batch file Windows Batch-将目录和文件名与完整文件路径字符串分开 - Windows Batch - separate directory and filename from full file path string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM