Move files based on last three digits

Microsoft Windows xp professional (upgr...
May 18, 2010 at 11:45:00
Specs: Windows XP
I have about 71,000 all in one subdirectory that I need to move into other subdirectories based on the last three digits of the folder name. For example, a folder named "0-060-21.1 would be moved into a folder named "211" and a folder named 0-336-42.8 would be moved to a folder named "428". Can you help me write a batch script that would accomplish this.

See More: Move files based on last three digits

Report •

#1
May 18, 2010 at 18:59:54
for /f "tokens=1-4 delims=_." %%a in ('dir /b *.*') do (
:: test to see if this looks right
echo md %%c%%d 2>nul
echo copy %%a_%%b_%%c.%%d %%c%%d
pause
)
::------ end
run from the directory that has the files.

Report •

#2
May 19, 2010 at 06:52:26
Thank you
This is the first batch script that I have run so I am a bit unclear
as to how this code reads. The files currently sit in a directory
K:\patients\ and are named something like:
0-060-21.1
0-336-42.8
and I want them to end up like this
M:\Patients\
211\0-060-211
428\0-336-428

Does this script create the subdirectory based on the last three digits? I do have them
all created (000 through 999) already.

I am not sure how to add the directories to this code. Thank you so much for your help.

for /f "tokens=1-4 delims=_." %%a in ('dir /b *.*') do (
:: test to see if this looks right
echo md %%c%%d 2>nul
echo copy %%a_%%b_%%c.%%d %%c%%d
pause
)
::------ end


Report •

#3
May 19, 2010 at 10:31:10
sorry--I replied twice.

Report •

Related Solutions

#4
May 19, 2010 at 12:21:10
This previews what's to be done. To activate, remove ECHO in front of MOVE.

=============================
@echo off & setLocal EnableDELAYedeXpansion

pushd K:\patients
for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set O=%%a
set O=!O:~0,-4!
set N=%%a
set N=!N:.=!
set N=!N:~-3!
echo move %%a M:\Patients\!N!\!O!!N!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#5
May 19, 2010 at 20:14:49
I am sorry to say that this still does not work. I get the error /f"tokens=* delims=" was unexpected at this time. I have tried several different things and cannot get it to successfully move the files. Can anyone please help??? Thank you.

Report •

#6
May 19, 2010 at 20:49:39
you need a space twixt /f and "tokens. change
/f"tokens=* delims="
to:
/f "tokens=* delims="

this holds true for both scripts. either one should work (M2's more likely Lol!)
remove the "echo" when you want the real deal.
ps: if your curious about batch:
set /?
for /?
if /?
setlocal /?
from the command prompt, tells you most of what you need.


Report •

Ask Question