I want to change a version number in 4 text files. The 4 files are
not the same, but the changes will be the same. For example. I want
to change "version 1.0" to "version 1.1". The files do have more
information than this and that information should remain unchanged,
and the location of version 1.0 is not always the same in the file.
Is it possible to do this for all the files at once? If it is not,
how would u do it for one file at a time?
Thank you in advance
>I want to change a version number in 4 text files.
Call the following using wildcards:
(based on http://www.dostips.com/?t=Batch.FindAndReplace )
x--- snip ---x
@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%*"=="" goto USAGE
if "%4" == "" goto BEGIN
if "%4" == "FOOBAR" goto FOOBAR
goto END
:USAGE
::BS - parses a File line by line and replaces a substring"
::syntax: BS.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
if "%*"=="" findstr "^::" "%~f0"&GOTO:EOF
:BEGIN
for %%f in (%3) do call %0 %1 %2 %%f FOOBAR
goto END
:FOOBAR
if exist FOOBAR.txt del FOOBAR.txt
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X >> FOOBAR.txt
) ELSE echo. >> FOOBAR.TXT
)
copy FOOBAR.txt %3>NUL
del FOOBAR.txt
:END
I'd use gsar (general search and replace -
<http://gnuwin32.sourceforge.net/packages/gsar.htm>), or an awk of sed
script. Being me, I'd actually do it with gawk:
for %A in (file list) do ( gawk "{gsub(/version 1.0/, \"version
1.1\");print $0}" %A > other_directory\%A)
--
T.E.D. (tda...@mst.edu)
Sorry but I do not know what wildcards are. I am a complete noob for
creating batch files.
I ran the code that you have generously provided. with
BatchSubstitude.bat inside outside "File\test.txt"
and it works for test.txt
I do not understand how to work with the other files as well?
test2.txt is in the same Folder.
I know i can call
BatchSubstitude.bat inside outside "File\test2.txt"
but is there any way to do both of the files at once?
>I'd use gsar (general search and replace)
I'd use sed too Ted, or ed even not being you, but the batch I gave him will
work just fine and he is a newbie so getting together sed
"s/findthis/replaceitwiththis/g" myoldfile > mynewfile might be too nixy.
Definitely not msdos but cygwin is my friend too.
Bill
If you use the batch I provided make sure you call it bs.bat
"wildcards" are abbreviations for grouping similar files together. The
asterisk is often used.
For example, if you wanted to replace the same string in a group of files
called test1.txt, test2.txt and test3.txt you could use the following:
BS inside outside test*.txt
You can see how this autocompletes the filename. Another method is to use a
"joker". The question mark is often used. But this method can only be used
for a single character.
BS inside outside test?.txt
And that was my simple explanation.
Bill
> "Ted Davis" <tda...@mst.edu> wrote:
> Bill
*** SED is available for DOS. Another option is MiniTrue:
MT FILES - STRING = REPLACEMENT
One or more links to websites
for the referred program(s)
can be found at:
http://www.chebucto.ca/~ak621/DOS/Websites.html
Richard Bonner
http://www.chebucto.ca/~ak621/DOS/
> Another option is MiniTrue:
>
>MT FILES - STRING = REPLACEMENT
ISTM that MT is the 16-bit version, for which the - should be /.
Or that, using -, the (better) 32-bit version, MTR, is needed.
MTR will do all sorts of viewing/searching/altering files.
However, while I could use MTR interactively in a Win98 command prompt
of some sort, and can use MT interactively in a WinXP command prompt of
either sort, I cannot get MTR keyboard input OK in WinXP sp2/3 CMD.EXE,
although non-interactive use is fine. Any suggestions?
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 7.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> unsupported.
On Jul 9, 7:02 pm, ak...@chebucto.ns.ca (Richard Bonner) wrote:
>
> *** SED is available for DOS. Another option is MiniTrue:
The DJGPP v2 port supports LFNs and can do in-place editing (well,
close enough).
Example: sed -i "/version 1\.0/s/0/1/" my_file.txt
http://gd.tuwien.ac.at/pc/dos/djgpp/beta/v2gnu/sed42b.zip
http://gd.tuwien.ac.at/pc/dos/djgpp/current/v2gnu/sed42s.zip
> Richard Bonner posted:
> > Another option is MiniTrue:
> >
> >MT FILES - STRING = REPLACEMENT
> ISTM that MT is the 16-bit version, for which the - should be /.
> Or that, using -, the (better) 32-bit version, MTR, is needed.
*** Oops - thanks, John. Yes, your syntax is correct.
> MTR will do all sorts of viewing/searching/altering files.
> --
> (c) John Stockton
*** It sure will. I must thank you for turning me on a number of years
ago to this wonderful DOS tool.
Richard Bonner
http://www.chebucto.ca/~ak621/DOS/