text find and replace scheduled task

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Marlowe_P
Posts: 4
Joined: 22 Feb 2013 11:07

text find and replace scheduled task

#1 Post by Marlowe_P » 22 Feb 2013 11:14

i have a txt file on my windows computer. this file is automatically and successfully filled every day by a procedure, but for me to use it i need first to find and replace a string of characters, always the same. what's the simplest way to do that automatically (not manually)? compile a bat file and then create a scheduled task that launch it? where can i find a bat file like that in where i can just type the "find_string", the "replace_string" and the "myfile_url", so then i can just tell the windows scheduler the bat file url and have the task automatically done every day? thanks for helping

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

Re: text find and replace scheduled task

#2 Post by foxidrive » 22 Feb 2013 11:42

Here's something you can use. Alter the in.txt and out.txt to suit you.

If you need assistance in getting it going then please provide details about the task.


Code: Select all

:: Search and replace
@echo off
if "%~2"=="" (
echo search and replace usage: "%~nx0" "search string" "new string"
echo.
echo. input file is in.txt and output file is out.txt
pause
goto :EOF
)


set "file=in.txt"
set "newfile=out.txt"
set "oldstr=%~1"
set "newstr=%~2"

set "tempfile=%temp%\sartmp.vbs"
echo> "%tempfile%" s = Wscript.StdIn.ReadAll
echo>> "%tempfile%" Wscript.Echo Replace(s,"%oldstr%","%newstr%")

type "%file%" |cscript /nologo "%tempfile%"  > "%newfile%"
del "%tempfile%"

Marlowe_P
Posts: 4
Joined: 22 Feb 2013 11:07

Re: text find and replace scheduled task

#3 Post by Marlowe_P » 22 Feb 2013 13:14

first thing: from here, windows xp, if a save as .bat a text file it can't be run on dos just double clicking: why is that???
second: i can't exactly understand which string i should personalise into that code.
i'm very unexpert user

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: text find and replace scheduled task

#4 Post by mfm4aa » 22 Feb 2013 14:46

This is from Google: http://www.dostips.com/?t=batch.findandreplace
please note the script output.

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

Re: text find and replace scheduled task

#5 Post by foxidrive » 22 Feb 2013 21:24

Marlowe_P wrote:first thing: from here, windows xp, if a save as .bat a text file it can't be run on dos just double clicking: why is that???


Yes, it can. A batch file normally does a task and ends. You can launch it from a cmd window to see if any error messages are on the screen.

The file content you are changing is in.txt and the changed content will be in out.txt both in the current folder.

Change these lines

set "file=in.txt"
set "newfile=out.txt
"

Marlowe_P
Posts: 4
Joined: 22 Feb 2013 11:07

Re: text find and replace scheduled task

#6 Post by Marlowe_P » 23 Feb 2013 04:50

to foxidrive:

the string i want to replace is always and exactly this: cons;
the string i want to obtain is always and exactly this: cons;x

it would be better if it could be done on the same file, not creating a new file (i've seen that if i put the same document for in.txt and out.txt it gives me error), but it's ok even like this...

the real problem is that the script works but apparently not for string that contain the colon (;) character: how can i solve that???!!!

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

Re: text find and replace scheduled task

#7 Post by foxidrive » 23 Feb 2013 04:55

The line near the top creates a file.
The line near the bottom shows what is in the file after changing it.

Try it for yourself. I suspect you didn't quote the text when you used it - that would cause issues with ; = and other characters.

As for having a different output file - that is how batch files work.
You can use this as the last line to overwrite the original file.
move /y "out.txt" "in.txt" >nul


Code: Select all

@echo off
>in.txt echo here is a cons; text
type in.txt

set "file=in.txt"
set "newfile=out.txt"
set "oldstr=cons;"
set "newstr=cons;x"

set "tempfile=%temp%\sartmp.vbs"
echo> "%tempfile%" s = Wscript.StdIn.ReadAll
echo>> "%tempfile%" Wscript.Echo Replace(s,"%oldstr%","%newstr%")
type "%file%" |cscript /nologo "%tempfile%"  > "%newfile%"
del "%tempfile%"


type out.txt
pause

Marlowe_P
Posts: 4
Joined: 22 Feb 2013 11:07

Re: text find and replace scheduled task

#8 Post by Marlowe_P » 23 Feb 2013 10:29

thanks again, you're right.

still a problem unfortunately....

about special characters like ;

i can make the script working from the prompt typing
"replace.bat" "cons;" "cons;x"

but when i try to set a scheduled job, it works only with no quotes, so i can just replace strings with no special characters!!!

is it possible to change the bat file so that the two strings are straight part of it (given the fact i need to find and replace exactly that string, always the same: from cons; to cons;x): then i can just tell the scheduler to run the bat file without specifying the two strings that are already in the bat file???

thanks for helping

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

Re: text find and replace scheduled task

#9 Post by foxidrive » 23 Feb 2013 11:06

You can hard code the path and filename and also the strings.

Alter the c:\data to the folder where your file is.

Also alter the in.txt filename in two places to what your filename is.

You can leave the out.txt names as they are and then just schedule it without any parameters.

Code: Select all

@echo off
set "folder=c:\data"

set "file=%folder%\in.txt"
set "newfile=%folder%\out.txt"
set "oldstr=cons;"
set "newstr=cons;x"

set "tempfile=%temp%\sartmp.vbs"
echo> "%tempfile%" s = Wscript.StdIn.ReadAll
echo>> "%tempfile%" Wscript.Echo Replace(s,"%oldstr%","%newstr%")
type "%file%" |cscript /nologo "%tempfile%"  > "%newfile%"
del "%tempfile%"
move /y "%folder%\out.txt" "%folder%\in.txt" >nul



For what it's worth, you can use quotes in task scheduler parameters but you might find that in some places it's difficult to figure it out.
I usually enter the text in the command line, save it, then edit it and put the quotes where they should be.

Post Reply