# MSFN Forum: Text Replacement Using A Batch - MSFN Forum

Jump to content

• 2 Pages
• 1
• 2

## Text Replacement Using A Batch Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.msfn.org/board/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=42248&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1fillalph

• Advanced Member
• Group: Members
• Posts: 324
• Joined: 29-July 03

Posted 24 March 2005 - 07:00 PM

I am curious as to how to replace text in a file using a batch file. Here is my reasoning.

Here is my directory structure:
Untitled_1.gif (3K)
Number of downloads: 78
In my VOL1 and VOL2 I have my boot directories. In there my vol1.dat and vol2.dat as well as txtsetup.sif. I use nlite so my txtsetup.sif changes with each modification of my i386. I have my cdimage.cmd run a couple of things before making my iso. It copies txtsetup.sif from the i386 folder to my VOL1 and VOL2 folders. The problem is, that each time I will have to edit this line in that file: SetupSourcePath = "\" to SetupSourcePath = "\Prox\".

So basically, can someone post some code, or recommend a site to help me understand this idea of replacing text in file.

Thanks
]Bonkers[

### #2dman

• Friend of MSFN
• Group: Members
• Posts: 717
• Joined: 01-February 05

Posted 24 March 2005 - 07:21 PM

SED (stream editor) is one of the most common and useful commands in UNIX. It is also available for DOS/Windows. It is basically a command line "find and replace" utility.
Command is usually something like SED <input file>, <text to replece>, <replacement text>
You can find some info on it here...
http://www.student.n...du/pemente/sed/

There is also a command called "MUNGE" in the NT4 resource kit that is much the same as SED
http://www.ss64.com/nt/munge.html

### #3fillalph

• Advanced Member
• Group: Members
• Posts: 324
• Joined: 29-July 03

Posted 24 March 2005 - 08:11 PM

dman: thanks for your reply. I will try and work with these tools.

This was more though of what I was looking for Find and Replace.

Thanks
]Bonkers[

### #4clavicle

• Advanced Member
• Group: Members
• Posts: 454
• Joined: 28-January 04
• OS:Windows 7 x64
• Country:

Posted 24 March 2005 - 08:20 PM

Thanks dman. Earlier about I was searching for a help for a similar type but got no response. (Refer http://www.msfn.org/board/index.php?showtopic=37723&hl= ) Post still is unanswered. But I found munge on the net. But I think sed is still a better option. Thanks again!

### #5fillalph

• Advanced Member
• Group: Members
• Posts: 324
• Joined: 29-July 03

Posted 24 March 2005 - 08:45 PM

I was thinking more along the lines of using the FOR /F command but I am not sure on the rest of the code.

]Bonkers[

### #6fillalph

• Advanced Member
• Group: Members
• Posts: 324
• Joined: 29-July 03

Posted 24 March 2005 - 09:40 PM

I looked for a copy of munge.exe but I didn't find one. If someone has it could they upload it and provide a link?

Thanks
]Bonkers[

### #7dman

• Friend of MSFN
• Group: Members
• Posts: 717
• Joined: 01-February 05

Posted 25 March 2005 - 08:10 AM

Munge.exe

#### Attached File(s)

•  munge.zip (26.47K)
Number of downloads: 137

### #8fillalph

• Advanced Member
• Group: Members
• Posts: 324
• Joined: 29-July 03

Posted 25 March 2005 - 01:15 PM

dman: thank you for posting that! I don't think that is going to help me because there are spaces in the string that I want to replace and there are quotes which makes it difficult.

I am going to try and use sed instead. I think this will be the solution that I need. If anyone though is aquainted with the program and would know how to change:
```SetupSourcePath = "\"
```
to
```SetupSourcePath = "\Prox\"
```

in sed could you please post the code. I will be running it from a cmd file.

or if someone can do it in batch, that would be cool to .

Thanks
]Bonkers[

### #9clavicle

• Advanced Member
• Group: Members
• Posts: 454
• Joined: 28-January 04
• OS:Windows 7 x64
• Country:

Posted 02 April 2005 - 12:13 AM

@dman
Can you please elaborate the exact usage of sed command.
I want to use for replacing the string multi(0)disk(0)rdisk(1)partition(1)\WINXP="Microsoft Windows XP Professional" to multi(0)disk(0)rdisk(1)partition(1)\WINXP="XP Disk 1".

I am really not getting through. Please refer to my earlier post http://www.msfn.org/board/index.php?showtopic=37723&hl=

Thanks!

### #10Dahi

• Member
• Group: Members
• Posts: 146
• Joined: 25-November 03

Posted 02 April 2005 - 12:46 AM

clavicle, on Apr 2 2005, 12:13 AM, said:

I want to use for replacing the string multi(0)disk(0)rdisk(1)partition(1)\WINXP="Microsoft Windows XP Professional" to multi(0)disk(0)rdisk(1)partition(1)\WINXP="XP Disk 1".
<{POST_SNAPBACK}>

You can use BootCfg to modify BOOT.INI
Search for BootCfg in the Unattended forum for more info.

### #11IcemanND

• MSFN Junkie
• Group: Super Moderator
• Posts: 3,266
• Joined: 24-September 03
• OS:Windows 7 x64
• Country:

Posted 02 April 2005 - 12:20 PM

This should work for any text file.
replace "path\textfile" with the path and name of your original file.
replace "originaltext" with the text you want to replace.
replace "newtext" with the text you want instead.

```for /f %%L in (path\textfile) DO (
if "%%L"=="originaltext" (
echo newtext>>path\temp.TMP
) ELSE (
echo %%L>>path\temp.TMP
)
)
copy /Y path\temp.TMP path\textfile
```

### #12jaclaz

• The Finder
• Group: Developers
• Posts: 11,433
• Joined: 23-July 04
• OS:none specified
• Country:

Posted 02 April 2005 - 01:02 PM

Yes, IcemanND snippet is the easiest way, here is a more complete batch (that needs to be modified for your use of course, but that should give you a good starting base:
```@ECHO OFF
rem -------------------------------------------------------------------------
rem appdisk.cmd Script to mount a virtual disk and
rem attach a REDO in RAMdrive
rem by Sanbarrow
rem modified by jaclaz
rem -------------------------------------------------------------------------
echo Mounting Applicationdisk

:: Keep variables local
SETLOCAL

:: Check temp
if "%temp%" == "" goto _err

::HERE THE PATHS MUST BE SET
Set FilePath=\programs\kenkato\
Set Filename=program-disk
Set FileSuffix=-s001
Set FileExtension=.vmdk
Set DestPath=R:\kenkato\

Set FiletoOpen=%systemdrive%%FilePath%%Filename%%FileExtension%
Set DiskFiletoOpen=%systemdrive%%FilePath%%Filename%%Filesuffix%%FileExtension%

:: Check if file/path exists and create it
IF NOT EXIST "%FiletoOpen%" GOTO :Error1
IF NOT EXIST "%DiskFiletoOpen%" GOTO :Error2
IF EXIST "%Destpath%*.exe" del "%Destpath%*.exe"
IF EXIST "%Destpath%*.sys" del "%Destpath%*.sys"
IF EXIST "%Destpath%%Filename%%FileExtension%" del "%Destpath%%Filename%%FileExtension%"
IF NOT EXIST "%Destpath%" md "%Destpath%"
IF EXIST "%systemdrive%%FilePath%vdk.exe" copy "%systemdrive%%FilePath%vdk.exe" "%Destpath%" > nul
IF EXIST "%systemdrive%%FilePath%vdk.sys" copy "%systemdrive%%FilePath%vdk.sys" "%Destpath%" > nul

:Start
:: Search the file line by line
FOR /F "tokens=* delims=" %%A IN ('TYPE %filetoopen%') DO CALL :ParseFILE "%%A"
GOTO:EOF

:ParseFILE
:: Store quoted line in variable
SET Line=%~1
:: Check if this line is the required one to be modified
ECHO.%Line%| FIND /I "%Filename%%Filesuffix%%FileExtension%" > NUL
IF NOT ERRORLEVEL 1 (

FOR /F "tokens=1,2,3,4 delims= " %%A IN ('echo %Line%') DO (

rem UNCOMMENT FOLLOWING TWO LINES IF NEEDED
rem Echo Original line = %Line%
rem Modified Line = %%A %%B %%C "%DiskFiletoOpen%"

ECHO %%A %%B %%C "%DiskFiletoOpen%">>"%Destpath%%Filename%%FileExtension%"
)
)ELSE (
ECHO.%Line%>>"%Destpath%%Filename%%FileExtension%"
)

GOTO:EOF

::Here is the action

::UNCOMMENT FOLLOWING TWO LINES
rem "%Destpath%vdk.exe" START
rem "%Destpath%vdk.exe" OPEN 0 "%Destpath%%Filename%%FileExtension%" /UNDO /P:1 /L:S:

:Error1
ECHO.
ECHO File %FiletoOpen% does not exist!
ECHO.
Goto :EOF

:Error2
ECHO.
ECHO File %DiskFiletoOpen% does not exist!
ECHO.
Goto :EOF

:_err
echo.
echo  No temp variable set...
echo  Try adding a ramdrive...
echo.

:EOF
ENDLOCAL
Echo Program Terminated
```

This one searches and substitutes entire lines, and should be what clavicle needs too.

jaclaz

### #13dman

• Friend of MSFN
• Group: Members
• Posts: 717
• Joined: 01-February 05

Posted 02 April 2005 - 09:11 PM

I made small util to do text replacements that can be called from command line or batch file. It will replace all instances of search with replacement as specified in simple comma delimited script file, case sensitive. Only downside is it requires VFP6 runtime (see readme.txt for link and info). Not well tested, but shouldent be able to hurt anything. Hope it can be useful to someone.
dman

### #14clavicle

• Advanced Member
• Group: Members
• Posts: 454
• Joined: 28-January 04
• OS:Windows 7 x64
• Country:

Posted 02 April 2005 - 09:55 PM

Thanks dman, jaclaz, IcemanND!

I am already off to get the work done the way suggested by you all and I shall post the results soon.

### #15clavicle

• Advanced Member
• Group: Members
• Posts: 454
• Joined: 28-January 04
• OS:Windows 7 x64
• Country:

Posted 02 April 2005 - 10:13 PM

@dman

Downloaded VFP6 runtimes, and carried out the instructions and got the results that I wanted. Big thanks!! It works!

@jaclaz
I am on my way to edit the cmd. I hope it works! Any instructions further. Thanks again!

### #16dman

• Friend of MSFN
• Group: Members
• Posts: 717
• Joined: 01-February 05

Posted 02 April 2005 - 10:36 PM

@clavicle
yay! that's what I like to hear. Glad I could help

### #17jaclaz

• The Finder
• Group: Developers
• Posts: 11,433
• Joined: 23-July 04
• OS:none specified
• Country:

Posted 03 April 2005 - 08:09 AM

@clavicle
yep, if you have problems, just post here the batch you made and I'll see what I can do.

jaclaz

### #18gareththegod

• Member
• Group: Members
• Posts: 113
• Joined: 28-September 03

Posted 02 May 2005 - 04:07 PM

IcemanND, on Apr 2 2005, 06:20 PM, said:

This should work for any text file.
replace "path\textfile" with the path and name of your original file.
replace "originaltext" with the text you want to replace.
replace "newtext" with the text you want instead.

```for /f %%L in (path\textfile) DO (
if "%%L"=="originaltext" (
echo newtext>>path\temp.TMP
) ELSE (
echo %%L>>path\temp.TMP
)
)
copy /Y path\temp.TMP path\textfile
```

<{POST_SNAPBACK}>

I have tried to make a batch file that searches an extracted help file webpage rename it to a text. Then do a find and replace the links in the string. CMD file is called 2 in the attached zip file.

```@echo off
Setlocal
set NAME=ntcmds
set FOLDER=cmds

echo Deleting old folder if exists.
IF EXIST %NAME% RD /S /Q %NAME%

Echo Decompiling %NAME% Help file
hh.exe -decompile .\%NAME%\ %WINDIR%\Help\%NAME%.chm

Echo Moving all other files to %FOLDER%
md %NAME%\%FOLDER%
cd %NAME%
move *.* %FOLDER%
Move .\%FOLDER%\%NAME%.htm .\%NAME%.htm

ren %NAME%.htm %NAME%.txt
find "MS-ITS:%NAME%.chm::/" %NAME%.txt /c
```

The last line of the code is what I want to find and replace. I Then removed it and added see below.

I wanted to change the links in the webpage to see the wepages in the folder below it.

```for /f %%L in (%NAME%.txt) DO (
if "%%L"=="MS-ITS:%NAME%.chm::/" (
echo %FOLDER%\>>.\temp.TMP
) ELSE (
echo %%L>>.\temp.TMP
)
)
copy /Y temp.TMP %NAME%.txt
```

I used the code above to changed the find and replace to be what I want to replace etc.
find = MS-ITS:%NAME%.chm::/
Replace with = %FOLDER%\ or cmds\ but prefer to use the varible

But it doesn't work all that is in the temp file is the first few charaters from each line.

I don't know much about the for command and would like help in understanding it.

### #19tittoproject

• Group: Members
• Posts: 1
• Joined: 01-August 05

Posted 01 August 2005 - 06:54 PM

Is it possible to replace some strings contained between two other ones?

For example: BEGINstringEND --> BEGINnew_stringEND

Thanks

### #20Eki

• Group: Members
• Posts: 9
• Joined: 17-March 05

Posted 01 August 2005 - 11:38 PM

• 2 Pages
• 1
• 2

## 2 User(s) are reading this topic0 members, 2 guests, 0 anonymous users

All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy