[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Reading from a Text File and Writing to a Text File

I am trying to write a MS-DOS batch file to read from a text file called batch_bo_report_param. I am trying to read the third line of the text file (batch_bo_report_param) which looks like this:

BOUSER=bouser
BOPASS=bouser
Execution Date (YYYY-MM-DD) : =2005-03-28
Execution Month (YYYY-MM) : =2005-03
Batch Bo Report Path: =C:\MDB_BATCH_BO_REPORTS\Report\
Log Batch Bo Report Path: =C:\MDB_BATCH_BO_REPORTS\Log\
Batch Result Bo Report Path: =C:\MDB_BATCH_BO_REPORTS\Result_Report\
Logo Path Name : =C:\MDB_BATCH_BO_REPORTS\Param\KLLogo.bmp

The command that I used was this:
FOR /F "skip=2 tokens=6-8 delims=-,:= " %%i in (d:\batch_bo_report_param.txt) do @echo %%i
%%j %%k

The output that I received after running the above command was this:
2005 03 28
03
\MDB_BATCH_BO_REPORTS\Report\
C \MDB_BATCH_BO_REPORTS\Log\
C \MDB_BATCH_BO_REPORTS\Result_Report\

My 1st Question: I am trying to read the date which is in the third line of the text file (batch_bo_report_param) and am able to do so. But why am I getting the additional lines like 03 and \MDB_BATCH...... and so on.

How should the script be re-written in order to only get the date from the third  line and prevent the other lines after the third line from appearing?


My 2nd Question: At the third line of the batch_bo_report_param text file, I would like to replace the date with today's date. I have the code to generate today's date and to put today's date into variables, but how do I write these variables holding today's date into the third line at the part that has the date 2005-03-28?


Previously I had posted a question rather similar to the above, but I was given a script in VBS. I am interested in learning and getting code in MS-DOS. My understanding of VBS is very slow and I get confused. I am only looking for code for a MS-DOS batch file. I need help on this urgently.

Thank You.
0
ben1211
Asked:
ben1211
  • 4
  • 3
  • 2
2 Solutions
 
LittleRed1Commented:
Try this:

FOR /F "tokens=6-8 delims=-,:= " %%i in ('type d:\batch_bo_report_param.txt^|find /i "execution date"') do @echo %%i %%j %%k
0
 
oBdACommented:
Just for the record: You are *not* working with MS-DOS here. You are using the NT command shell, which is quite more powerful than MS-DOS (you won't find "for /f ..." in MS-DOS, for example).
That said, to extract only the line containing the execution date, use "find":

@echo off
setlocal
set InputFile=C:\Temp\test.txt
set OutputFile=C:\Temp\test2.txt
for /f "tokens=6-8 delims=-,:= " %%a in ('type "%InputFile%" ^| find /i "Execution Date"') do set ExecutionDate=%%a %%b %%c
echo Execution Date: %ExecutionDate%

To change the execution date, you can use a "for /f" loop as well. Type the original file, and echo it unchanged to the output file, unless the line starts with "Execution Date" (note that you need to escape the brackets with a caret in the echo command, since they are reserved symbols):

@echo off
setlocal
set InputFile=C:\Temp\test.txt
set OutputFile=C:\Temp\test2.txt
set NewExecutionDate=2005-05-09
if exist "%OutputFile%" del "%OutputFile%"
for /f "tokens=1,2*" %%a in ('type "%InputFile%"') do (
  if /i "%%a %%b"=="Execution Date" (
    >>"%OutputFile%" echo Execution Date ^(YYYY-MM-DD^) : =%NewExecutionDate%
  ) else (
    >>"%OutputFile%" echo %%a %%b %%c
  )
)

The redirection of the echo command is at the beginning of the line to not produce additional spaces at the end of the lines.
I'm assuming W2k or later here, the "else" isn't available in NT4.
0
 
ben1211Author Commented:
LittleRed1

Could you please explain this line to me: FOR /F "tokens=6-8 delims=-,:= " %%i in ('type d:\batch_bo_report_param.txt^|find /i "execution date"') do @echo %%i %%j %%k

What exactly does this line do: ('type d:\batch_bo_report_param.txt^|find /i "execution date"')

Why can't I use the "skip" command that is found with the FOR statement?

Thank You.

Ben
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LittleRed1Commented:
Hi Ben

It's quite simple. Instead of reading all the lines in the file, use the FIND command to filter the content for lines containing the string 'execution date'. The /i switch tells FIND to be case insensitive.

To be able to use find in this way, you have to pipe (|) the file to it so that the output can be fed into the FOR command. The easiest way to do this is to use TYPE.

The whole command is enclosed in single quotes because we want the FOR command to execute it. The carat before the pipe (^|) tells the call to treat the pipe as a special character.

If you run the command (without the carat) from the command line you should see only the line that you're looking for, and that is what is used by FOR to extract the date.

So, the command - type d:\batch_bo_report_param.txt|find /i "execution date"

should give you the output:
Execution Date (YYYY-MM-DD) : =2005-03-28

which means there is no need to skip the first two lines.

0
 
ben1211Author Commented:
Hi LittleRed1,

I ran the line of script that you gave me:
FOR /F "tokens=6-8 delims=-,:= " %%i in ('type c:\batch_bo_report_param.txt^|find /i "execution date"') do @echo %%i %%j %%k

and it worked, given me the date that was in the text file.


After some time, I ran the same script again, and I received an error message stating: The system cannot find the file specified.

I checked and am certain that the batch_bo_report_param.txt file is located in the c:\  as well as the batch file with the above given code. Any ideas?

I ran the same batch file on a different PC and it worked fine. Would appreciate any help.

Thank You.


Ben
0
 
LittleRed1Commented:
Not really sure Ben, but I'd check the syntax carefully, make sure you haven't left out a ' or ". A quick DIR will verify the existence of the file. If the file is there, make sure you can read it by running the TYPE command - type c:\batch_bo_report_param.txt.

Apart from that, I can't think of any other reason why it would do that.
0
 
ben1211Author Commented:
LittleRed1,

You have given me code to extract a certain line or date from a text file. Now considering that I have extracted the date, how can I replace the date with values that I want. For example, I read the execution date and received three values within three variables. How can I use these variables to replace the date.

Second question: When would I use the command "skip" in a FOR statement. Any examples you have?

oBdA has given me some code, but I am rather confused in reading it and trying to understand it. I do value his/her's input and the code given, but I can't seem to understand it very well.

Could you help out please?

Much Appreciated. Thank You.
0
 
oBdACommented:
My script does basically the same as yours; I just like to keep my scripts easily adoptable, which is why I usually define files which are used for input as a variable; makes later changes easier. In your case, Inputfile should have been set to c:\batch_bo_report_param.txt (forgot that when I copy and pasted the script after testing).
So this will set the variable %ExecutionDate% to the execution date read from the file:

@echo off
setlocal
set InputFile=c:\batch_bo_report_param.txt
for /f "tokens=6-8 delims=-,:= " %%a in ('type "%InputFile%" ^| find /i "Execution Date"') do set ExecutionDate=%%a %%b %%c
echo Execution Date: %ExecutionDate%

To actually change the date in the file, you need the second code snippet. As you're still reading from the original text file while creating the new one, you'll need two file definitions here, the InputFile (with the original date), and the OutputFile (with the new date).
The script will then read the input file line by line using a for loop, splitting the line into three tokens: the first word (%%a), the second word (%%b), and the rest of the line (%%c).
The first two words will then be checked; if they indicate the correct line, the line with the new date will be written to the output file.
If the line does not begin with the "Execution date", the line will be written without changes to the output file.

@echo off
setlocal
set InputFile=c:\batch_bo_report_param.txt
set OutputFile=c:\batch_bo_report_param-new.txt
set NewExecutionDate=2005-05-10
if exist "%OutputFile%" del "%OutputFile%"
for /f "tokens=1,2*" %%a in ('type "%InputFile%"') do (
 if /i "%%a %%b"=="Execution Date" (
   >>"%OutputFile%" echo Execution Date ^(YYYY-MM-DD^) : =%NewExecutionDate%
 ) else (
   >>"%OutputFile%" echo %%a %%b %%c
 )
)

The ('type "%InputFile%"') instead of (%InputFile%) is used because of possible problems with spaces in the file name. Using (%InputFile%) with a file name that contains spaces (File with spaces.txt) will result in an error, because three files will be scanned: "File", "with", "spaces.txt". Putting quotes around it won't help, because the interpreter would then simply look at the string "File with spaces.txt", instead of the file. The 'type ...' is compatible with NT4.

The "skip" is of use when you're processing the output of a command that at the beginning has some describing lines that you don't need. In your case, it won't help, because "skip" will only skip lines at the beginning, but there are other lines following, which will be processed by the for loop as well.
0
 
ben1211Author Commented:
obda

@echo off
setlocal
set InputFile=c:\batch_bo_report_param.txt
for /f "tokens=6-8 delims=-,:= " %%a in ('type "%InputFile%" ^| find /i "Execution Date"') do set ExecutionDate=%%a %%b %%c
echo Execution Date: %ExecutionDate%


I copied the above code into a .bat file.  Ran it and it didn't work. It didn't echo the Execution Date.

Ben
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now