批处理命令检查部分文件名然后移动 [英] Batch command to check for partial filename then move
问题描述
我需要一个批处理命令,该命令将查找目录的子文件夹并找到部分文件名.如果该文件存在,则移动该目录,否则将其保留.
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\
在每个电影文件夹中,我想知道是否有预告片下载到该文件夹中,如果是,则将该目录移动到x:\movies
.我说的是部分文件名,因为每个预告片文件都将包含电影的标题,并在结尾加上"-trailer
"(例如The Interview (2014)-trailer
).
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
).
推荐答案
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
要查找MOVIES
子文件夹下的所有那些文件,并在文件扩展名.
之前的文件名末尾添加-trailer
:
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
仅获取文件夹名称:
->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
命令不允许源目录带有反斜杠:
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.
因此,我们需要从Windows Shell(CLI)切换到批处理脚本.创建文件28167824.bat
(例如,使用notepad
并将其保存到D:\bat\StackOverflow
文件夹中):
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
运行此批处理脚本,下一个move
命令的模板与上述成功的move
命令的模板相同:
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\"
宾果!现在我们可以在move "%srcfolder%" "d:\test\xMovies\"
之前删除rem
,保存脚本并运行它:
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
这篇关于批处理命令检查部分文件名然后移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!