使用.bat批处理文件从文件名中删除字符串

时间:2015-09-30 03:58:12

标签: regex windows batch-file cmd batch-processing

我有一个包含数千个这样的文件名的文件夹。我需要将它们放在文件夹中,并从文件名中删除无用的部分。我需要在我的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
  • 首先,我想删除文件中的AnnoyingSpam.com标签
  • 然后,根据文件名创建一个没有标签且没有扩展名
  • 的文件夹
  • 最后,将文件移至新文件夹
  • 对批处理文件的根目录中的其余文件重复

到目前为止,我得到的是一个可以创建文件夹和移动文件的脚本。但是它会在每个文件夹中添加标签“Folder”,并且不会删除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

所以我的问题是如何从上述脚本中创建的文件夹名称中删除这些字符串:

“[www.AnnoyingSpam.com]”

“文件夹”

2 个答案:

答案 0 :(得分:2)

由于您正在执行检查if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a",我的理解是您将从'target'文件夹中运行批处理。

以下是基于样本的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

脚本几乎是不言自明的(参见里面的评论),但是我想指出一些方面:

  • 实际上形成setlocal ENABLEDELAYEDEXPANSION环绕的指令endlocalfor-loop确定循环中的变量将在执行时被评估(扩展)时间而不是解析时间。请注意,变量在循环内引用为!var!。有关此here的更多信息,请参阅。

  • 此命令执行删除不需要的'垃圾邮件'部分:set "c=!c:[ www.AnnoyingSpam.com ] =!"。下面的示例最好地描述了它的作用:set "var=!var:substring1=substring2!"因此在 var <中用 substring2 替换每次出现的 substring1 / em>的。 如果substring2为null(如此处所示),则实际效果是删除原始变量中找到的每个 substring1 模式。有关此here的更多信息,以及作为multichar分隔符的有趣用法,请参阅 dbenham的答案here

并且,在使用实际文件夹上的脚本之前,一如既往地进行彻底测试。

答案 1 :(得分:0)

我知道你已经要求批处理脚本,但是如果安装了php,你可以使用它:

<强> renameSpam.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");
        }

    }
}

renameSpam.php放在包含要重命名的文件的目录上,然后执行php renameSpam.php 我已经在Windows和Linux上测试了它,它按预期工作。