1.

Solve : 2 part Batch Renaming Help.?

Answer»

Two parts:

1st- I would simply like to add text from a variable to file names. the PROBLEM that I am getting is that the new text replaces part of the old text. here is the example

The old file name : AlarmLog_001.log

Batch File Command = rename f:\%sta%_Dump_%UNIQUE%\ACTLOG\*.* %sta%_*.*

The Variable %Sta% = WT01_MCC

The result that I get is : WT01_MCC_001.log

The result that I WANT is: WT01_MCC_AlarmLog_001.log

Part 2 - within a text file there is a date stamp that I would like to use in a filename. I would like the batch file to open the file (there will be more than one in the directory), find the date stamp (always in the same place) convert the date stamp into a different format and then rename the file with that text. Here is the example:

The File name is startup_alarmlog_01.log

First two lines of the text file:
INLS_LOS Version 0.38
Fri 31-Mar-2006 14:08:10 System 1 Startup

The output that I need is 060331_140810_Alarmlog.log

Can you do that?? Super THANKS in Advanced.Hello,

I. Rename

The command you want is

Code: [Select]rename f:\%sta%_Dump_%UNIQUE%\ACTLOG\*.* %sta%_AlarmLog_*.*
I'll deal with these one problem at a time; let me know how this fix above works for you. 8-)Sorry but it dosn't it simply renames all of the files the same file name (well it tries to and after the first creates an error) It is still replacing the original file name. I need it to add to the original filename. Thanks again.Of course, it's never the easy solution. I'll keep working at it. In the meantime if anyone else knows, it's fine by me.For the first question, try the next for:

for %%A in (F:\test\*) do move %%A F:\test\WT01_MCC_%%~nA%%~xA

this rename all files in the directory F:\TEST like this:

one.log --> WT01_MCC_one.log
two.log --> WT01_MCC_two.log
three.log --> WT01_MCC_three.log
AlarmLog_001.log --> WT01_MCC_AlarmLog_001.log
tralaritralara.txt --> WT01_MCC_tralaritralara.txt
bye --> WT01_MCC_bye

You must replace "F:\test" for the path to your files.Worked like a champ. !!!! Thank you much.ok i take that back...it almost worked perfectly. here is what happened:

during the batch file the following varables get set
%sta%=PM02_LOS, %UNIQUE% = the date (YYYY.MM.DD)

here is the command line:
for %%A in (f:\%sta%_Dump_%UNIQUE%\CP\*) do move %%A f:\%sta%_Dump_%UNIQUE%\CP\%sta%_%%~nA%%~xA

here is the contents of the F:\ %sta%_Dump_%UNIQUE%\CP\ directory:
AlarmLog_000
AlarmLog_001
AlarmLog_002
AlarmLog_003
DataServer_000
DataServer_001
DataServer_002...DataServer_010 (total of 11 dataserver_XXX.log files)and here is the results that I got.

-PM02_LOS_AlarmLog_000 THRU PM02_LOS_AlarmLog_003 (JUST AS EXPECTED)
-PM02_LOS_PM02_LOS_DataServer_000 THRU -PM02_LOS_PM02_LOS_DataServer_010 (JUST AS NOT EXPECTED)

When I tried it with more than 11 dataserver files heres what happened. (the alarmlogs seemed just fine)
Dataserver_000 thru DataServer_017 (18 files)
Output:
PM02_LOS_PM02_LOS_DataServer_000 thru PM02_LOS_PM02_LOS_DataServer_013 (WEIRD)
PM02_LOS_DataServer_014 THRU PM02_LOS_DataServer_014 (JUST WHAT THE DOCTOR ORDERED)

huh???
Once again Thanks in Advance!!!!!correction to the last line

PM02_LOS_DataServer_014 THRU PM02_LOS_DataServer_017 (JUST WHAT THE DOCTOR ORDERED)

i forgot to change the number after i pasted it!!!Ok, I have create a directory like you:

F:\PM02_LOS_Dump_2006.04.04\CP>dir /b
DataServer_001.log
DataServer_002.log
DataServer_003.log
DataServer_004.log
DataServer_005.log
DataServer_006.log
DataServer_007.log
DataServer_008.log
DataServer_009.log
DataServer_010.log
DataServer_011.log
DataServer_012.log
DataServer_013.log
DataServer_014.log
DataServer_015.log
DataServer_016.log
DataServer_017.log

And I have create a batch like this:

Code: [Select]set sta=PM02_LOS
set UNIQUE=2006.04.04
for %%A in (f:\%sta%_Dump_%UNIQUE%\CP\*) do move %%A f:\%sta%_Dump_%UNIQUE%\CP\%sta%_%%~nA%%~xA
When I execute the previous bat In W2003 Server, it works fine:

F:\PM02_LOS_Dump_2006.04.04\CP>dir /b
PM02_LOS_DataServer_001.log
PM02_LOS_DataServer_002.log
PM02_LOS_DataServer_003.log
PM02_LOS_DataServer_004.log
PM02_LOS_DataServer_005.log
PM02_LOS_DataServer_006.log
PM02_LOS_DataServer_007.log
PM02_LOS_DataServer_008.log
PM02_LOS_DataServer_009.log
PM02_LOS_DataServer_010.log
PM02_LOS_DataServer_011.log
PM02_LOS_DataServer_012.log
PM02_LOS_DataServer_013.log
PM02_LOS_DataServer_014.log
PM02_LOS_DataServer_015.log
PM02_LOS_DataServer_016.log
PM02_LOS_DataServer_017.log

GOOD !!!

I repeat it in W2000 Profesional. This are the results:

F:\PM02_LOS_Dump_2006.04.04\CP>dir /b
PM02_LOS_PM02_LOS_DataServer_001.log
PM02_LOS_PM02_LOS_DataServer_002.log
PM02_LOS_PM02_LOS_DataServer_003.log
PM02_LOS_PM02_LOS_DataServer_004.log
PM02_LOS_PM02_LOS_DataServer_005.log
PM02_LOS_PM02_LOS_DataServer_006.log
PM02_LOS_PM02_LOS_DataServer_007.log
PM02_LOS_PM02_LOS_DataServer_008.log
PM02_LOS_PM02_LOS_DataServer_009.log
PM02_LOS_PM02_LOS_DataServer_010.log
PM02_LOS_PM02_LOS_DataServer_011.log
PM02_LOS_PM02_LOS_DataServer_012.log
PM02_LOS_PM02_LOS_DataServer_013.log
PM02_LOS_PM02_LOS_DataServer_014.log
PM02_LOS_PM02_LOS_DataServer_015.log
PM02_LOS_PM02_LOS_DataServer_016.log
PM02_LOS_PM02_LOS_DataServer_017.log

worse than yours !!!

I slightly modify the FOR (put the 'move' in parenthesis):

Code: [Select]for %%A in (f:\%sta%_Dump_%UNIQUE%\CP\*) do (
move %%A f:\%sta%_Dump_%UNIQUE%\CP\%sta%_%%~nA%%~xA
)
An the result is ...

F:\>dir f:\PM02_LOS_Dump_2006.04.04\CP\ /b
PM02_LOS_PM02_LOS_DataServer_001.log
PM02_LOS_PM02_LOS_DataServer_002.log
PM02_LOS_PM02_LOS_DataServer_003.log
PM02_LOS_PM02_LOS_DataServer_004.log
PM02_LOS_DataServer_005.log
PM02_LOS_DataServer_006.log
PM02_LOS_DataServer_007.log
PM02_LOS_DataServer_008.log
PM02_LOS_DataServer_009.log
PM02_LOS_DataServer_010.log
PM02_LOS_DataServer_011.log
PM02_LOS_DataServer_012.log
PM02_LOS_DataServer_013.log
PM02_LOS_DataServer_014.log
PM02_LOS_DataServer_015.log
PM02_LOS_DataServer_016.log
PM02_LOS_DataServer_017.log

of crazy !
A very very big [size=24]BUG[/size]

I rewrite the code: :-/

Code: [Select]SETLOCAL ENABLEDELAYEDEXPANSION
set sta=PM02_LOS
set UNIQUE=2006.04.04
for %%A in (f:\%sta%_Dump_%UNIQUE%\CP\*) do (
SET name=%%~nA
SET ext=%%~xA
move %%A f:\%sta%_Dump_%UNIQUE%\CP\%sta%_!name!!ext!
)
And the result is:

F:\>dir f:\PM02_LOS_Dump_2006.04.04\CP\ /b
PM02_LOS_PM02_LOS_DataServer_001.log
PM02_LOS_PM02_LOS_DataServer_002.log
PM02_LOS_DataServer_003.log
PM02_LOS_DataServer_004.log
PM02_LOS_DataServer_005.log
PM02_LOS_PM02_LOS_DataServer_006.log
PM02_LOS_DataServer_007.log
PM02_LOS_DataServer_008.log
PM02_LOS_DataServer_009.log
PM02_LOS_PM02_LOS_DataServer_010.log
PM02_LOS_DataServer_011.log
PM02_LOS_DataServer_012.log
PM02_LOS_DataServer_013.log
PM02_LOS_PM02_LOS_DataServer_014.log
PM02_LOS_DataServer_015.log
PM02_LOS_DataServer_016.log
PM02_LOS_DataServer_017.log

I'm becoming crazy
BLEP [smiley=vrolijk_26.gif] BLEP [smiley=vrolijk_26.gif] BLEP

The error is in the command MOVE, after several trys with different code, I have seen that the problem is with the character after the %sta% in the name of file (in this case, '_'). If you REMOVE, the MOVE work fine:

for %%A in (f:\%sta%_Dump_%UNIQUE%\CP\*) do move %%A f:\%sta%_Dump_%UNIQUE%\CP\[highlight]%sta%%%~nA[/highlight]%%~xA

But you don't want this:


F:\>dir f:\PM02_LOS_Dump_2006.04.04\CP\ /b
PM02_LOSDataServer_001.log
PM02_LOSDataServer_002.log
PM02_LOSDataServer_003.log
PM02_LOSDataServer_004.log
PM02_LOSDataServer_005.log
PM02_LOSDataServer_006.log
PM02_LOSDataServer_007.log
PM02_LOSDataServer_008.log
PM02_LOSDataServer_009.log
PM02_LOSDataServer_010.log
PM02_LOSDataServer_011.log
PM02_LOSDataServer_012.log
PM02_LOSDataServer_013.log
PM02_LOSDataServer_014.log
PM02_LOSDataServer_015.log
PM02_LOSDataServer_016.log
PM02_LOSDataServer_017.log

Every character that you insert after the %sta%, give you an error. Why ? Sorry, I haven't clue. :-? :-? :-?

Post your problem to MICROSOFT Good Luck
without running huge amounts of tests.....

when i deleted one of the letters from the filename it works fine. it only starts acting up when the file name is equal to 14 characters (i did not try longer) and then it only effects the first 14 files...go figure. and one more thing when i do the same thing but point it at my C:\ drive the results are completely different. it actually worked for me.....


what Can I do?!?!!the next code works fine for me, try it:

Code: [Select]@echo off
set sta=PM02_LOS
set UNIQUE=2006.04.04

mkdir f:\tmp
move f:\%sta%_Dump_%UNIQUE%\CP\* f:\tmp\
for %%A in (f:\tmp\*) do (
move %%A f:\tmp\%sta%_%%~nA%%~xA
MOVE f:\tmp\%sta%_%%~nA%%~xA f:\%sta%_Dump_%UNIQUE%\CP\
)
I rename all the files in a temporary directory, and then move it to the directory F:\%sta%_Dump_%UNIQUE%\CP\.

Good Luck.Who da man??

CARLOS DA MAN!!!!!!

Thank You...



Discussion

No Comment Found