Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Text Replacement Using A Batch

- - - - -

  • Please log in to reply
20 replies to this topic

#1
fillalph

fillalph

    Advanced Member

  • Member
  • PipPipPip
  • 324 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:
Attached File  Untitled_1.gif   3KB   79 downloads
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[
Antec Solo ; Corsair HX520 ; Gigabyte P35-DS3R (bios F4) ; Intel Core 2 Duo E6550 @ 2.33GHz w/ ThermalRight Ultra 120 (fanless) ; OCZ Platinum XTC REV.2 PC2-6400 2GB @ 4-4-4-15 ; Western Digital SE16 320GB ; Maxtor DiamondMax 10 250GB ; XFX 7600GS ; Pioneer DVR-212D ; Win XP Pro SP2 (reduced with nLite) ; Dell 2007WFP (withS-IPS panel)


How to remove advertisement from MSFN

#2
dman

dman

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 717 posts
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

#3
fillalph

fillalph

    Advanced Member

  • Member
  • PipPipPip
  • 324 posts
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[
Antec Solo ; Corsair HX520 ; Gigabyte P35-DS3R (bios F4) ; Intel Core 2 Duo E6550 @ 2.33GHz w/ ThermalRight Ultra 120 (fanless) ; OCZ Platinum XTC REV.2 PC2-6400 2GB @ 4-4-4-15 ; Western Digital SE16 320GB ; Maxtor DiamondMax 10 250GB ; XFX 7600GS ; Pioneer DVR-212D ; Win XP Pro SP2 (reduced with nLite) ; Dell 2007WFP (withS-IPS panel)

#4
clavicle

clavicle

    Advanced Member

  • Member
  • PipPipPip
  • 454 posts
  • OS:Windows 7 x64
  • Country: Country Flag
Thanks dman. Earlier about I was searching for a help for a similar type but got no response. (Refer http://www.msfn.org/...topic=37723&hl= ) Post still is unanswered. But I found munge on the net. But I think sed is still a better option. Thanks again!

#5
fillalph

fillalph

    Advanced Member

  • Member
  • PipPipPip
  • 324 posts
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[
Antec Solo ; Corsair HX520 ; Gigabyte P35-DS3R (bios F4) ; Intel Core 2 Duo E6550 @ 2.33GHz w/ ThermalRight Ultra 120 (fanless) ; OCZ Platinum XTC REV.2 PC2-6400 2GB @ 4-4-4-15 ; Western Digital SE16 320GB ; Maxtor DiamondMax 10 250GB ; XFX 7600GS ; Pioneer DVR-212D ; Win XP Pro SP2 (reduced with nLite) ; Dell 2007WFP (withS-IPS panel)

#6
fillalph

fillalph

    Advanced Member

  • Member
  • PipPipPip
  • 324 posts
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[
Antec Solo ; Corsair HX520 ; Gigabyte P35-DS3R (bios F4) ; Intel Core 2 Duo E6550 @ 2.33GHz w/ ThermalRight Ultra 120 (fanless) ; OCZ Platinum XTC REV.2 PC2-6400 2GB @ 4-4-4-15 ; Western Digital SE16 320GB ; Maxtor DiamondMax 10 250GB ; XFX 7600GS ; Pioneer DVR-212D ; Win XP Pro SP2 (reduced with nLite) ; Dell 2007WFP (withS-IPS panel)

#7
dman

dman

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 717 posts
Munge.exe

Attached Files



#8
fillalph

fillalph

    Advanced Member

  • Member
  • PipPipPip
  • 324 posts
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[
Antec Solo ; Corsair HX520 ; Gigabyte P35-DS3R (bios F4) ; Intel Core 2 Duo E6550 @ 2.33GHz w/ ThermalRight Ultra 120 (fanless) ; OCZ Platinum XTC REV.2 PC2-6400 2GB @ 4-4-4-15 ; Western Digital SE16 320GB ; Maxtor DiamondMax 10 250GB ; XFX 7600GS ; Pioneer DVR-212D ; Win XP Pro SP2 (reduced with nLite) ; Dell 2007WFP (withS-IPS panel)

#9
clavicle

clavicle

    Advanced Member

  • Member
  • PipPipPip
  • 454 posts
  • OS:Windows 7 x64
  • Country: Country Flag
@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/...topic=37723&hl=

Thanks! :)

#10
Dahi

Dahi

    Member

  • Member
  • PipPip
  • 146 posts

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.

#11
IcemanND

IcemanND

    MSFN Junkie

  • Super Moderator
  • 3,266 posts
  • OS:Windows 7 x64
  • Country: Country Flag
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


#12
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,266 posts
  • OS:none specified
  • Country: Country Flag
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

#13
dman

dman

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 717 posts
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

Attached Files



#14
clavicle

clavicle

    Advanced Member

  • Member
  • PipPipPip
  • 454 posts
  • OS:Windows 7 x64
  • Country: Country Flag
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.

#15
clavicle

clavicle

    Advanced Member

  • Member
  • PipPipPip
  • 454 posts
  • OS:Windows 7 x64
  • Country: Country Flag
@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:

#16
dman

dman

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 717 posts
@clavicle
yay! :thumbup that's what I like to hear. Glad I could help

#17
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,266 posts
  • OS:none specified
  • Country: Country Flag
@clavicle
yep, if you have problems, just post here the batch you made and I'll see what I can do.

jaclaz

#18
gareththegod

gareththegod

    Member

  • Member
  • PipPip
  • 113 posts

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.

#19
tittoproject

tittoproject
  • Member
  • 1 posts
Is it possible to replace some strings contained between two other ones?

For example: BEGINstringEND --> BEGINnew_stringEND

Thanks

#20
Eki

Eki
  • Member
  • 9 posts
How Can I Find and Replace Text in a Text File?

#21
akira6968

akira6968
  • Member
  • 1 posts
  • OS:Windows 7 x86
  • Country: Country Flag
Found that the link for the VFP6 Runtimes is broken (for dman's utility).... had a hard time finding VFP6 runtimes....not sure if it HAS to be 6, but just in case, here is a link to VFP6-9 runtime installers I found.

http://code.msdn.mic...x?ReleaseId=125




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN