且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.BAT文件按文件名将文件移动到文件夹和子文件夹中...无法使子文件夹正常工作

更新时间:2022-11-28 19:32:54

此方法更简单:

setlocal

for /F "tokens=1,2* delims=_" %%a in ('dir /B *_*_*.pdf *_*_*.png') do (
   if not exist "%%a" md "%%a"
   if not exist "%%a\%%b" md "%%a\%%b"
   move "%%a_%%b_%%c" "%%a\%%b\%%c"
)

即:dir /B *_*_*.pdf命令产生具有PREFIX_PREFIX2_filename.pdf格式的名称列表. for /F "tokens=1,2* delims=_" %%a通过以下方式将此类名称分为3个标记:%%a=PREFIX%%b=PREFIX2%%c=filename.pdf.其余的很明显...

That is: dir /B *_*_*.pdf command produce a list of names with PREFIX_PREFIX2_filename.pdf format. for /F "tokens=1,2* delims=_" %%a divide such names in 3 tokens this way: %%a=PREFIX, %%b=PREFIX2 and %%c=filename.pdf. The rest is obvious...

PS-我建议您将标题中的sort单词更改为move.第一项提出了完全不同的任务的想法...

PS - I suggest you to change the sort word in your title to move. The first term gives the idea of an entirely different task...