|
Answer» I originally posted this as part two in a two part problem but it seems that noone was up to the challenge so I am re-posting this all by itself also because I have changed it just a bit. I am told this is possible but you tell me.... anyone willing to step up to the plate?
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 alarmlog_01.log thru alarmlog_12.log
First two lines of the first text file(alarmlog_01.log): INLS_LOS Version 0.38 Fri 31-Mar-2006 14:08:10 System 1 Startup
What I need to happen is: 1- rename the file to 060331_140810_Alarmlog.log 2- check the next file and rename it as well using the date stamp in that file 3- repeat the process until all files have been done 4- if any on the files have the same date stamp, append the second one with an "_A" or "_1" or what ever.
Can you do that?? Super Duper Thanks in Advanced.does the line "system 1 startup" always occur in the line with the date stamp, does it also occur elsewhere in the file"System 1 Startup" always appears on the second line at the end in the same exact location in each file as shown below. "System 1 Startup" ONLY occurs one time during each file. I have included and example of the first few lines from a typical log file.
INLS_CF Version 0.38 Sat 22-Apr-2006 13:55:17 System 1 Startup Sat 22-Apr-2006 13:55:17 Vessel Configuration changed from -1 to 0 Sat 22-Apr-2006 13:55:19 NMS1: MASTER Sat 22-Apr-2006 13:55:19 Position: Manual Mode Sat 22-Apr-2006 13:56:11 Heading: Manual Mode Sat 22-Apr-2006 13:56:13 Thruster Bow T1: OFF Sat 22-Apr-2006 13:58:22 Thruster Port Main: OFF Sat 22-Apr-2006 13:58:54 Thruster Stbd Main: OFF Sat 22-Apr-2006 14:03:14 Gyro 1: ONLINE
Thanks[size=9]this works in windows xp CMD, i suppose it should work in win2000
you need two batch files
name the 1st file renlog.bat it should contain the following line
@for %%x in (*.log) do call fixx %%x
name the 2nd batch file fixx.bat, its contents should be as follows
@echo off find /i "system 1 startup" %1|find /i "system">$$$1.bat echo set date=%%1>Sat.bat echo set time=%%2>>Sat.bat echo set dd=%%date:~0,2%%>>Sat.bat echo set mm=%%date:~3,3%%>>Sat.bat echo set yy=%%date:~9,2%%>>Sat.bat echo if %%mm%%#==Jan# set mm=01>>Sat.bat echo if %%mm%%#==Feb# set mm=02>>Sat.bat echo if %%mm%%#==Mar# set mm=03>>Sat.bat echo if %%mm%%#==Apr# set mm=04>>Sat.bat echo if %%mm%%#==May# set mm=05>>Sat.bat echo if %%mm%%#==Jun# set mm=06>>Sat.bat echo if %%mm%%#==Jul# set mm=07>>Sat.bat echo if %%mm%%#==Aug# set mm=08>>Sat.bat echo if %%mm%%#==SEP# set mm=09>>Sat.bat echo if %%mm%%#==Oct# set mm=10>>Sat.bat echo if %%mm%%#==Nov# set mm=11>>Sat.bat echo if %%mm%%#==Dec# set mm=12>>Sat.bat echo set date=%%yy%%%%mm%%%%dd%%>>Sat.bat echo set hh=%%time:~0,2%%>>sat.bat echo set mm=%%time:~3,2%%>>sat.bat echo set ss=%%time:~6,2%%>>sat.bat echo set time=%%hh%%%%mm%%%%ss%%>>sat.bat echo set name=%%date%%_%%time%%_Alarmlog.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_1.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_2.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_3.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_4.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_5.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_6.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_7.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_8.log>>sat.bat echo if exist %%name%% set name=%%date%%_%%time%%_Alarmlog_9.log>>sat.bat echo ren %1 %%name%%>>sat.bat echo set yy=>>sat.bat echo set mm=>>sat.bat echo set dd=>>sat.bat echo set hh=>>sat.bat echo set ss=>>sat.bat copy Sat.bat Sun.bat>nul copy Sat.bat Mon.bat>nul copy Sat.bat Tue.bat>nul copy Sat.bat Wed.bat>nul copy Sat.bat Thu.bat>nul copy Sat.bat Fri.bat>nul call $$$1.bat del $$$1.bat del .bat
place both files in the directory containing the files to be renamed and run renlog.bat
Please post back to tell whether it works[/size]ok I ran it and here is what happened. additionally I did a before and after dir so you could SEE that it seemed to create a few extra files.I have a few additional facts to add they may or may not mean anything, but here they are:
1st - i lied (fibbed actually) about the file names, they are actually AlarmLog_001 (one more leading 0 than I had originally said) but i renamed them for my test.
2nd - there are other files in the directory, i removed them from the directory for my test. they will be there eventually called DataServer_001 thru DataServer_XXX. no action is required for these files.
3rd - after carefull review I noted that there are two spaces between the year and the hour and then again between the second and the System 1 Startup...I however eliminated that space for my test and it did not help.
Fri 31-Mar-2006 16:57:52 System 1 Startup
4th - some of the files have a variation after the INLS_ ....some will be CF and some will be LOS. However, each will be in a seperate directory....there will not be CF & LOS files in the same directory. If that will matter we can just MAKE a seperate routine for each and execute one based upon a known trigger
INLS_CF Version 0.38
== thisbegins my dos session==
C:\xx>dir Volume in drive C is WT07_MCC Volume Serial Number is 4CA5-9F13
Directory of C:\xx
05/05/2006 01:58 PM . 05/05/2006 01:58 PM .. 01/02/2002 07:10 AM 9,814 AlarmLog_00.log 01/01/2002 03:07 PM 1,694 AlarmLog_01.log 01/05/2002 06:20 PM 252,694 AlarmLog_02.log 04/27/2006 12:09 PM 65,546 AlarmLog_03.log 05/05/2006 01:53 PM 2,126 fixx.bat 05/05/2006 01:53 PM 109 RENLOG.BAT 6 File(s) 331,983 bytes 2 Dir(s) 15,076,622,336 bytes free
C:\xx>renlog
C:\xx>call fixx AlarmLog_00.log #==Jan# was unexpected at this time.
C:\xx>dir Volume in drive C is WT07_MCC Volume Serial Number is 4CA5-9F13
Directory of C:\xx
05/05/2006 01:59 PM . 05/05/2006 01:59 PM .. 05/05/2006 01:59 PM 45 $$$1.bat 01/02/2002 07:10 AM 9,814 AlarmLog_00.log 01/01/2002 03:07 PM 1,694 AlarmLog_01.log 01/05/2002 06:20 PM 252,694 AlarmLog_02.log 04/27/2006 12:09 PM 65,546 AlarmLog_03.log 05/05/2006 01:53 PM 2,126 fixx.bat 05/05/2006 01:59 PM 1,148 Fri.bat 05/05/2006 01:59 PM 1,148 Mon.bat 05/05/2006 01:53 PM 109 RENLOG.BAT 05/05/2006 01:59 PM 1,148 Sat.bat 05/05/2006 01:59 PM 1,148 Sun.bat 05/05/2006 01:59 PM 1,148 Thu.bat 05/05/2006 01:59 PM 1,148 Tue.bat 05/05/2006 01:59 PM 1,148 Wed.bat 14 File(s) 340,064 bytes 2 Dir(s) 15,076,433,920 bytes free
C:\xx>
== this ends my dos session ==do not modify contents of any of the files the code depends on the date, time and "system 1 startup" being where they originally are. Without spaces seperating them, the batch will not work. the renaming of files as you have done is needed. the batch will only process files with .log extension
however if this does not solve the problem it would be because CMD in win 2000 lacks some of the features present in XP in XP it is possible to get selected characters from a given environmental variable into a new environmental variable. for eg. set mm=%date:~0,2% would define "mm"" as containing first 2 characters of dateI am using windows XP but it is intended to go on windows 2000.
but let me get this straight...
I am supposed to rename the files only and do not bother anything inside of them. I did this and it did not help.
i tested it on xp and on 2000
i could just zip the whole directory and attach or send it to you. If you would be willing to look at it.fine, send it to meIt is not impossible..you are working in XP i suppose (from the threads) and there are plenty of better scripting tools out there that you can use..i am sure you are not restricted to using them , right? or are you only allowed to use plain batch programming only? It is much easier to do it in say perl or python (or other scripting languages) here's one in python..not the best , but it does do what you want, though you should tailor it to your needs if you want to use it.
import os,time dir_where_file_is = os.path.join("C:\\","directory") os.chdir(dir_where_file_is) renamed = "_Alarmlog.log" for fi in os.listdir(dir_where_file_is): f = open(fi).readlines()[1].split(' ') #get the second line where it shows "System 1 Startup" thedate = f[1] #assume same file structures thetime = f[3] #assume same file structures year,mth,day = time.strptime(thedate,"%d-%b-%Y")[0:3] if mth in range(1,9): mth = "0" + str(mth) formatteddate = "%s%s%s" % (str(year)[-2:],mth,day) formatedtime = ''.join(thetime.split(":")) os.rename(fi,formatteddate + "_" + formatedtime + renamed )i would not know how to modify anything in pyhton...do i have to install additional software on the computers? the basic idea for me is that I have about 35 computers running windows 2000, they are running and allen bradley software that creats a continous log file of events & alarms. I am restricted to a memory stick...I cannot install any sotware on the host computers...i have been using a batch file because that is where I began when all i wanted to do was to copy the alarm files off of the computer, now that i am renaming and quite a few other things maybe i should look elsewhere?
what does anyone think about that?Quote i would not know how to modify anything in pyhton...do i have to install additional software on the computers? the basic idea for me is that I have about 35 computers running windows 2000, they are running and allen bradley software that creats a continous log file of events & alarms. I am restricted to a memory stick...I cannot install any sotware on the host computers...i have been using a batch file because that is where I began when all i wanted to do was to copy the alarm files off of the computer, now that i am renaming and quite a few other things maybe i should look elsewhere?
what does anyone think about that? I am once a Windows Admin too and have to write batch files to do my job. As i write more and more batch files, i find i needed more scripting power such as text/string manipulation, date manipulation, arrays, etc... so i chose python as my scripting choice instead of batch. I am not saying Windows batch programming cannot do the job , but it will take some time for you to figure out how to do what you want for this case. yes, you have to install the python interpreter...just like for example.. if you want to program in java, you have to install the Java runtime... I guess if you really cannot install any software, then u might really have to stick to batch...sorry, can't really help you as i have not done batch since i used python..hope DOSman or others can help you with that. good luck
Is this resolved?
If yes then the forum rules suggest to post the result here so that everybody can benefit from it. Also there are DOS dudes that always want to improve a solution.
I.e.: You can easy convert the month string into it's two digit number using the following 3 lines:
Code: [Select]SET mon=Mai
SET map=Jan-01;Feb-02;Mar-03;Apr-04;Mai-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12 CALL SET mon=%%map:*%mon%-=%% SET mon=%mon:;=&rem.%
ECHO.%mon% %mon% will be 05.
|