我需要一个批处理命令,它将查看目录的子文件夹并找到部分文件名。如果该文件存在,则移动该目录,否则不管它。
我有一个装满电影的文件夹:
D:\Movies\movie1\
\movie2\
\movie3\
在每个影片文件夹中,我想知道它是否有下载到该文件夹中的预告片,如果是,则将该目录移至x:\movies
。我说部分文件名,因为每个预告片文件都会包含添加到最后的“-trailer
”电影的标题(f.e. The Interview (2014)-trailer
)。
答案 0 :(得分:0)
让我们从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\"
宾果!现在我们可以在rem
之前删除move "%srcfolder%" "d:\test\xMovies\"
,保存脚本并运行它:
->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