views:

39

answers:

3

I have some files that contain some of if not all of the following:

Program RxBIN RXPCN RxGroup MemberID

not all files will have all of those headers but the ones that do I will need to replace so it looks like this:

@Program @RxBIN @RXPCN @RxGroup @MemberID

Thanks in Advance, Joe

A: 

Thanks to Rudi I have figured out a way to achieve what I was looking for. But Needs improvements. Will write new question to fix this so I can use it.

jmituzas
+1  A: 

I'd still be inclined to go with a tool built for this purpose: Batch Replacer seems to fit the bill perfectly.

But if you're dead set on it... something using the old repl.bat with a multi arg parser... this becomes a bit of a monster that using the DEBUG command but here's the final masterpiece:

Source code: mmrepl.bat

@ECHO OFF
setLocal EnableDelayedExpansion

::: mmrepl - Replaces one or more strings with others, in file(s)
:::
::: syntax: mmrepl.bat $file $find $replace [$find $replace] ...
:::            $file    [in] - file to be parsed
:::            $find    [in] - string to find
:::            $replace [in] - string to replace with
:::
:::         * $find & $replace should be supplied in pairs, n multiples allowed
:::         * $file can be a single file (eg.txt) or a file filter (*.txt)
:::           you can supply any command that works with 'dir /b $file'
:::           so paths and drives are also valid.
:::         * $find,$replace strings can be single words, or strings enclosed
:::            in quotes (which will be stripped)
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
if not exist %1 echo No files matching %1 found&GOTO:EOF

::Creates the following log file:
set log=%0.log
::Temporarily creates the following files but cleans up
set replscr=TEMPDEBUG.SCR
set cmplsrc=TEMPEDLN.SCR

echo To see the work of %0.bat view the log file %log%
echo Multi-Multi Replacement (%0.bat)>%log%
echo.>>%log%
set "files=%1"
shift
set mmreplcmd=
:: Pair up find/replaces
:strippairs
set p1=%1
set p1=!p1:"=!
set p2=%2
set p2=!p2:"=!
SET mmreplcmd=%mmreplcmd%'1R%p1%' 1A '%p2%' 0D 0A 
echo Replacing "%p1%" with "%p2%" >> %log%
shift
shift
if "%~1" neq "" goto:strippairs

::Build script
echo N%cmplsrc% > %replscr%
echo E CS:100 %mmreplcmd%'E' 0D 0A>> %replscr%
echo RCX >> %replscr%
echo 40 >> %replscr%
echo W >> %replscr%
echo Q >> %replscr%
DEBUG < %replscr% > NUL
::Execute on files
for /f %%a IN ('dir /b %files%') do (
  echo.>>%log%
  echo *** File: %%a >> %log%
  EDLIN %%a < %cmplsrc% >> %log%
)
DEL %replscr%
DEL %cmplsrc%

NOTE: I had to remove the "is contained" within searching of repl.bat because you're adding an ampersand to each title so it's always containing... my testing shows this works fine, but YMMV (test).

Rudu
Wow thanks alot!Works great.
jmituzas
+1  A: 

This can be done trivially with sed (the *s*tream *ed*itor) which is available for Windows in several places.

It could be done with something like

sed -e "s/\(Program\|RxBIN\|RXPCN\|RxGroup\|MemberID\)/@\1/g" myfile.txt > newfile.txt

This is a regular expression that basically says "substitute for the word 'Program' or 'RxBIN' or 'RXPCN' or 'RxGroup' or 'MemberID', an at-sign and whatever word was matched."

There are a lot of great Unix commands which have been ported to Windows that can be extremely helpful.

Stephen P
Unix-originated tools are usually the best option in these cases, especially if you dont want to "roll-your-own" with Java or C#.
Mondain