[英]remove string from filenames with .bat batch file
I have a folder with thousands of filenames like this.我有一个包含数千个这样的文件名的文件夹。 I need to put them inside folders and remove the useless parts from the filenames.我需要将它们放在文件夹中并从文件名中删除无用的部分。 I need to do this before adding the files inside my XBMC library.我需要在 XBMC 库中添加文件之前执行此操作。
[ www.AnnoyingSpam.com ] Some.File.Name.With.A.Very.Long.String.avi
[ www.AnnoyingSpam.com ] Another.File.With.A.Very.Long.String.mpg
[ www.AnnoyingSpam.com ] Again.And.Again.mp4
So far all I got is a script that will create folder and move files.到目前为止,我得到的只是一个可以创建文件夹和移动文件的脚本。 But it adds the tag "Folder" to each folder and it doesn't remove the AnnoyingSpam.com tag但它会向每个文件夹添加标签“文件夹”,并且不会删除 AnnoyingSpam.com 标签
@echo off
for /f "usebackq delims=?" %%a in (`dir /a-d /b`) do (
if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
)
goto :EOF
:func
set file=%~1
set dir=%file% Folder
md "%dir%" Folder 2>nul
move "%file%" "%dir%"
goto :EOF
So my question is how can i remove these strings from the folder names that are being created in the above script:所以我的问题是如何从上述脚本中创建的文件夹名称中删除这些字符串:
"[ www.AnnoyingSpam.com ]" “[ www.AnnoyingSpam.com ]”
" Folder" “ 文件夹”
Since you are performing the check if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
, my understanding is that you will be running the batch from inside the 'target' folder.由于您正在执行检查if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
,我的理解是您将从“目标”文件夹中运行批处理。
What follows is a batch
script, based structurally on your sample, that accomplishes the necessary filtering and handles the rename and move operations.下面是一个batch
脚本,在结构上基于您的示例,它完成必要的过滤并处理重命名和移动操作。
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "usebackq delims=?" %%a in (`dir /a-d /b`) do (
if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
)
endlocal
echo Done.
goto EOF
:func
set file=%~1
for /F "delims=" %%a in ("!file!") do (
rem stripped extension, next step is to filter to remove the 'spam' part
set "c=%%~Na"
rem original filename, will be needed for the renaming
set "o=%%~a"
rem store the extension to use later on when re-constructing the filename
set "e=%%~xa"
rem this is effectively a multichar stripper; filters the 'spam' part out
set "c=!c:[ www.AnnoyingSpam.com ] =!"
rem reconstruct the filename
set "f=!c!!e!"
rem echo !o! : !c! : !e! ::: !f!
rem rename original file to the filtered version
ren "!o!" "!f!"
rem create dir named after the filtered filename, if it does not already exist; just a precaution
if not exist !c! (md !c!)
rem move the filtered file to the appropriate dir
move "!f!" "!c!" > nul
)
:EOF
The script is almost self-explanatory (see the comments inside), however there are some aspects that I would like to point out:该脚本几乎是不言自明的(请参阅里面的评论),但是我想指出一些方面:
The instructions setlocal ENABLEDELAYEDEXPANSION
and endlocal
, that in fact form a wrap around the for-loop
, establish that the variables inside the loop will be evaluated (expanded) at the execution time and not during parse time.指令setlocal ENABLEDELAYEDEXPANSION
和endlocal
实际上形成了for-loop
,确定循环内的变量将在执行时而不是在解析时被评估(扩展)。 Notice that the variables are referenced inside the loop as !var!
请注意,变量在循环内被引用为!var!
. . See more on this here .在此处查看更多信息。
The removal of the unwanted 'spam' part is performed by this command: set "c=!c:[ www.AnnoyingSpam.com ] =!"
删除不需要的“垃圾邮件”部分由以下命令执行: set "c=!c:[ www.AnnoyingSpam.com ] =!"
. . What it does is best described by the following example: set "var=!var:substring1=substring2!"
下面的例子最好地描述了它的作用: set "var=!var:substring1=substring2!"
, has as a result the substitution of every occurrence of substring1 by substring2 in var .具有作为结果通过在VAR substring2 substring1的每次出现的取代。 If substring2 is null
(as is the case here), then the actual effect is the removal of every substring1 pattern found in the original variable.如果 substring2 为null
(就像这里的情况),那么实际效果是删除在原始变量中找到的每个substring1模式。 More on this here , and for an interesting use as a multichar delimiter see dbenham's answer here .更多关于此这里,并作为multichar定界符看到dbenham的回答一个有趣的应用在这里。
And, as always, test thoroughly before you use the script on the actual folders.而且,与往常一样,在实际文件夹上使用脚本之前,请进行彻底测试。
I know you've asked for a batch script, but if you have php installed, you can use this:我知道您要求提供批处理脚本,但是如果您安装了 php,则可以使用以下命令:
renameSpam.php重命名垃圾邮件.php
<?php
$removeTag = "[ www.AnnoyingSpam.com ]";
foreach(glob("*.*") as $file){
if (strpos($file, $removeTag) !== false) {
$newFile = trim(str_ireplace($removeTag, "", $file));
$ext = pathinfo($newFile, PATHINFO_EXTENSION);
$fnNoExt = basename($newFile,".".$ext);
if(!is_dir($fnNoExt)){
mkdir($fnNoExt, 0777);
rename($file, "./$fnNoExt/$newFile");
}else{
rename($file, "./$fnNoExt/$newFile");
}
}
}
Place renameSpam.php
on the directory with the files to rename and execute as php renameSpam.php
.将renameSpam.php
放在包含要重命名的文件的目录中,并作为php renameSpam.php
执行。
I've tested this on windows and linux and it works as intended.我已经在 windows 和 linux 上测试过了,它按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.