Newbie... Batch file creation?

Dell / Insperion 6400
January 12, 2009 at 23:34:01
Specs: Windows NT, 3GHz/2Gb RAM
Hi all,
I apologies for my total lack of programming knowledge, but I have been having a look at some of the threads on this forum and am convinced someone can help me...
I was wondering if it is possible to create a script/batch file to do a couple of things...

I guess I will start off with the problem. I have a certain program (call it Program 1) which creates files entitled "qcdtl01.txt". Within the "qcdtl01" text file is a series of data, including the time the file was created in the following format "Jan 13 2009 02:51 pm". Another program I run at the same time (Program 2) uses the data within the text files created by "Program 1", however to do this:

1) The text file needs to have the name changed from "qcdtl01.txt" to "qcdetail.txt"; and
2) The time stamp within the file needs to be in 24 hour time.
This needs to be done without altering any of the other data within the file.

So I guess what I was wondering was is it possible to create a script/batch? file to:
1) Search for recently created files on a drive and in all subdirectories on that drive for files with the name "qcdtl01.txt", and IF it finds them carry out step 2 which would be;
2) Rename the file to "qcdetail.txt" and modify the time stamp within the txt file to 24 hour time, all without changing the other contents of the text file.

These 2 steps need to happen every 15 seconds or so, so it would be cool if it could be contained within a batch file or something that i can run with a task scheduler...
I hope that makes sense, and looking forward to your replies.

cheers


See More: Newbie... Batch file creation?

Report •


#1
January 13, 2009 at 00:39:56
Whether the time stamp in qcdtl01.txt file
always the first line in that file?
If not, you have to post the content of a
typical qcdtl01.txt file.

--
Holla.


Report •

#2
January 13, 2009 at 01:56:50
Beyond that, and ignoring for the moment the '02:51 pm' [LOL], you need to state the EXACT OS.

Then maybe we can take on the time conversion.

You gotta wonder if program1 thinks 14:00 is in the morning.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#3
January 13, 2009 at 13:27:48
Hi Holla and Mechanix2Go,
Cheers for the quick reply. Holla the time isn't in the first line of the text file. I will paste an example of a qcdtl01 file. Hopefully it comes out ok. Mechanix yeah I know, the time does some weird things. Even though it says 02:51 PM it does actually mean 14:51. If a qcdtl01 file is created before midday it simply just puts an AM after the time.
Oh and the OS on the computer is Windows XP Professional, Version 2, SP2. Hopefully that helps.
The following is an example qcdtl01 text file. There is a whole heap of other stuff under what i have pasted below, but i figure thats not important? just so you can see what line the time stamp is in? Also the qcdtl01 file does not contain the headings Line1, Line2, etc. I put them in below to show you where each line begins. All other spaces between texts are real however. So if you just pasted what is below into a txt file and deleted the Line1, line2 etc that is what the start of the file looks like. Also with regards to the time it is only the time stamp in line 3 that needs to be changed into 24 hour time. Let me know if any of this info isnt sufficient.

Line1:[Header Info]
Line2: 9145213_06.D D:\TRaimondo\9145213\ 'tr ICP-MS C:\ICPCHEM\1\CALIB\ BENMNZ.C --- --- --- Point to Point
Line3:Jan 13 2009 02:51 pm BenMnz.M 'RR-1 'Sample c:\ICPCHEM\1\METHODS\ BenMnz.M
Line4:--- 1 1.000 Undiluted 1.000 ---
Line5:'
Line6:'Sample calibration curve Jul 28 2008 03:28 pm External Calibration Method 1/(SD*SD)


I really appreciate you both having a look at this.

cheers


Report •

Related Solutions

#4
January 14, 2009 at 16:36:27
This *might* do it, just be sure to test on unimportant copies/directories in case something goes wrong, after all it will modify your files(well sort of).

@ECHO OFF
setlocal
for /f "delims=" %%q in ('dir drive:\qcdtl01.txt /s/b/a-d') do (
>"%%~dpqnewfile$$$$$.txt" type nul
set cntr=0
for /f "usebackq delims=" %%a in ("%%~fq") do (
set line="%%a"
SETLOCAL ENABLEDELAYEDEXPANSION
if !cntr! lss 3 set /a cntr+=1
if !cntr! equ 3 (
for /f "tokens=1-5,* delims= " %%b in (!line!) do (
if /i "%%f"=="am" (
set line="%%b %%c %%d %%e %%g"
) else (
set hr12=%%e
if "!hr12:~0,1!"=="0" set hr12=!hr12:~-4!
set /a hr24=!hr12:~0,-3!+12
if !hr24! geq 24 set /a hr24-=12
set hr24=!hr24!!hr12:~-3!
set line="%%b %%c %%d !hr24! %%g"
)
)
set cntr=4
)
set line=!line:~1,-1!
>>%%~dpqnewfile$$$$$.txt echo !line!
call :endo
)
echo ren "%%~dpqnewfile$$$$$.txt" "%%~dpqqcdetail.txt"
)
pause
goto :eof
:endo
endlocal & set cntr=%cntr%
goto :eof

"drive" needs to be the drive letter your working on. It will process all subdirectories of the drive. A file named "newfile$$$$$.txt" will be made in every directoy that contains a "qcdtl01.txt" it *should* contain the modified time stamp and the rest of your data. The part of the script that renames the file is prefixed with echo so it will not be executed, this is so you can check the newfile$$$$$.txt files are accurate. Only when you are completely sure should you remove the echo command.

I am not responsible for any lost data...


Report •

#5
January 14, 2009 at 21:15:44
i found a shortcut changing the am/pm to 24-hr without using math:

time %%e %%f & time /t & time %time%

drawback is it can mess your time setting by few mili-seconds. and time /t depends on regional setting, but echo %time% is not.


Report •

#6
January 14, 2009 at 21:31:36
Personally I think the 5 lines I used on the math isn't worth messing with the rtc. If any processing is reliant on time stamps and a file happens to be created at the moment the time is out then problems could start.

Anyhow sometimes locked down systems have access to changing the time removed, this almost certainly would also apply to the time command.

I'm not debating it's a cleaver idea, in this instance however it may be overkill.


Report •

#7
January 14, 2009 at 21:37:59
Hi Judago and Reno
So close! I tried it with the echo to check the newfile$$$$$.txt files and there are a few issues.

Firstly the rest of the data in line 3 after the time is deleted/missing in the newfile.txt (i.e. BenMnz.M 'RR-1 'Sample c:\ICPCHEM\1\METHODS\ BenMnz.M).

Secondly in all of the data after line 6 in my example file above, for each line with no data in it, it has added "ECHO is off". This is probably my fault as I didn’t paste everything that is in the original qcdtl01.txt file”. Is there a way to get it to ignore everything after line 6? Or do I need to try to paste the whole qcdetail file?

And finally when I run the batch with “echo” removed from the start of the “ren” command it creates the newfile but doesn’t rename it to qcdetail. The batch file keeps repeating the messages "The syntax of the command is incorrect” and “Maximum setlocal recursion level reached”.

I hope this makes sense and again I appreciate everyones help.

cheers


Report •

#8
January 14, 2009 at 22:27:06
Ok most of those problems were my mistakes

@ECHO OFF
for /f "delims=" %%q in ('dir d:\qcdtl01.txt /s/b/a-d') do (
>"%%~dpqnewfile$$$$$.txt" type nul
set cntr=0
for /f "usebackq delims=" %%a in ("%%~fq") do (
set line="%%a"
SETLOCAL ENABLEDELAYEDEXPANSION
if !cntr! lss 3 set /a cntr+=1
if !cntr! equ 3 (
for /f "tokens=1-5,* delims= " %%b in (!line!) do (
if /i "%%f"=="am" (
set line="%%b %%c %%d %%e %%g"
) else (
set hr12=%%e
if "!hr12:~0,1!"=="0" set hr12=!hr12:~-4!
set /a hr24=!hr12:~0,-3!+12
if !hr24! geq 24 set /a hr24-=12
set hr24=!hr24!!hr12:~-3!
set line="%%b %%c %%d !hr24! %%g"
)
)
set cntr=4
)

set line=!line:~1,-1!
>>%%~dpqnewfile$$$$$.txt echo:!line!
for /f %%p in ("!cntr!") do endlocal&set cntr=%%p
)
ECHO ren "%%~dpqnewfile$$$$$.txt" "qcdetail.txt"
)
pause

I wasn't able to recreate the problem with the half line, try this one and see how you go. As for blank lines this script ignores them, if they are important I can add support for them.


Report •

#9
January 15, 2009 at 00:41:18
Hey Judago,
Getting so much closer! This time it renames the newfile to qcdetail, and it doesnt add crazy "Echo Off" statements to the line spaces. However it is still deleting the information after the time in line 3 (i.e. BenMnz.M 'RR-1 'Sample c:\ICPCHEM\1\METHODS\ BenMnz.M). Also is it possible to keep the AM/PM after the time, as it appears that the other program needs it (even though it is 24 hour time...). Crazy i know.
One final thing, as i was going to run it in the background automtically with a task manager is it possible to not have the "Press any key to continue..." prompt when the batch file is run?
By the way I owe you big time

cheers



Report •

#10
January 15, 2009 at 01:10:40
Adding am or pm - done.
Supporting empty lines - done.

As for the the rest of the line missing, I'm not sure; it's working fine for me. Here is a debug version so we can find the problem. Run it as before and copy the output and post it back. To copy from the cmd window right click on it and select mark the left click and drag over the text then right click on the selected text to copy it.

You must test this on a file that contains a pm time otherwise the output will be of no use to me.

@ECHO OFF
for /f "delims=" %%q in ('dir d:\qcdtl01.txt /s/b/a-d') do (
>"%%~dpqnewfile$$$$$.txt" type nul
set cntr=0
for /f "delims=" %%a in ('type "%%~fq"^|find /n /v "" ') do (
set line="%%a"
SETLOCAL ENABLEDELAYEDEXPANSION
call :strip in te rn
:strip
if "%~1%~2%~3"=="intern" (
if defined line (
if "!line:~0,1!"=="]" (
set line="!line:~1!
goto :eof
) else (
set line=!line:~1!
goto strip
)
) else (
goto :eof
)
)
if !cntr! lss 3 set /a cntr+=1
if !cntr! equ 3 (
for /f "tokens=1-5,* delims= " %%b in (!line!) do (
if /i "%%f"=="pm" (
echo:!line! --- full line
echo:%%g -- after xm
set hr12=%%e
if "!hr12:~0,1!"=="0" set hr12=!hr12:~-4!
set /a hr24=!hr12:~0,-3!+12
if !hr24! geq 24 set /a hr24-=12
set hr24=!hr24!!hr12:~-3!
set line="%%b %%c %%d !hr24! %%f %%g"
)
)
set cntr=4
)

if "!line!"=="" (set line=) else set line=!line:~1,-1!
>>%%~dpqnewfile$$$$$.txt echo:!line!
for /f %%p in ("!cntr!") do endlocal&set cntr=%%p
)
ren "%%~dpqnewfile$$$$$.txt" "qcdetail.txt"
)
pause

Removing the "press any key to continue. . . ." is very easy, just remove the very last line "pause", but don't do it yet because we need to see the output.


Report •

#11
January 15, 2009 at 14:14:15
Hey Judago,
Ok now the line after the time is sweet. Its all there. One last problem though....now the only thing its not doing is changing the time to 24 hour time. Everything else seems to be good though.
When I run the batch file it now shows and empty command shell and only pops up with the "press any key..." once it has finished changing the files. So there is actually no output to copy and paste to show you, if that makes sense...
cheers

Report •

#12
January 15, 2009 at 18:16:47
If there is not output other than pause something is wrong. I assume this was tested on a pm file otherwise the time would be correct(unchanged). Are there any blank lines before line 3, which contains the time.

I'll send you a pm so you can email me a complete file to test on(if you don't mind). That way I can be sure it works as expected....


Report •

#13
January 15, 2009 at 23:06:46
Ok I think this is now completely sorted. The presented problem was caused by a little old tab. The forums convert tabs to spaces here unless they are contained within pre tags. So the data I copied was not an accurate indication of your data. When I changed the pm to am in the file you sent me to am the new file was exact according to fc(file compare). Ditto for pm files after I changed the time back.

@ECHO OFF
for /f "delims=" %%q in ('dir d:\qcdtl01.txt /s/b/a-d') do (
>"%%~dpqnewfile$$$$$.txt" type nul
set cntr=0
for /f "delims=" %%a in ('type "%%~fq"^|find /n /v "" ') do (
set line="%%a"
SETLOCAL ENABLEDELAYEDEXPANSION
call :strip in te rn
:strip
if "%~1%~2%~3"=="intern" (
if defined line (
if "!line:~0,1!"=="]" (
set line="!line:~1!
goto :eof
) else (
set line=!line:~1!
goto strip
)
) else (
goto :eof
)
)
if !cntr! lss 3 set /a cntr+=1
if !cntr! equ 3 (
for /f "tokens=1-5 delims=	 " %%b in (!line!) do (
if /i "%%f"=="pm" (
set hr12=%%e
if "!hr12:~0,1!"=="0" set hr12=!hr12:~-4!
set /a hr24=!hr12:~0,-3!+12
if !hr24! geq 24 set /a hr24-=12
set hr24=!hr24!!hr12:~-3!
for %%h in (!hr24!) do set line=!line:%%e=%%h!
)
)
set cntr=5
)

if "!line!"=="" (set line=) else set line=!line:~1,-1!
>>%%~dpqnewfile$$$$$.txt echo:!line!
for %%p in (!cntr!) do endlocal&set cntr=%%p
)
ren "%%~dpqnewfile$$$$$.txt" "qcdetail.txt"
)


Report •

#14
January 15, 2009 at 23:19:18
Judago you are a deadset legend. Works a treat. Sorry it took so long to find the workaround. I should have just gave you the file to start with. I feel bad that you didn't get anything out of it...
Thanks for all your help!

Report •

#15
January 16, 2009 at 01:31:45
No worries, nobody forces me to do this..

Report •

#16
January 21, 2009 at 09:32:44
Hi Judago,

I'm glad you did the heavy lifting on this one.


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question