Question

Replace text string using batch file

Asked by: qvhit

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:~%pos%! %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=%mailboxnamechange%

CALL :STRIPLINENUMBER "%~1"

>>%outFile% echo %str%=%valuePart%

goto :EOF

:NOVALUE

call :STRIPLINENUMBER "%~1"

if "%str%"=="" (
  echo.>>%outFile%
  goto :EOF
)

>>%outFile% echo %str%

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-12-20 at 07:47:56ID21670668
Tags

replace

,

string

,

batch

,

file

,

text

Topic

MS DOS

Participating Experts
1
Points
400
Comments
9

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Search String Batch file
    How do I create a batch file where it will search the last modified file in about 10 directories and then copies the latest file to a different location? It will be some type of search string.
  2. Repeat batch until user presses a key
    I have the following batch file which searches for a file then copies it into the root folder: @echo off setlocal :START REM Set variable name filename to prompt result. set /p fileName=Enter filename? if "%fileName%"=="" goto :EOF REM Set expression cnt...
  3. What does this @rem mean?
    Hi At the top of a perl script, I see this @rem. Looks like comments, but I know comments start with #. So what does this mean? Thanks. Jirong @rem = ' PERL for Windows NT -- ccperl must be in search path @echo off ccperl %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl @rem '...
  4. Batch File
    Hi... I'm trying to create a batch file that is supposed to check a directory for specific file extensions and then delete them or move them to a different folder. After that it is supposed to send a message to the administrator or send an email to the administrator. Is t...
  5. Dos batch file - dealing with a nul value
    I am using a batch file in windows xp. I am trying to create a continuous string with date and time stamp in it. I can do it with the following command: set archivestamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%%TIME:~3,2% the only catch is that if it is before 10 am i...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: SteveGTRPosted on 2005-12-20 at 07:51:10ID: 15519176

We could just change the previous batch processing to only change the specific entry in the section you specify.

 

by: SteveGTRPosted on 2005-12-20 at 08:09:07ID: 15519336

Here's a revised copy of the original that only changes the MailboxName entry in section 4:

@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 mailboxName=!un:~%pos%! %un:~0,1%

REM ** Change this part
set inFile="C:\temp\temp\V1.prf"
set outFile="C:\temp\temp\V1a.prf"
set workFile="temp.txt"

>%workFile% find /N /V "_STM_STM_" %inFile%

if exist %outFile% del %outFile% >NUL

set /a lineCnt=0

set inSection4=

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

:SETSECTION

if %1==4 (
  set inSection4=Y
) else (
  set inSection4=
)

goto :EOF

:PROCESS

set /a lineCnt+=1

echo Processing line %lineCnt%

if "%~2"=="" goto NOVALUE

set valuePart=%~2

if "%inSection4%"=="Y" for /f "tokens=2 delims=] " %%a in ('echo %~1 2^>NUL') do if "%%a"=="MailboxName" set valuePart=%mailboxName%

CALL :STRIPLINENUMBER "%~1"

>>%outFile% echo %str%=%valuePart%

goto :EOF

:NOVALUE

for /f "tokens=2,3" %%a in ('echo %~1 2^>NUL') do if "%%a"=="Section" call :SETSECTION "%%b"

call :STRIPLINENUMBER "%~1"

if "%str%"=="" (
  echo.>>%outFile%
  goto :EOF
)

>>%outFile% echo %str%

Good Luck,
Steve

 

by: qvhitPosted on 2005-12-20 at 08:22:22ID: 15519465

Cheers for that Steve, wasn't sure I would get a repsonse from you so was just trying to make it simple for whoever picked it up.

The new version seems to change just section 4 fine but it doesn't convert JBloggs into Bloggs J anymore.  Coming back with JBloggs still.

Also out of interest when the file runs it shows which line it's processing.  Is it possible to get it to stay on one line and count up instead of showing a new one for each?

Obvioulsy not the most important thing so no worries if you can't!  If you can then I shall raise the value of the question.

Neil

 

by: SteveGTRPosted on 2005-12-20 at 08:41:22ID: 15519654

Bug in new code. I didn't have quotes around the 4 in SETSECTION:

@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 mailboxName=!un:~%pos%! %un:~0,1%

REM ** Change this part
set inFile="C:\temp\temp\V1.prf"
set outFile="C:\temp\temp\V1a.prf"
set workFile="temp.txt"

>%workFile% find /N /V "_STM_STM_" %inFile%

if exist %outFile% del %outFile% >NUL

set /a lineCnt=0

set inSection4=

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

:SETSECTION

if %1=="4" (
  set inSection4=Y
) else (
  set inSection4=
)

goto :EOF

:PROCESS

set /a lineCnt+=1

echo Processing line %lineCnt%

if "%~2"=="" goto NOVALUE

set valuePart=%~2

if "%inSection4%"=="Y" for /f "tokens=2 delims=] " %%a in ('echo %~1 2^>NUL') do if "%%a"=="MailboxName" set valuePart=%mailboxName%

CALL :STRIPLINENUMBER "%~1"

>>%outFile% echo %str%=%valuePart%

goto :EOF

:NOVALUE

for /f "tokens=2,3" %%a in ('echo %~1 2^>NUL') do if "%%a"=="Section" call :SETSECTION "%%b"

call :STRIPLINENUMBER "%~1"

if "%str%"=="" (
  echo.>>%outFile%
  goto :EOF
)

>>%outFile% echo %str%

As for the other stuff, it is possible, but would require more processing and take longer.

 

by: SteveGTRPosted on 2005-12-20 at 10:56:19ID: 15520876

Here's an update with the one line progress information:

@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 mailboxName=!un:~%pos%! %un:~0,1%

REM ** Change this part
set inFile="C:\temp\temp\V1.prf"
set outFile="C:\temp\temp\V1a.prf"
set workFile="temp.txt"

>%workFile% find /N /V "_STM_STM_" %inFile%

if exist %outFile% del %outFile% >NUL

set /a lineCnt=0

set inSection4=

:: *** In the next line, the variable BS needs to be set to a backspace.
:: *** To do this, open the script in edit ("edit progressclock.cmd" in a command window).
:: *** Position the cursor directly after "BS="; enter <Ctrl-P>; in the status bar at the bottom,
:: *** it will display something like "Enter the control sequence to be inserted"; now hit <BackSpace>
:: *** or <Ctrl-H>; a square white symbol with a circle should appear. Save the file and quit edit.
set BS=

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.
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

:SETSECTION

if %1=="4" (
  set inSection4=Y
) else (
  set inSection4=
)

goto :EOF

:PROCESS

set /a lineCnt+=1

<NUL set /p Dummy=%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%Processing line %lineCnt%

REM echo Processing line %lineCnt%

if "%~2"=="" goto NOVALUE

set valuePart=%~2

if "%inSection4%"=="Y" for /f "tokens=2 delims=] " %%a in ('echo %~1 2^>NUL') do if "%%a"=="MailboxName" set valuePart=%mailboxName%

CALL :STRIPLINENUMBER "%~1"

>>%outFile% echo %str%=%valuePart%

goto :EOF

:NOVALUE

for /f "tokens=2,3" %%a in ('echo %~1 2^>NUL') do if "%%a"=="Section" call :SETSECTION "%%b"

call :STRIPLINENUMBER "%~1"

if "%str%"=="" (
  echo.>>%outFile%
  goto :EOF
)

>>%outFile% echo %str%

 

by: SteveGTRPosted on 2005-12-20 at 10:57:05ID: 15520881

Doesn't take any longer. For some reason I read your original request incorrectly and thought you wanted to count down to 0.

 

by: qvhitPosted on 2005-12-22 at 03:38:11ID: 15533225

Steve that has worked perfectly and this time I actually tested it properly (I think!!).

As you probably know by now this is all because we are migrating from windows NT to XP and we need a quick and easy way to be able to create new user profiles.  Along with this section that creates the PRF file I have a part in my logon script that adds a registry key to say run the newly created PRF file once.  My issue now is that because it runs the registry key each time the user logs on it looks to run the PRF file again and therefore resetting any changes the user may have made.  Do you have any ideas how I might get round this?

I don't know if it possible to send a standard e-mail once the user logs on to a generic account that my colleagues and I look at to remind us to now remove them from the logon script or......

If through Regedit you can ask it to look for an exsisting key.  If it's there then it doesn't need to run?

Alternatively if it's possible to get the batch file to check for a particular file on the users home directory before it runs the registry key and then afterwards to create the file so next time it doesn't need to run it?

If you can help with this I'll up the points to 750.

Cheers,

Neil

 

by: SteveGTRPosted on 2005-12-22 at 05:45:09ID: 15533760

There are a number of ways to accomplish the task you request. One would be to scan the registry and see if the values are there and skip this processing. This would take a little time. This has been asked a number of times on EE:

http://www.experts-exchange.com/Programming/Q_21010073.html

You could create a file with the username and place it on a common network share. Then you could check to see if that file exists and skip the processing. I like this method.

if exist "\\server\share\InstallCheck\%username%.txt" goto SKIPPRFCONVERT

REM ** PRF Convert processing

echo Done>"\\server\share\InstallCheck\%username%.txt"

:SKIPPRFCONVERT

REM ** Continue processing

 

by: qvhitPosted on 2005-12-28 at 00:58:35ID: 15561312

Thanks for that Steve,

I actually solved it myself about 10 mins before you replied.  Thank you all the same

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...