Page 1 of 1

Search and replace

Posted: 29 Feb 2012 09:15
by kadara
Hi,

I'm new in creating batch files. I have a question regarding the 'search and replace a string' function.
I use the BatchSubstitute.bat (http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace) to replace a string in a text file and create a new text file with the changed content. I created a batch file using the code:

Code: Select all

BatchSubstitute.bat OldText NewText Text01.txt>Text02.txt

This code is working well. But I would like to print the new file (Text02.txt). For this I created a batch file with the following code:

Code: Select all

BatchSubstitute.bat OldText NewText Text01.txt>Text02.txt
Print Text02.txt /D:\\w20hy116\ZDesignerHY116

where \\w20hy116\ZDesignerHY116 is a shared printer.
After executing the first line (with BatchSubstitute) the batch file is closed without executing the printing instruction.
How could I print the new text file? Please help.
Thanks

Re: Search and replace

Posted: 29 Feb 2012 10:43
by Squashman
The printer needs to be a device. Eg: LPT1
Here is some other options for you.
http://www.robvanderwoude.com/printfiles.php

Re: Search and replace

Posted: 29 Feb 2012 11:02
by kadara
Nothing wrong with the line with the Print function.
The batch file is closed after executing the line with BatchSubstitute, it doesn't matter what is in the line after the BatchSubstitute.

Re: Search and replace

Posted: 29 Feb 2012 12:42
by Squashman
Learn something new everyday. Didn't realize you could print directly to the share name. I have always mapped to LPT# and then used the device name.

Are you seeing any error messages?
Put a PAUSE at the end of your script.
If it is printing it will display a message on the screen that it is printing the text file.
Does the PRINT work if you manually type it in at a cmd prompt?

Re: Search and replace

Posted: 29 Feb 2012 12:52
by kadara
I tried with the Pause, but nothing changed.

Re: Search and replace

Posted: 29 Feb 2012 12:57
by Squashman
kadara wrote:I tried with the Pause, but nothing changed.

So you are saying that even putting a pause at the end of your script it is just closing not executing the PRINT command.
Did you try typing the PRINT command at the cmd prompt to see if that does indeed work?

Re: Search and replace

Posted: 29 Feb 2012 13:11
by kadara
Yes, the Print command is working well if I execute it from the cmd prompt or included in a separate batch file.

Re: Search and replace

Posted: 29 Feb 2012 13:13
by Squashman
I don't have an explanation for why you need to do this. Someone will be along later I am sure to explain it but for some reason if you CALL the batch file it will work.

Code: Select all

call BatchSubstitute.bat Banana Apple Text01.txt>Text02.txt
print /D:\\Server\Printer_Share Text02.txt

Re: Search and replace

Posted: 29 Feb 2012 13:29
by kadara
OK, with the CALL function is working fine.
Thanks.

Re: Search and replace

Posted: 29 Feb 2012 13:34
by Squashman
This works as well.

Code: Select all

CMD /C BatchSubstitute.bat Banana Apple Text01.txt>Text02.txt 
print /D:\\Server\Printer_Share Text02.txt

Re: Search and replace

Posted: 29 Feb 2012 18:43
by foxidrive
Squashman wrote:if you CALL the batch file it will work.

Code: Select all

call BatchSubstitute.bat Banana Apple Text01.txt>Text02.txt
print /D:\\Server\Printer_Share Text02.txt



Yes: if a batch file is not CALLed then control will pass to the next batch file and not return to the original batch file, so execution will finish at that step.


In other words CALL is always necessary when executing other batch files (unless you want to pass control on to another batch file without returning).

Re: Search and replace

Posted: 01 Mar 2012 07:08
by kadara
OK.
Thanks for your reply.