Replace text string using batch file
Asked by qvhit in MS DOS
Tags: replace, string, batch, file, text
Previously I recieved help from SteveGTR to create the batch file below which changes the UserName variable into a different format then calls on a standard PRF file and creates a new one with the correct UserName format. The problem was that the batch file
replaced the Value for MailboxName in each section and not just in section 4 where it was needed. So the only way I can think to correct this issue is to make the MailboxName in section 4 unique and for the batch file to look for that instead.
So I changed the PRF (in section 4) and the batch file from MailboxName to MailboxNameChange and it has the desired effect. Problem is that I now need the same batch file to look for anything in the output file by the name of MailboxNameChange and convert it back to MailboxName so that I can run the PRF correctly.
Thanks
@echo off
setlocal enabledelayedexpansion
set un=%~1
if "%un%"=="" set un=%username%
set /a pos=1
:NEXTCHAR
if "!un:~%pos%,1!"=="" echo Parsing error&goto :EOF
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if "!un:~%pos%,1!"=="%%a" (
set /a pos+=1
goto NEXTCHAR
)
)
set /a pos-=1
set MailboxNameChange=!un:~%po s%! %un:~0,1%
REM ** Change this part
set inFile="c:/prf/v1.prf"
set outFile="C:/prf/V1a.prf"
set workFile=" temp .txt"
>%workFile% find /N /V "_STM_STM_" %inFile%
if exist %outFile% del %outFile% >NUL
set /a lineCnt=0
for /f "skip=2 tokens=1,2 delims==" %%a in ('type %workFile% 2^>NUL') do call :PROCESS "%%a" "%%b"
if exist %workFile% del %workFIle% >NUL
echo Output in %outFile%
goto :EOF
:STRIPLINENUMBER
set str=%~1
:SLN_NEXTCHAR
if "%str:~0,1%"=="]" goto GOTLINE
set str=%str:~1%
goto SLN_NEXTCHAR
:GOTLINE
set str=%str:~1%
goto :EOF
:PROCESS
set /a lineCnt+=1
echo Processing line %lineCnt%
if "%~2"=="" goto NOVALUE
set valuePart=%~2
for /f "tokens=2 delims=] " %%a in ('echo %~1 2^>NUL') do if "%%a"=="MailboxNameChange" set valuePart=%mailboxnamechan
ge%
CALL :STRIPLINENUMBER "%~1"
>>%outFile% echo %str%=%valuePart%
goto :EOF
:NOVALUE
call :STRIPLINENUMBER "%~1"
if "%str%"=="" (
echo.>>%outFile%
goto :EOF
)
>>%outFile% echo %str%
So I changed the PRF (in section 4) and the batch file from MailboxName to MailboxNameChange and it has the desired effect. Problem is that I now need the same batch file to look for anything in the output file by the name of MailboxNameChange and convert it back to MailboxName so that I can run the PRF correctly.
Thanks
@echo off
setlocal enabledelayedexpansion
set un=%~1
if "%un%"=="" set un=%username%
set /a pos=1
:NEXTCHAR
if "!un:~%pos%,1!"=="" echo Parsing error&goto :EOF
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if "!un:~%pos%,1!"=="%%a" (
set /a pos+=1
goto NEXTCHAR
)
)
set /a pos-=1
set MailboxNameChange=!un:~%po
REM ** Change this part
set inFile="c:/prf/v1.prf"
set outFile="C:/prf/V1a.prf"
set workFile=" temp .txt"
>%workFile% find /N /V "_STM_STM_" %inFile%
if exist %outFile% del %outFile% >NUL
set /a lineCnt=0
for /f "skip=2 tokens=1,2 delims==" %%a in ('type %workFile% 2^>NUL') do call :PROCESS "%%a" "%%b"
if exist %workFile% del %workFIle% >NUL
echo Output in %outFile%
goto :EOF
:STRIPLINENUMBER
set str=%~1
:SLN_NEXTCHAR
if "%str:~0,1%"=="]" goto GOTLINE
set str=%str:~1%
goto SLN_NEXTCHAR
:GOTLINE
set str=%str:~1%
goto :EOF
:PROCESS
set /a lineCnt+=1
echo Processing line %lineCnt%
if "%~2"=="" goto NOVALUE
set valuePart=%~2
for /f "tokens=2 delims=] " %%a in ('echo %~1 2^>NUL') do if "%%a"=="MailboxNameChange"
CALL :STRIPLINENUMBER "%~1"
>>%outFile% echo %str%=%valuePart%
goto :EOF
:NOVALUE
call :STRIPLINENUMBER "%~1"
if "%str%"=="" (
echo.>>%outFile%
goto :EOF
)
>>%outFile% echo %str%