且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从多个文件中删除前缀?

更新时间:2023-12-04 20:58:16

 @ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=1*delims=]" %%a IN (
 'dir /b /a-d "%sourcedir%\*" '
 ) DO IF "%%b" neq "" (
 FOR /f "tokens=*" %%h IN ("%%b") DO ECHO(REN "%sourcedir%\%%a]%%b" "%%h"
)

GOTO :EOF
 

您需要更改sourcedir的设置以适合您的情况.

所需的REN命令仅出于测试目的而ECHO. 确认命令正确无误后,将ECHO(REN更改为REN以实际重命名文件.

/b基本模式/a-d在没有目录的情况下对源目录执行目录扫描,并标记找到的每个文件名-第一个]%%a之前的部分,其余部分到%%b./p>

如果%%b不为空(即不包含]),则不执行任何操作,然后使用默认令牌集(包括空格)和tokens=0将前导空格从%%b剥离为%%h,然后构建原始文件名并重命名.

I downloaded a lot of videos that are named like [site.com] filename.mp4 and I wanted to remove the prefix so that they are named like filename.mp4.

I tried a batch file with the following code:

ren "[site.com] *.mp4" "///////////*.mp4"

But the result was .com] filename.mp4 and can't rename anything beyond the dot, any ideas?

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=1*delims=]" %%a IN (
 'dir /b /a-d "%sourcedir%\*" '
 ) DO IF "%%b" neq "" (
 FOR /f "tokens=*" %%h IN ("%%b") DO ECHO(REN "%sourcedir%\%%a]%%b" "%%h"
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

Perform a directory scan of the source directory, in /b basic mode /a-d without directories and tokenise each filename found - the part before the first ] to %%a and the remainder to %%b.

If %%b is not empty (ie. did not contain ]) then do nothing, therwise use the default token set (which includes space) and tokens=0 to strip the leading spaces from %%b into %%h, then build the original filename and rename.