Batch Created Folder By Filename Part and Move

╄→гoц情女王★ 提交于 2019-12-24 11:20:03

问题


I found magoo's post and been playing around with it. I can't seem to get the DIR part to parse out the file name to create the folder and move the files to the respective folders. The following are examples of the files I'm working with:

...
800.1.gif
800.2.gif
800.3.jpg
801.1.gif
801.2.jpg
801.3.gif
...

The batch should create folders 800 and 801 and move the 800.X and 801.X files respectively. I've tried FINDSTR and other masks and not having much luck.

Here's magoo's original batch code (source: http://bit.ly/1ua8IIF):

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
 'dir /b /a-d "*_*_*-*-* *.*"'
 ) DO (  
 ECHO MD %%a
 ECHO MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF

My attempt after a few hours:

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
 'dir /b /a-d ^|findstr /r "\.[1-9]"'
 ) DO (  
 ECHO MD %%a
 ECHO MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF

I'm still playing around with it but any help would be greatly appreciated it!


回答1:


The modifications you'd need to make are:

FOR /f "tokens=1*delims=." %%a IN (

Adding the delims=. means "treat . as a delimiter." In the original, the default delimiter Space was used.

 'dir /b /a-d "*.*.gif" "*.*.jpg"'

Perform the directory list on files matching *.*.gif or *.*.jpg

The quotes are unnecessary but harmless. In the original, the filemask needed to have the space included in the mask string; space is a separator and quoting the string removes the special meaning.

This would match any file matching either extension. If you want to match any file of the pattern *.*.* then feel free to change this to

 'dir /b /a-d *.*.*'

finally,

 ECHO MOVE "%%a.%%b" .\%%a\

It's simply a matter of rebuilding the original string by re-inserting the . parsed out by the for - in this case . but in the original Space



来源:https://stackoverflow.com/questions/27362450/batch-created-folder-by-filename-part-and-move

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