Petri IT Knowledgebase Forums
 

Petri.co.il forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > DOS Command Shell
Petri.co.il is happy to award auglan the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read

Notices

.bat Find and replace text

.bat Find and replace text

this thread has 6 replies and has been viewed 3947 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 31st August 2010, 16:35
jmituzas jmituzas is offline
Casual
Casual
 
 Join Date: Aug 2010
  6 month star 12 month star
 Posts: 6
 Reputation: jmituzas is on a distinguished road (10)
Default .bat Find and replace text

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
  #2  
Old 31st August 2010, 21:02
Ossian Ossian is online now
Administrator
 
 Join Date: Nov 2003
  6 month star 12 month star
 Location: Bonnie Scotland
 Posts: 15,263
  Send a message via Skype™ to Ossian
 Reputation: Ossian has much to be proud ofOssian has much to be proud ofOssian has much to be proud ofOssian has much to be proud ofOssian has much to be proud ofOssian has much to be proud ofOssian has much to be proud ofOssian has much to be proud ofOssian has much to be proud ofOssian has much to be proud of (1326)
Default Re: .bat Find and replace text

If it is a word document, you can write a macro to do it -- you do not have to use a bat file. if it is some other format, please give us a clue!
__________________
Tom Jones
MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
PhD, MSc, FIAP, MIITT
IT Trainer / Consultant
Ossian Ltd
Scotland

** Remember to give credit where credit is due and leave reputation points where appropriate **
  #3  
Old 31st August 2010, 21:19
jmituzas jmituzas is offline
Casual
Casual
 
 Join Date: Aug 2010
  6 month star 12 month star
 Posts: 6
 Reputation: jmituzas is on a distinguished road (10)
Default Re: .bat Find and replace text

Solved

I found the solution http://www.dostips.com/?t=Batch.FindAndReplace

had to rewite it a bit to actually get the job done.

I am sure there is an easier way of doing this but I got it to work

call BatchSubstitute.bat Program @Program InDesignData.txt >newfile.txt

Code:
if not exist newfile.txt goto skip
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
:skip

call BatchSubstitute.bat RxBIN @RxBIN InDesignData.txt >newfile.txt 
if not exist newfile.txt goto skip
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
:skip

call BatchSubstitute.bat RXPCN @RXPCN InDesignData.txt >newfile.txt 
if not exist newfile.txt goto skip
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
:skip

call BatchSubstitute.bat RxGroup @RxGroup InDesignData.txt >newfile.txt 
if not exist newfile.txt goto skip
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
:skip

call BatchSubstitute.bat MemberID @MemberID InDesignData.txt >newfile.txt 
if not exist newfile.txt goto skip
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
:skip

Last edited by biggles77; 7th September 2010 at 15:58.. Reason: Wrap script in CODE tag
  #4  
Old 7th September 2010, 16:01
biggles77's Avatar
biggles77 biggles77 is offline
Administrator
 
 Join Date: Dec 2003
  6 month star 12 month star
 Location: Nowhere that I like.
 Posts: 10,769
 Reputation: biggles77 is a splendid one to beholdbiggles77 is a splendid one to beholdbiggles77 is a splendid one to beholdbiggles77 is a splendid one to beholdbiggles77 is a splendid one to beholdbiggles77 is a splendid one to beholdbiggles77 is a splendid one to behold (738)
Default Re: .bat Find and replace text

Thanks for posting back with your find.
__________________
"There I stood at the bar, wearing a Mae West, no jacket, and beginning to leak blood from my torn boot. None of the golfers took any notice of me - after all, I wasn't a member!" Kenneth Lee - after being shot down during the Battle of Britain on the 18th August 1940.

************************************************** **********************
** Remember to give credit where credit is due and leave reputation points where appropriate **
************************************************** **********************
  #5  
Old 19th September 2010, 02:56
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,282
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (903)
Default Re: .bat Find and replace text

Names of the labels that are used in a batch must be unique. Your batch is using :skip multiple times and therefore the batch won't work as expected.
Further more, your batch will add an "@" also if there was already a leading @ added to the header text.

A good command line utility for text processing that I would recommend is gnu sed for Windows (free for download)!

But if you don't like to use tools (like sed or gawk) - you can try this "all in one batch" solution,
Code:
@echo off & SETLOCAL ENABLEEXTENSIONS

Set "file=InDesignData.txt"

color 6A & echo please wait.. & title parsing file, "%file%"

call:BatchSubstitude "%file%" > "newfile.txt"

MOVE /Y "newfile.txt" "InDesignData.txt"

goto:EOF
:BatchSubstitude *** source http://www.dostips.com/?t=Batch.FindAndReplace
   SETLOCAL DISABLEDELAYEDEXPANSION
   for /f "tokens=1,* delims=]" %%A in ('"type "%~1"|find /n /v """') do (
     set "line=%%B"
     if defined line (
         call:replace "Program" "@Program"
         call:replace "RxBIN" "@RxBIN"
         call:replace "RXPCN" "@RXPCN"
         call:replace "RxGroup" "@RxGroup"
         call:replace "MemberID" "@MemberID"

         call set "line=echo.%%line%%"
         for /f "delims=" %%* in ('"echo."%%line%%""') do (%%~*)
     ) ELSE (echo.))
   ENDLOCAL
   exit /b 0
   :replace
   >nul 2>&1 (
   call set line |Findstr.exe /RIC:"\<%~1\>" && call set line |Findstr.exe /RIVC:"\<@%~1\>" && call set "line=%%line:%~1=%~2%%")
exit /b 0
Instead of defining a single file (InDesignData.txt) it would also be possible to modify the batch so it loops through all files of a particular folder (or directory).

\Rems
__________________

This posting is provided "AS IS" with no warranties, and confers no rights.

__________________

** Remember to give credit where credit's due **
and leave Reputation Points for meaningful posts
  #6  
Old 22nd September 2010, 18:35
imag1nat1on's Avatar
imag1nat1on imag1nat1on is offline
Casual
Casual
 
 Join Date: Aug 2010
  6 month star 12 month star
 Location: Kansas City, MO USA
 Posts: 7
 Reputation: imag1nat1on is on a distinguished road (10)
Default Re: .bat Find and replace text

Might have to use this one

Thanks!
  #7  
Old 23rd September 2010, 14:10
WraithForm WraithForm is offline
Casual
Casual
 
 Join Date: Jul 2010
  6 month star 12 month star
 Posts: 22
 Reputation: WraithForm is on a distinguished road (30)
Default Re: .bat Find and replace text

Here is a quick Powershell alternative (from Powershell.com iirc):

Quote:
Replace Text in Files
Often, some text will need to be replaced in a text file. That's easy with Get-Content and Set-Content - or not?

Get-Content c:\somefile.txt | Foreach-Object { $_ -replace 'old', 'new' } | Set-Content c:\somefile.txt

If you try this, PowerShell will complain that the file is in use and can't be written to. PowerShell cannot read and write to a file at the same time. Your solution: use parenthesis so that PowerShell reads the file first and only and then processes the content:

(Get-Content c:\somefile.txt) | Foreach-Object { $_ -replace 'old', 'new' } | Set-Content c:\somefile.txt
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace text via CMD? rack04 General Scripting 6 24th June 2009 10:22
Script that will extract text for a text file rack04 General Scripting 12 30th May 2009 19:47
Find and Replace within AD? Tieem Active Directory 3 7th November 2008 12:35
Windows text file parser; I need to compare two text files... how? Nonapeptide Misc 10 30th April 2008 14:21
XP does not find text in files. JDMils Windows 2000 Pro, XP Pro 7 6th September 2006 14:27


All times are GMT +3. The time now is 14:47.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri