Computing.Net > Forums > Programming > Batch move and rename files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch move and rename files

Reply to Message Icon

Name: maxbre
Date: September 9, 2009 at 03:38:38 Pacific
OS: Windows XP
CPU/Ram: 2 gb
Subcategory: Batch
Comment:

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.txt

output example:
20090101.txt
20090102.txt

20090131.txt
20090201.txt
20090202.txt

20090228.txt

I’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



Sponsored Link
Ads by Google

Response Number 1
Name: gtaion
Date: September 9, 2009 at 06:43:50 Pacific
Reply:

This isn't tested, but I beleive it is close to what you are looking for.

@echo off
setlocal EnableDelayedExpansion

for /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!"
)


0

Response Number 2
Name: Sen Hu
Date: September 9, 2009 at 09:12:12 Pacific
Reply:


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


0

Response Number 3
Name: maxbre
Date: September 10, 2009 at 00:12:57 Pacific
Reply:

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


0

Response Number 4
Name: maxbre
Date: September 10, 2009 at 01:04:33 Pacific
Reply:

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


0

Response Number 5
Name: gtaion
Date: September 10, 2009 at 06:04:30 Pacific
Reply:

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 EnableDelayedExpansion

for /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"
)


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Batch move and rename files

BATCH to copy and rename files www.computing.net/answers/programming/batch-to-copy-and-rename-files/15221.html

Batch file copy and Rename www.computing.net/answers/programming/batch-file-copy-and-rename/18689.html

Copy and Rename Batch File www.computing.net/answers/programming/copy-and-rename-batch-file/15297.html