Batch file to move files based on name w/o creating new folders

六眼飞鱼酱① 提交于 2019-12-02 10:09:38
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
PUSHD "%sourcedir%"
FOR /f "tokens=1*delims= " %%a IN (
 'dir /b /a-d "* *.pdf" '
 ) DO (
 IF EXIST "..\%%a\." (ECHO(MOVE "%%a %%b" "..\%%a\") ELSE (ECHO(Leave "%%a %%b")
)
popd

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances. Assigning it to %~dp0 is a possibility.

Given (partial dir /a:d of %sourcedir%)

20/01/2015  09:49    <DIR>          one
20/01/2015  09:49    <DIR>          t w o
20/01/2015  09:50    <DIR>          1232
20/01/2015  09:50    <DIR>          1234

and dir of source of pdfs:

Directory of u:\sourcedir\t w o

20/01/2015  09:50                 0 dum myfile2.pdf
20/01/2015  09:50                 0 1231 54321.pdf
20/01/2015  09:50                 0 1232 54321.pdf
20/01/2015  09:50                 0 1233 54321.pdf
20/01/2015  09:50                 0 1234 54321.pdf
20/01/2015  09:50                 0 1235 54321.pdf
               6 File(s)              0 bytes

This yields

Leave "dum myfile2.pdf"
Leave "1231 54321.pdf"
MOVE "1232 54321.pdf" "..\1232\"
Leave "1233 54321.pdf"
MOVE "1234 54321.pdf" "..\1234\"
Leave "1235 54321.pdf"

The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)

The Leave message and its associated else clause would of course be optional.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!