• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
Sign in to follow this  
Followers 0
fillalph

Text Replacement Using A Batch

21 posts in this topic

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[

0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

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[

0

Share this post


Link to post
Share on other sites

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[

0

Share this post


Link to post
Share on other sites

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[

0

Share this post


Link to post
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[

0

Share this post


Link to post
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! :)

0

Share this post


Link to post
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.

0

Share this post


Link to post
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

0

Share this post


Link to post
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

0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
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:

0

Share this post


Link to post
Share on other sites

@clavicle

yay! :thumbup that's what I like to hear. Glad I could help

0

Share this post


Link to post
Share on other sites

@clavicle

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

jaclaz

0

Share this post


Link to post
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

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.

0

Share this post


Link to post
Share on other sites

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

For example: BEGINstringEND --> BEGINnew_stringEND

Thanks

0

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.