Batch Script Move Files by Filename

Microsoft Windows xp professional w/serv...
May 14, 2010 at 09:25:37
Specs: Windows XP, 3.2/4GB
I am trying to move files by filename into an organized folder structure by year. The file names begin with the year and are followed by an auto incrementing number. i.e. 201000001.tif, 201000002.tif. I would like to have a batch script that moves the files to \\server\share\year, with the year being defined in the first four characters of the filename.

If I had a delimiter after the year, I could write it as follows:
For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d *.tif') Do (
IF not Exist \\Server\Share\%%A MD \\Server\Share\%%A
Move /Y %%A*.tif \\Server\Share\%%A

I don't know how to write it without the delimiter. Thank you for your help.

See More: Batch Script Move Files by Filename

Report •

May 14, 2010 at 13:09:48
@echo off & setlocal enabledelayedexpansion
For /F "tokens=*" %%A in ('dir /b /a-d *.tif') Do (
set yr=%%a
::this gets a substring like you want, 1st 4 bytes:
set yr=!yr:~0,4!
md !yr! 2>nul
Move /Y %%A \\Server\Share\!yr!

oops! thanks, but suprised it worked. You must have caught my mistake
(%%a vs %%A). good eye!

Report •

May 14, 2010 at 13:50:05
Exactly what I was looking for. Thanks for the help.

Report •
Related Solutions

Ask Question