windows 替换或删除文件夹中所有文件的文件名中的某些字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16636996/
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 10:31:25  来源:igfitidea点击:

Replace or delete certain characters from filenames of all files in a folder

windowsfilepowershellbatch-filecmd

提问by VS1

How do I delete certain characters or replace certain characters with other characters by some batch file execution, for filenames of all files in a Windows folder in one go, is there a DOS command for that?

如何通过一些批处理文件执行删除某些字符或用其他字符替换某些字符,一次性删除 Windows 文件夹中所有文件的文件名,是否有 DOS 命令?

回答by Ravi Thapliyal

Use PowerShell to do anything smarter for a DOS prompt. Here, I've shown how to batch rename all the files and directories in the current directory that contain spaces by replacing them with _underscores.

使用 PowerShell 为 DOS 提示符执行更智能的操作。在这里,我展示了如何批量重命名当前目录中包含空格的所有文件和目录,方法是将它们替换为_下划线。

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

EDIT :
Optionally,the Where-Objectcommand can be used to filterout ineligible objects for the successive cmdlet(command-let). The following are some examples to illustrate the flexibility it can afford you:

编辑:
可选地,Where-Object命令可用于过滤掉后续cmdlet(command-let) 的不合格对象。以下是一些示例来说明它可以为您提供的灵活性:

  • To skip any document files

    Dir |
    Where-Object { $_.Name -notmatch "\.(doc|xls|ppt)x?$" } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    
  • To process only directories (pre-3.0 version)

    Dir |
    Where-Object { $_.Mode -match "^d" } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    

    PowerShell v3.0 introduced new Dirflags. You can also use Dir -Directorythere.

  • To skip any files already containing an underscore (or some other character)

    Dir |
    Where-Object { -not $_.Name.Contains("_") } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    
  • 跳过任何文档文件

    Dir |
    Where-Object { $_.Name -notmatch "\.(doc|xls|ppt)x?$" } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    
  • 仅处理目录(3.0 之前的版本)

    Dir |
    Where-Object { $_.Mode -match "^d" } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    

    PowerShell v3.0 引入了新Dir标志。你也可以Dir -Directory在那里使用。

  • 跳过任何已经包含下划线(或其他一些字符)的文件

    Dir |
    Where-Object { -not $_.Name.Contains("_") } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    

回答by aphoe

A one-liner command in Windows PowerShell to delete or rename certain characters will be as below. (here the whitespace is being replaced with underscore)

Windows PowerShell 中删除或重命名某些字符的单行命令如下所示。(这里的空格被下划线替换)

Dir | Rename-Item –NewName { $_.name –replace " ","_" }

回答by Dustin Malone

The PowerShell answers are good, but the Rename-Item command doesn't work in the same target directory unless ALL of your files have the unwanted character in them (fails if it finds duplicates).

PowerShell 的答案很好,但 Rename-Item 命令在同一目标目录中不起作用,除非您的所有文件都包含不需要的字符(如果发现重复则失败)。

If you're like me and had a mix of good names and bad names, try this script instead (will replace spaces with an underscore):

如果你像我一样,既有好名字也有坏名字,试试这个脚本(将用下划线替换空格):

Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace(" ", "_") }

回答by foxidrive

This batch file can help, but it has some limitations. The filename characters = and % cannot be replaced (going from memory here) and an ^ in the filenames might be a problem too.

这个批处理文件可以提供帮助,但它有一些限制。文件名字符 = 和 % 无法替换(从这里开始),文件名中的 ^ 也可能是一个问题。

In this portion %newname: =_%on every line in the lower block it replaces the character after :with the character after =so as it stands the bunch of characters are going to be replaced with an underscore.

%newname: =_%下部块中每一行的这一部分中,它用后面:的字符替换了后面的字符,=因此就这样,一堆字符将被替换为下划线。

Remove the echoto activate the ren command as it will merely print the commands to the console window until you do.

删除echo以激活 ren 命令,因为它只会将命令打印到控制台窗口,直到您这样做为止。

It will only process the current folder, unless you add /sto the DIR command portion and then it will process all folders under the current one too.

它只会处理当前文件夹,除非您添加/s到 DIR 命令部分,然后它也会处理当前文件夹下的所有文件夹。

To delete a certain character, remove the character from after the = sign. In %newname:z=%an entry like this would remove all z characters (case insensitive).

要删除某个字符,请删除 = 符号后的字符。在这样%newname:z=%的条目中将删除所有 z 字符(不区分大小写)。

@echo off
for /f "delims=" %%a in ('dir /a:-d /o:n /b') do call :next "%%a"
pause
GOTO:EOF
:next
set "newname=%~nx1"

set "newname=%newname: =_%"
set "newname=%newname:)=_%"
set "newname=%newname:(=_%"
set "newname=%newname:&=_%"
set "newname=%newname:^=_%"
set "newname=%newname:$=_%"
set "newname=%newname:#=_%"
set "newname=%newname:@=_%"
set "newname=%newname:!=_%"
set "newname=%newname:-=_%"
set "newname=%newname:+=_%"
set "newname=%newname:}=_%"
set "newname=%newname:{=_%"
set "newname=%newname:]=_%"
set "newname=%newname:[=_%"
set "newname=%newname:;=_%"
set "newname=%newname:'=_%"
set "newname=%newname:`=_%"
set "newname=%newname:,=_%"

echo ren %1 "%newname%

回答by Endoro

I would recommend the renamecommand for this. Type ren /?at the command line for more help.

我会为此推荐rename命令。ren /?在命令行中键入以获取更多帮助。