March 25, 200521 yr 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: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[
March 25, 200521 yr 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 SEDhttp://www.ss64.com/nt/munge.html
March 25, 200521 yr Author 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[
March 25, 200521 yr 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!
March 25, 200521 yr Author 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[
March 25, 200521 yr Author 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[
March 25, 200521 yr Author 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[
April 2, 200521 yr @dmanCan 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!
April 2, 200521 yr 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.
April 2, 200521 yr 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
April 2, 200521 yr 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 OFFrem -------------------------------------------------------------------------rem appdisk.cmd Script to mount a virtual disk andrem attach a REDO in RAMdriverem by Sanbarrowrem modified by jaclazrem -------------------------------------------------------------------------echo Mounting Applicationdisk:: Keep variables localSETLOCAL:: Check temp if "%temp%" == "" goto _err::HERE THE PATHS MUST BE SETSet FilePath=\programs\kenkato\Set Filename=program-diskSet FileSuffix=-s001Set FileExtension=.vmdkSet DestPath=R:\kenkato\Set FiletoOpen=%systemdrive%%FilePath%%Filename%%FileExtension%Set DiskFiletoOpen=%systemdrive%%FilePath%%Filename%%Filesuffix%%FileExtension%:: Check if file/path exists and create itIF NOT EXIST "%FiletoOpen%" GOTO :Error1IF NOT EXIST "%DiskFiletoOpen%" GOTO :Error2IF 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%" > nulIF EXIST "%systemdrive%%FilePath%vdk.sys" copy "%systemdrive%%FilePath%vdk.sys" "%Destpath%" > nul:Start:: Search the file line by lineFOR /F "tokens=* delims=" %%A IN ('TYPE %filetoopen%') DO CALL :ParseFILE "%%A"GOTO:EOF:ParseFILE:: Store quoted line in variableSET Line=%~1:: Check if this line is the required one to be modifiedECHO.%Line%| FIND /I "%Filename%%Filesuffix%%FileExtension%" > NULIF NOT ERRORLEVEL 1 (FOR /F "tokens=1,2,3,4 delims= " %%A IN ('echo %Line%') DO (rem UNCOMMENT FOLLOWING TWO LINES IF NEEDEDrem 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 LINESrem "%Destpath%vdk.exe" START rem "%Destpath%vdk.exe" OPEN 0 "%Destpath%%Filename%%FileExtension%" /UNDO /P:1 /L:S: :Error1ECHO.ECHO File %FiletoOpen% does not exist!ECHO.Goto :EOF:Error2ECHO.ECHO File %DiskFiletoOpen% does not exist!ECHO.Goto :EOF:_errecho.echo No temp variable set...echo Try adding a ramdrive...echo.:EOFENDLOCALEcho Program TerminatedThis one searches and substitutes entire lines, and should be what clavicle needs too.jaclaz
April 3, 200521 yr 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.dmandfindrep.zip
April 3, 200521 yr 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.
April 3, 200521 yr @dmanDownloaded VFP6 runtimes, and carried out the instructions and got the results that I wanted. Big thanks!! It works!@jaclazI am on my way to edit the cmd. I hope it works! Any instructions further. Thanks again!
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now