使用 windows REN 命令从文件名中删除前缀

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

Using windows REN command to remove a prefix from filenames

windowsbatch-filerenamebrackets

提问by mike

I'm trying to come up with a solution for this issue using only windows cmd line if it's possible.

如果可能的话,我正在尝试仅使用 windows cmd 行为这个问题提出解决方案。

I have a series of files that look like the following,

我有一系列如下所示的文件,

[sometexthere233] Tv episode 1  
[sometexthere233] Tv episode 2  
[sometexthere233] Tv episode 3  

I would like to detect any file names in the current directory that contain text within brackets as the prefix, and remove that portion of the file name.

我想检测当前目录中包含括号内文本作为前缀的任何文件名,并删除文件名的那部分。

Tv episode 1  
Tv episode 2  
Tv episode 3  

I've done some research using the windows REN command, but I can seem to approach the right syntax or wild card for it to execute.

我已经使用 windows REN 命令做了一些研究,但我似乎可以找到正确的语法或通配符来执行它。

Any help on how to do this, or to create a bat file that is able to do this would be greatly appreciated.

任何有关如何执行此操作或创建能够执行此操作的 bat 文件的帮助将不胜感激。

回答by Andriy M

The following script searches the current directory for files matching the mask [*] *and renames them by removing the bracketed part and the space after it:

以下脚本在当前目录中搜索与掩码匹配的文件,[*] *并通过删除括号部分及其后的空格来重命名它们:

@ECHO OFF
FOR %%F IN ("[*] *") DO CALL :process "%%F"
GOTO :EOF

:process
SET oldname=%1
SET "newname=%~nx1"
SET "newname=%newname:*] =%"
RENAME %oldname% "%newname%"