how do I hard code parameters in batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ciru
Posts: 3
Joined: 24 Feb 2012 08:40

how do I hard code parameters in batch file

#1 Post by ciru » 24 Feb 2012 09:01

Hoping someone can help me modify the attached batch file. Currently, this batch file basically finds and replaces text within a file. Currently, parameters are passed at the command line when executed. For example, if I want to find and replace an exclaimation point ! with a space within my input file INPUT.TXT and save to a new file called OUTPUT.txt, at the command line, I type the following: FindReplace.bat ! " " INPUT.TXT >>OUTPUT.txt

I need to modify this batch file so that there is no need to pass the parameters. I want the parameters hardcoded in the logic below to always find and replace ! with " " and always save it to a file called OUTPUT.txt This way, all I have to do is just type FindReplace.bat at the command prompt.


Can someone help me modify the code below to hard code the find and replace values within the batch file along with the NewFilename? Any help is greatly appreciated.

Code: Select all

@echo off

SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION


::syntax: FindReplace.bat OldStr NewStr OldFilename >>NewFileName
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: OldFilename [in] - file to be parsed
:: NewFilename [output] - new output filename

if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
 
Last edited by ciru on 24 Feb 2012 16:20, edited 2 times in total.

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

Re: how do I hard code parameters in batch file

#2 Post by Squashman » 24 Feb 2012 09:31

%1 is the character you want to replace.
%2 is the character you want to replace it with.
%3 is the filename

ciru
Posts: 3
Joined: 24 Feb 2012 08:40

Re: how do I hard code parameters in batch file

#3 Post by ciru » 24 Feb 2012 16:26

My apologies, but I am really at a loss. I have never coded DOS batch files and most of the syntax is all so foreign. I have inherited this logic and tried very hard to get it to go based on the suggestion provided for the last 5 hours. :? :oops: Is there anyway, that someone can show me how the changes are made within the code? I really need to get it going and put into production as soon as possible. I would greatly greatly appreciate it. Thank you.

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

Re: how do I hard code parameters in batch file

#4 Post by foxidrive » 24 Feb 2012 16:45

ciru wrote:This way, all I have to do is just type FindReplace.bat at the command prompt.


The best way to deal with a utility batch file is to create another batch file to call it.
Create this batch file and name it FR.BAT and place it in the same spot as FindReplace.bat

The beauty is that you can type FR on the command line and save keystrokes (remembering that FR is short for FindReplace)

Code: Select all

@echo off
call FindReplace.bat ! " " INPUT.TXT >>OUTPUT.txt

ciru
Posts: 3
Joined: 24 Feb 2012 08:40

Re: how do I hard code parameters in batch file

#5 Post by ciru » 24 Feb 2012 17:27

THANK YOU SO MUCH FoxiDrive! It worked out beautifully. :D Nice way to end a Friday.

Post Reply