Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

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

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 EnableDelayedExpansionset /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

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

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

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

![]() |
batch file not finding ex...
|
need help setting this up
|

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