Search and replace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kadara
Posts: 6
Joined: 29 Feb 2012 08:40

Search and replace

#1 Post by kadara » 29 Feb 2012 09:15

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search and replace

#2 Post by Squashman » 29 Feb 2012 10:43

The printer needs to be a device. Eg: LPT1
Here is some other options for you.
http://www.robvanderwoude.com/printfiles.php

kadara
Posts: 6
Joined: 29 Feb 2012 08:40

Re: Search and replace

#3 Post by kadara » 29 Feb 2012 11:02

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search and replace

#4 Post by Squashman » 29 Feb 2012 12:42

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?

kadara
Posts: 6
Joined: 29 Feb 2012 08:40

Re: Search and replace

#5 Post by kadara » 29 Feb 2012 12:52

I tried with the Pause, but nothing changed.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search and replace

#6 Post by Squashman » 29 Feb 2012 12:57

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?

kadara
Posts: 6
Joined: 29 Feb 2012 08:40

Re: Search and replace

#7 Post by kadara » 29 Feb 2012 13:11

Yes, the Print command is working well if I execute it from the cmd prompt or included in a separate batch file.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search and replace

#8 Post by Squashman » 29 Feb 2012 13:13

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

kadara
Posts: 6
Joined: 29 Feb 2012 08:40

Re: Search and replace

#9 Post by kadara » 29 Feb 2012 13:29

OK, with the CALL function is working fine.
Thanks.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search and replace

#10 Post by Squashman » 29 Feb 2012 13:34

This works as well.

Code: Select all

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Search and replace

#11 Post by foxidrive » 29 Feb 2012 18:43

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).

kadara
Posts: 6
Joined: 29 Feb 2012 08:40

Re: Search and replace

#12 Post by kadara » 01 Mar 2012 07:08

OK.
Thanks for your reply.

Post Reply