【问题标题】:Batch command to check for partial filename then move批处理命令检查部分文件名然后移动
【发布时间】:2015-01-27 10:08:48
【问题描述】:

我需要一个批处理命令来查看目录的子文件夹并找到部分文件名。如果该文件存在,则移动该目录,否则不要理会它。

我有一个装满电影的文件夹:

D:\Movies\movie1\
         \movie2\
         \movie3\

在每个电影文件夹中,我想知道它是否有一个预告片下载到该文件夹​​中,如果有,则将该目录移动到x:\movies。我说的是部分文件名,因为每个预告片文件都将包含电影的标题,并在末尾添加“-trailer”(例如The Interview (2014)-trailer)。

【问题讨论】:

    标签: batch-processing xbmc kodi


    【解决方案1】:

    让我们从带有-> 提示符的Windows CLI(命令行解释器)中寻找解决方案,使用An A-Z Index of the Windows CMD command line 和下一个场景:

    ->tree d:\test\movies
    D:\TEST\MOVIES
    ├───movie1
    ├───movie2
    ├───movie3
    └───movie4
    
    ->tree d:\test\xmovies
    D:\TEST\XMOVIES
    No subfolders exist
    
    ->dir /B /S /A-D "d:\test\movies"
    d:\test\movies\movie1\The View (2014)-trailer missing.rtf
    d:\test\movies\movie2\The Interview (2014)-trailer.bat
    d:\test\movies\movie2\The Interview (2014)-trailer.bmp
    d:\test\movies\movie2\The Interview (2014)-trailer.txt
    d:\test\movies\movie3\An Interview (2014)-trailer.bmp
    

    要查找MOVIES 子文件夹下的所有文件,并将-trailer 添加到文件扩展名. 之前的文件名末尾:

    ->dir /B /S /A-D "d:\test\movies\*-trailer.*"
    d:\test\movies\movie2\The Interview (2014)-trailer.bat
    d:\test\movies\movie2\The Interview (2014)-trailer.bmp
    d:\test\movies\movie2\The Interview (2014)-trailer.txt
    d:\test\movies\movie3\An Interview (2014)-trailer.bmp
    

    仅获取文件夹名称:

    ->for /F "tokens=*" %G in ('dir /B /S /A-D "d:\test\movies\*-trailer.*"') do @echo %~dpG
    d:\test\Movies\movie2\
    d:\test\Movies\movie2\
    d:\test\Movies\movie2\
    d:\test\Movies\movie3\
    

    但这里movie2 不止一次出现!而且,move 命令不允许源目录带有反斜杠:

    ->move "D:\test\Movies\movie4\" "D:\test\xMovies\"
    The system cannot find the file specified.
    
    ->move "D:\test\Movies\movie4" "D:\test\xMovies\"
            1 dir(s) moved.
    

    因此,我们需要从 Windows shell (CLI) 切换到批处理脚本。创建文件28167824.bat(例如使用notepad并将其保存到D:\bat\StackOverflow文件夹):

    @ECHO OFF >NUL
    @SETLOCAL enableextensions disabledelayedexpansion
    for /F "tokens=*" %%G in (
      'dir /B /S /A-D "d:\test\movies\*-trailer.*"'
      ) do (
        set "rawfolder=%%~dpG"
        if exist "%%~dpG" call :moveDir
      )
    :endlocal
    @ENDLOCAL
    goto :eof
    
    :moveDir
      set "srcfolder=%rawfolder:~0,-1%"
      `enter code here`echo move "%srcfolder%" "d:\test\xMovies\"
      rem move "%srcfolder%" "d:\test\xMovies\"
    goto :eof
    

    运行这个批处理脚本,下一个move命令的模板和上面提到的那个成功的move命令的模板一样:

    ->D:\bat\StackOverflow\28167824
    move "d:\test\Movies\movie2" "d:\test\xMovies\"
    move "d:\test\Movies\movie2" "d:\test\xMovies\"
    move "d:\test\Movies\movie2" "d:\test\xMovies\"
    move "d:\test\Movies\movie3" "d:\test\xMovies\"
    

    宾果!现在我们可以删除move "%srcfolder%" "d:\test\xMovies\"之前的rem,保存脚本并运行它:

    ->D:\bat\StackOverflow\28167824
    move "d:\test\Movies\movie2" "d:\test\xMovies\"
            1 dir(s) moved.
    move "d:\test\Movies\movie3" "d:\test\xMovies\"
            1 dir(s) moved.
    

    这是最终配置,参见。起始场景:

    ->tree d:\test\movies
    D:\TEST\MOVIES
    └───movie1
    
    ->tree d:\test\xmovies
    D:\TEST\XMOVIES
    ├───movie2
    ├───movie3
    └───movie4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-13
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2017-06-07
      • 1970-01-01
      相关资源
      最近更新 更多