Jump to content

Text Replacement Using A Batch


fillalph

Recommended Posts

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:

post-5288-1111712465_thumb.gif

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[

Link to comment
Share on other sites


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.northpark.edu/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

Link to comment
Share on other sites

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[

Link to comment
Share on other sites

@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! :)

Link to comment
Share on other sites

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

You can use BootCfg to modify BOOT.INI

Search for BootCfg in the Unattended forum for more info.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

dfindrep.zip

Link to comment
Share on other sites

@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! :lol:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...