Computing.Net > Forums > Programming > Replace text with batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Replace text with batch file

Reply to Message Icon

Name: GMV
Date: January 23, 2009 at 11:11:32 Pacific
OS: Windows XP
CPU/Ram: core2
Product: Hewlett-packard / Dc7800
Subcategory: Batch
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: January 25, 2009 at 01:05:59 Pacific
Reply:

This sounds like a job for sed, especially since you're using Linux.


0

Response Number 2
Name: GMV
Date: January 26, 2009 at 04:14:30 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: January 26, 2009 at 10:56:21 Pacific
Reply:

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.

M2


0

Response Number 4
Name: GMV
Date: January 27, 2009 at 07:57:57 Pacific
Reply:

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


0

Response Number 5
Name: klint
Date: January 27, 2009 at 09:42:27 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: GMV
Date: January 28, 2009 at 06:49:20 Pacific
Reply:

I've tried searching for "'d:\code", but still no joy - smae problem as before


0

Response Number 7
Name: klint
Date: January 28, 2009 at 08:44:09 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

batch file not finding ex... need help setting this up



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Replace text with batch file

Find and Replace text in Batch File www.computing.net/answers/programming/find-and-replace-text-in-batch-file/12413.html

Replace text through batch file www.computing.net/answers/programming/replace-text-through-batch-file/20140.html

batch file www.computing.net/answers/programming/batch-file/14689.html