我有一堆文件,我想根据前 11 个字符将它们组织到文件夹中(例如X-01234-567
),并且有些文件在这些数字后面有描述,有助于识别它们是什么。
我已经能够创建仅包含这 11 个字符的文件夹,然后将所有以相应 11 个字符开头的文件移动到这些文件夹中。
我遇到的问题是,已经有很多文件夹包含超过这 11 个字符(例如X-09902-024 Adapter, 0.38 NPT
)等等。
我希望批处理文件查找已经具有与该部分相同的前 11 个字符的文件夹,然后如果匹配则将该部分移动到该文件夹中,否则如果没有文件夹,则创建一个。附件是我尝试过的不同方法的屏幕截图。我假设它非常简单,但我无法弄清楚语法。
我不知道我还不能添加图像,所以这里是我尝试过的两个不同批处理文件的副本。提前感谢您的帮助。
@echo off
setlocal
set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename:~0,11!"
if not exist "!filename:~0,11!" md "!basename!"
)
move "!filename!.%%b" "!basename!"
for /F "delims=" %%c in ("!basename!") do (
endlocal
set "basename=%%c
)
)
我尝试的第二个是在这里:
@echo off
for /f "delims=" %%F in (
'Dir /b *.dwg *.jpg *.pdf *.slddrw *.sldprt *.sldasm *.STEP^|findstr "^[X]-[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]" '
) do call :subr "%%F"
exit /b
:subr
set "file=%~n1"
set "fold=%file:~0,11%\"
if not exist "%fold%*" md "%fold%"
move %1 "%fold%"
我附上的图片显示了如果文件夹是由脚本创建的,文件将如何进入该文件夹,但是有一个文件夹同名,只是字符更多,并且文件夹是空的。如果您需要我澄清任何事情,请告诉我!
编辑:
我使用并修改了 LotPings 脚本来获得我想要的东西。下面是它的副本,供参考。
@echo off & setlocal EnableDelayedExpansion
set "basedir=F:\Kits Fixed\Test"
PushD "%baseDir%" || (echo couldn't locate baseDir & pause & exit /B 1)
:: iterate all dirs and move files with same prefix to dir
for /F "delims=" %%A in ('dir /B /AD ^| sort /R') do (
set "file=%%A"
set "folder=!file:~0,11!"
Move "!folder!"*.* "%%A"
)
:: there shouldn't be any files left with matching dir
for /F "delims=" %%G in ('dir /B *.dwg *.jpg *.pdf *.slddrw *.sldprt *.sldasm *.STEP *.x_t *.IGS *.DXF *.STP *.rpt *.err *.txt') do (
set "files=%%G"
set "folders=!files:~0,11!"
if not exist "!folders!" md "!folders!"
Move "%%G" "!folders!"
)
Pause
PopD
答案1
更有意义的是:
- 迭代当前文件夹(按相反顺序排列,以便先列出较长的名称)
- 并使用名称作为前缀来移动带有附加通配符的文件
*.*
这批
:: Q:\Test\2019\04\22\SU_1428072.cmd
@echo off & setlocal EnableDelayedExpansion
set "basedir=X:\Path\to\files-n-folders"
PushD "%baseDir%" || (echo couldn't locate baseDir & pause & exit /B 1)
:: iterate all dirs and move files with same prefix to dir
for /F "delims=" %%a in ('dir /B /AD ^| sort /R') do Move "%%a*.*" "%%a"
:: there shouldn't be any files left with matching dir
for /F "delims=" %%a in ('dir /B /A-D') do (
set "file=%%a"
set "folder=!file:~0,11!"
if not exist "!folder!" md "!folder!"
Move "%%a" "!folder!"
)
PopD
转换这棵初始树
> tree /F
│ X-01234-567.dwg
│ X-01234-567.pdf
│ X-09902-024 (rev B).dwg
│ X-09902-024 (rev B).pdf
│ X-09902-024 Adapter, 0.38 NPT to Engine Block.dwg
│ X-09902-024 Adapter, 0.38 NPT to Engine Block.jpg
│ X-09902-024 Adapter, 0.38 NPT to Engine Block.pdf
│ X-09902-024 Adapter, 0.38 NPT to Engine Block.sldasm
│ X-09902-024 Adapter, 0.38 NPT to Engine Block.slddrw
│ X-09902-024 Adapter, 0.38 NPT to Engine Block.sldprt
│ X-09902-024 Rev A Adapter, 0.38 NPT to Engine Block.dwg
│ X-09902-024 Rev A Adapter, 0.38 NPT to Engine Block.pdf
│
├───X-01234-567
└───X-09902-024 Adapter, 0.38 NPT
进入这个结果
> tree /f
├───X-01234-567
│ X-01234-567.dwg
│ X-01234-567.pdf
│
├───X-09902-024
│ X-09902-024 (rev B).dwg
│ X-09902-024 (rev B).pdf
│ X-09902-024 Rev A Adapter, 0.38 NPT to Engine Block.dwg
│ X-09902-024 Rev A Adapter, 0.38 NPT to Engine Block.pdf
│
└───X-09902-024 Adapter, 0.38 NPT
X-09902-024 Adapter, 0.38 NPT to Engine Block.dwg
X-09902-024 Adapter, 0.38 NPT to Engine Block.jpg
X-09902-024 Adapter, 0.38 NPT to Engine Block.pdf
X-09902-024 Adapter, 0.38 NPT to Engine Block.sldasm
X-09902-024 Adapter, 0.38 NPT to Engine Block.slddrw
X-09902-024 Adapter, 0.38 NPT to Engine Block.sldprt