Batch folder by file name and move

I found the message magoo and played with it. I can't seem to get the DIR part to parse the file name in order to create a folder and move the files to the appropriate 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 package should create folders 800 and 801 and move the files 800.X and 801.X, respectively. I tried FINDSTR and other masks and did not have much luck.

Here is magoo source package code (source: http://bit.ly/1ua8IIF ):

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

My attempt in a few hours:

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

I still play with him, but any help would be greatly appreciated!

+1
windows batch-file findstr folders dir
source share
1 answer

The modifications you need to perform are as follows:

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

Adding delims=. means treat as separator. The original used the default delimiter Space .

  'dir /b /ad "*.*.gif" "*.*.jpg"' 

Follow the list of directories in files matching *.*.gif or *.*.jpg

Quotation marks are not needed, but harmless. In the original, for a file mask, space must be included in the mask line; space is a separator and quoting a line removes any special meaning.

This will match any file matching either the extension. If you want to match any file with a *.*.* Pattern, you can change it to

  'dir /b /ad *.*.*' 

finally,

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

It is just a matter of restoring the original row by reinserting . processed by for - in this case . but in the original Space

+1
source share

All Articles