替换或删除文件夹中所有文件的文件名中的某些字符 [英] Replace or delete certain characters from filenames of all files in a folder
问题描述
如何通过一些批处理文件执行删除某些字符或将某些字符替换为其他字符,一次性删除Windows文件夹中所有文件的文件名,是否有DOS命令?
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?
推荐答案
使用 PowerShell 为 DOS 提示符执行更智能的操作.在这里,我展示了如何批量重命名当前目录中包含空格的所有文件和目录,方法是将它们替换为 _
下划线.
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.
Dir |
Rename-Item -NewName { $_.Name -replace " ","_" }
可选地,Where-Object
命令可用于过滤连续cmdlet(命令-让).以下是一些示例来说明它可以为您提供的灵活性:
EDIT :
Optionally, the Where-Object
command can be used to filter out ineligible objects for the successive cmdlet (command-let). The following are some examples to illustrate the flexibility it can afford you:
跳过任何文档文件
To skip any document files
Dir |
Where-Object { $_.Name -notmatch ".(doc|xls|ppt)x?$" } |
Rename-Item -NewName { $_.Name -replace " ","_" }
仅处理目录(3.0 之前的版本)
To process only directories (pre-3.0 version)
Dir |
Where-Object { $_.Mode -match "^d" } |
Rename-Item -NewName { $_.Name -replace " ","_" }
PowerShell v3.0 引入了新的 Dir
标志.您也可以在那里使用 Dir -Directory
.
PowerShell v3.0 introduced new Dir
flags. You can also use Dir -Directory
there.
跳过任何已经包含下划线(或其他一些字符)的文件
To skip any files already containing an underscore (or some other character)
Dir |
Where-Object { -not $_.Name.Contains("_") } |
Rename-Item -NewName { $_.Name -replace " ","_" }
这篇关于替换或删除文件夹中所有文件的文件名中的某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!