batch copy on file name string

September 8, 2009 at 07:08:03
Specs: Windows XP, 2 gb
I have a bunch of txt files (as many as some thousands) with the following name structure “0mtpaaaammgg.txt”(fixed 12 characters long) where “aaaa” is year and “mm” is month

I need to create some dir “aaaa” together with related subdir ”mm” and put all “correct” files in them

Example

Input:
0mtp20090908.txt

Output:
2009/09/0mtp20090908.txt

...and so on. I hope it’s enough clear what I mean

So far I’ve worked out without any success something like this…


@echo off
setLocal EnableDelayedExpansion

for %%a in (*.txt) do (
set aaaa=%%%~na:~4,4%
set mm=%%%~na:~8,2%

set dest=%%aaaa%/%mm%%

mkdir %%dest
copy %%a %%dest
)

Any help?

See More: batch copy on file name string

Report •


#1
September 8, 2009 at 07:40:48
This seems to work:

@echo off
setLocal EnableDelayedExpansion

for %%a in (*.txt) do (
set aaaa=%%~na
set mm=%%~na
set bbbb=!aaaa:~4,4!
set nn=!mm:~8,2!

set dest="!bbbb!\!nn!"

mkdir !dest!
copy %%a !dest!
)

You have to put the variable to be updated in each loop between two !, ie !<variable here>!

Gaetano


Report •

#2
September 8, 2009 at 07:59:39
Gaetano's script a bit revised...

@echo off & setlocal EnableDelayedExpansion
echo.  Copy in progress, please wait...
for %%a in (*.txt) do (
  set name=%%~na
  set aaaa=!name:~4,4!
  set mm=!name:~8,2!
  set dest=\!aaaa!\!mm!
  if not exist !dest! md !dest!
  copy %%a !dest! > nul
)
echo.  DONE 
:: End_Of_Batch


Report •

#3
September 8, 2009 at 08:20:49
I see, as usual I'm messing up with the declaration of dynamic variables (among many other things!)
thank you all for the help
max

Report •

Related Solutions


Ask Question