Replace text with batch file

Hewlett-packard / Dc7800
GMV January 23, 2009 at 11:11:32
Specs: Windows XP, core2
I am trying to replace the path names in several fortran files with an updated path for easy transfer between Windows and Linux. Ideally the paths should be user spoecified

currently the files read:
include 'D:\code\code1\file1.for'

I would like them to read
include '/home/user/code/code1/file1.for'

Is there some way of using a batch file to find the 'include' lines within the code and change the paths?

See More: Replace text with batch file

Report •

January 25, 2009 at 01:05:59
This sounds like a job for sed, especially since you're using Linux.

Report •

January 26, 2009 at 04:14:30
Message: Thanks for the help, but I'm still having problems. At the moment my batch file looks like:
set /p old= enter old directory
set /p new= enter new directory

sed 's_"'%old%'"_"'%new%'"_' %old%\user.for > %old%\temp\user1.for

For testing, the old directory is D:\code\code1 and the new directory is user/gv04/code/. When i run the file it does not seem to be able to find D:\code or it refuses to change it in the new file. The file I am searching (just for testing) simply says: include 'D:\code\code1\user.for' I would like it to say include 'user/gv04/code/user.for'
Any help would be gladly appreciated

Report •

January 26, 2009 at 10:56:21
This should change the include line in NT/XP but because DOS/winders uses a different line break than unix, the resulting file may make unix choke.

@echo off > newfile & setLocal EnableDelayedExpansion

set /p new=new?

for /f "tokens=* delims= " %%a in (myfile) do (
echo %%a | find /i "include" > nul
if errorlevel 1 (
echo %%a >> newfile
) else (
echo INCLUDE !new! >> newfile

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


Report •

Related Solutions

January 27, 2009 at 07:57:57
The code above seems to work for a file with just a single line of code, but does not work for longer files fixed format fortran files (it ruins the formating, making the file useless).

Is there any way of just replacing 'D:\code\code1\file.for' with '/home/gv04/file.for" without deleting and re-writing the whole whole include line (and ideally not having to delete the file.for bit - so that it can work with different filenames)?

Report •

January 27, 2009 at 09:42:27
I think the problem is your sed script is looking for 'D:\code'. The first ' is part of the string (it comes after "include ") but not the second '.

Report •

January 28, 2009 at 06:49:20
I've tried searching for "'d:\code", but still no joy - smae problem as before

Report •

January 28, 2009 at 08:44:09
It's case sensitive.

I also noticed you are using single quotes instead of double quotes around the sed command-line argument:

sed 's_"'%old%'"_"'%new%'"_' %old%\user.for > %old%\temp\user1.for

Single quotes only work under a Unix-style shell. You need double quotes in cmd.exe:

sed "s_'%old%_'%new%_" %old%\user.for > %old%\temp\user1.for

You are using _ as a delimiter, so make sure your directory names don't also use _ (or use something else like # as the delimiter.)

Report •

Ask Question