batch file to copy and (a tricky) rename

October 6, 2010 at 12:51:32
Specs: Windows 2000
Hello, newbie here, I've gone through several threads and haven't found my exact scenario, could be I'm not versed well enough or it might not be possible to do in batch.

What I am wanting to do is to schedule a nightly file copy from one server to another. Simple enough to do in Windows Scheduler with a date parm but on the destination the file has to be renamed as such.

Source File name:
(it will always start with "A" and then have YYMMDD then a 6 digit number sequence and a .OCC suffix

example: A101005150132.OCC

Destination File name:
First a B is added after the A at the front of the file, then the YYMMDD then the suffix ## would be the last two digits in the ## sequence in the Source File. The .OCC is dropped as well as the first 4 digits of the sequence
in this example the file would be named: AB101005.32

Two more examples:
A101005140931.OCC -> AB101005.31
A100930105370.OCC -> AB100930.70

It would have about 2-3 files to copy each night at midnight.

any thoughts or tips? THANKS!!

See More: batch file to copy and (a tricky) rename

Report •

October 6, 2010 at 13:32:54
I think I've solved your problem:

setLocal EnableDelayedExpansion
set loc=%CD%
for %%F in (A*.OCC) do set renamename=%%F
set ending=!renamename:~11,13!
set ending=!ending:~0,2!
set yymmdd=!renamename:~1,8!
ren "A!yymmdd!*.OCC" "AB%yymmdd%.%ending%"
if exist "*.OCC" (GOTO loop) ELSE (exit)

Tell me if it works or not.

Report •

October 6, 2010 at 18:22:45
download gawk for windows then do this

C:\test>dir /A-d /B A*OCC

C:\test>dir /A-d /B A*OCC | gawk "{o=$0;sub(/^A/,\"AB\",o);o=substr($0,1,7) \".OCC\";print \"ren \"$0\" \"o }"
ren A100930105370.OCC A100930.OCC
ren A101005140931.OCC A101005.OCC

C:\test>dir /A-d /B A*OCC | gawk "{o=$0;sub(/^A/,\"AB\",o);o=substr($0,1,7) \".OCC\";cmd=\"ren \"$0\" \"o;system(cmd) }"

C:\test>dir /A-D /B A*OCC

GNU win32 packages | Gawk

Report •

October 7, 2010 at 06:48:08
jahildebra, Thanks for the quick response. I've played around with it and it does rename when working with one file. There was one slight change to get the exact format I needed:
set yymmdd=!renamename:~1,6!

I'm having a little trouble when doing more than one file, it just keeps looping over and over and complaining about a duplicate file name:
C:\1>ren "A!yymmdd!*.OCC" "AB100930.70"
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
But this may be my problem due to where I have your loop nested in my batchfile, I'll have to look closer at it my logic and when to go into this loop when doing multiple file copies. I'm pretty new at this so it's not obvious to me.

ghostdog - thank you for your response as well. I take a look at gawk as an option.

This site is great. Great contributors - thanks so much.

Report •
Related Solutions

Ask Question