-->

Batch command to check for partial filename then m

2019-09-05 21:59发布

问题:

I'm in need of a batch command that will look into a directory's sub folders and find a partial file name. If that file exists, then move that directory otherwise leave it alone.

I have a folder full of movies:

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

Within each movie folder I would like to know if it has a trailer downloaded into the folder, if so then move that directory to x:\movies. I say partial file name as every trailer file will include the title of the movie with "-trailer" added to the end (f.e. The Interview (2014)-trailer).

回答1:

Let's look for a solution from Windows CLI (Command Line Interpreter) with -> prompt using An A-Z Index of the Windows CMD command line and with next scenario:

->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

To find all those files under MOVIES subfolder with -trailer added to the filename end preceding . of file extension:

->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

To get folder names only:

->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\

But here the movie2 appears more than once! And, moreover, the move command does not allow source directory with trailing backslash:

->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.

Therefore we need switch from Windows shell (CLI) to batch scripting. Create file 28167824.bat (e.g with notepad and save it to D:\bat\StackOverflow folder):

@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

Run this batch script, the template of next move commands is the same as in that successful move command mentioned above:

->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\"

Bingo! Now we could remove rem before move "%srcfolder%" "d:\test\xMovies\", save the script and run it:

->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.

And here is final configuration, cf. starting scenario:

->tree d:\test\movies
D:\TEST\MOVIES
└───movie1

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