Find and replace a string in a file

May 13, 2009 at 02:15:20
Specs: Windows XP
Hi,
I have a file which contains the name of various SQL scripts, like...
T1.sql;
t2.sql;
t3.sql;

inside these sql scripts i have a commit statement at the end of file like..
commit;

After this commit i want to add a exit statement so it will look like..
commit;
exit;

How can i do it through a batch file...
Please help.


See More: Find and replace a string in a file

Report •


#1
May 13, 2009 at 03:00:50
What does it mean?

I have a file which contains the name of various SQL scripts, like...
T1.sql;

inside these sql scripts i have a commit statement

Do you mean that the file contains nothing but the names of the SQL scripts or do you mean that you have a directory containing SQL scripts?

Your subject is "Find and replace a string in a file". Find what string and replace it with what?


Report •

#2
May 13, 2009 at 04:16:30
no, this is a normal file like a text file, it contains the names of different sql scripts. Inside those scripts at the end of the script a "commit" is there and i have to append a exit after the commit..

commit;
exit;


Report •

#3
May 13, 2009 at 04:48:51
Try this. Replace filename1.txt with the name of the file containing the sql filenames only. path is the path to where the actual sql files are located.

:: Code begins...
@echo off
cls

setlocal enabledelayedexpansion

for /f %%1 in (filename1.txt) do (
    set filename=%%1
    echo exit;>>path\!filename!
)
:: Code ends...

Good luck.

V.


Report •

Related Solutions

#4
May 13, 2009 at 04:57:42
for /f "tokens=* delims= " %%a in (myfile) do (
echo exit, >> %%a
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question