Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have many files stored in the following structure aaaamm\mmgg.txt where aaaa is year, mm is month, gg is day
I need to move the files in the same current dir and also rename them all as aaaammgg.txt
input example:200901\0101.txt
200901\0102.txt
…
200901\0131.txt
200902\0201.txt
200902\0202.txt
…
200902\0228.txtoutput example:
20090101.txt
20090102.txt
…
20090131.txt
20090201.txt
20090202.txt
…
20090228.txtI’ve worked out this but it’s obviously not correct because of the wrong nesting of the two for cycles…
@echo off & setLocal EnableDelayedExpansion
for /d %%a in (*) do (
set aaaamm=%%a
set aaaa=!aaaamm:~0,4!
for /f "tokens=* delims= " %%b in ('dir/s/b/a-d *.gz') do (
move "%%b" !aaaa!%%~nb.gz
)
rd %%a\ /s/q
)any help for this?
thanks

This isn't tested, but I beleive it is close to what you are looking for.
@echo off
setlocal EnableDelayedExpansionfor /f "tokens=*" %%A in ('dir /a:-d /b /s "*.gz"') do (
pushd %%~dpA
for %%b in (.) do (
set dirname=%%~dpnb
set aaaamm=%%~nb
set aaaa=!aaaamm:~0,4!
popd
move "%%A" !aaaa!%%~nA.gz
)
rmdir /s /q "!dirname!"
)

I will assume the aaaamm folders are under "C:/files". You want to move files to C:/movedfiles. Here is a script.# Script moverename.txt # SET THE FOLLOWING TO CORRECT PATHS. var str source, dest set $source = "C:/files" set $dest = "C:/movedfiles" # Go to top directory. cd $source # Collect a list of all .txt files in subfolders. var str list ; lf -r -n "*.txt" > $list # Go thru files one by one. while ($list <> "") do # Get the next file. var str file ; lex "1" $list > $file # Extract the file name after the last /. var str name ; stex -p "^/^l[" $file > $name stex "[^.^l" $name > null # $name now has mmgg. # Extract the subfolder name aaaamm. We just # want the year aaaa. var str year ; stex "[^/^l" $copyfile > null stex "^/^l[" $copyfile > $year ; chex "4[" $year > null # $year now have aaaa. # We now want to move $file # to $dest/$year$name.txt. system move ("\"+$file+"\") ("\""+$dest+"/"+$year+$name+".txt"+"\"") done
I have not tested it. Test first. Script is in biterscripting ( http://www.biterscripting.com ). To test, save the script as C:/Scripts/moverename.txt, start biterscripting, enter the following command.script "C:/Scripts/moverename.txt"
Make sure you set $source and $dest to correct folder paths. Use double quotes around paths.Sen

sorry but
- gtaion's batch is not working correctly (it's deleting everything: files and dirs); I'll try to spot the bug...
- sen's reply altought quite interesting is not suitable for my needs because I can not install third party utilities on the machine I'm working on
I have to stay necessarly on pure batch scripting
thanks anyway

here is the revised version of my original batch
@echo off & setLocal EnableDelayedExpansion for /d %%a in (*) do ( set aaaamm=%%a set aaaa=!aaaamm:~0,4! goto :here ) :here for /f "tokens=* delims= " %%b in ('dir/s/b/a-d *.gz') do ( move "%%b" !aaaa!%%~nb.gz )it semms working fine!
now I just need to insert somewhere the deletion of empty dir...
I'm wondering if there is a better solution than my very rough approach

It has got to be where the rmdir command is, it should probably moved out of the For loop and put at the end of the batch, maybe with another for loop, that goes through stating if the directory is empty to delete it.
Again this is not tested, I'm out of the office this week and don't have the resources to test this.@echo off
setlocal EnableDelayedExpansionfor /f "tokens=*" %%A in ('dir /a:-d /b /s "*.gz"') do (
pushd %%~dpA
for %%b in (.) do (
set aaaamm=%%~nb
popd
move "%%A" !aaaamm:~0,4!%%~nA.gz
))
for /f "usebackq" %%d in (`"dir /ad/b/s | sort /R"`) do (
rd "%%d"
)

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |