Home / Microsoft / Microsoft DOS / Find and Replace one string with another inside the file using DOS batch
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: Find and Replace one string with another inside the file using DOS batch  (Read 24280 times)
ariley
Topic Starter
Newbie



Posts: 2


« on: October 27, 2008, 02:16:00 PM »

I need to write a batch file that will look through all file lines and replace a special substring with another string. Any help will be greatly appreciated!

 :)
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #1 on: October 28, 2008, 04:54:40 AM »

Welcome to Computer Hope. We try to encourage posters to search the boards before posting questions that have been asked and answered.

Check out the set statement by typing set /? at the command prompt. Look for  variable substitution.

Code: [Select]
set input=%input:search=replace%

Happy coding.  8)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
ariley
Topic Starter
Newbie



Posts: 2


« Reply #2 on: October 30, 2008, 01:28:52 PM »

Thank you for the reply. I did try it, but it doesn't really work for me, thus I put a new post :'(
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #3 on: October 30, 2008, 01:37:10 PM »

Show us the code you're using. It's easier to help when we can see what we're doing instead of stumbling around in the dark.

 8)

IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Dias de verano
Guest
« Reply #4 on: November 01, 2008, 04:27:21 AM »

Yes! ariley, you should give much more information. One file, or many?
IP logged
newsflow
Starter



Posts: 4


« Reply #5 on: May 15, 2010, 04:05:46 AM »

I noticed that this topic is not answered ... in correct way, so ... I describe few more:

I have text-file "test.txt" that I need to modify it - to find the strings and replace them the another - like:

Before:
xxxx xxxx 0, 52, xxx xxx
xxx  xxx  0, 55, xxx xxx
xxx xx  0, 52, xxx xx xxx

After:
xxxx xxxx 0, 21, xxx xxx
xxx  xxx  0, 55, xxx xxx
xxx xx  0, 21, xxx xx xxx


So - how I be able to do this edition using batch file - and so that I don't need to do anything that run this batch file.
I know what is the string that need to replace another but them are so many that I don't like to manage them throug Notepad or edit window - it takes too much time. There are also several separate "strings" that needed to replace another.

So - could anyone help this? Please! Even if you really know that this is not possible to do in MS-dos ... this information would be nice to know. I tried to find propier commands ... without any real results :(
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,951

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #6 on: May 15, 2010, 04:56:16 AM »

You should NOT add to a thread last used 2 years ago! Do you mean "real" MS-DOS, or Windows NT family (2K, XP, Vista, 7 etc) command prompt? If the latter, then read on...

This was done at the prompt, but it works in a a batch file too, and you can use a FOR loop and delayed expansion to process every line in a text file, and you can replace a number of strings with a number of other strings.

Code: [Select]

C:\>set oldstring=xxxx xxxx 0, 52, xxx xxx

C:\>set newstring=%oldstring:0, 52=0, 21%

C:\>echo %oldstring%
xxxx xxxx 0, 52, xxx xxx

C:\>echo %newstring%
xxxx xxxx 0, 21, xxx xxx


IP logged


Proud to be European
newsflow
Starter



Posts: 4


« Reply #7 on: May 15, 2010, 06:07:24 AM »

Thanks "big fish" ,

but I'm not sure how this really works, because I want to edit only certain files and your example not depict changes to the certain file/-s ... am I right?

Anyway ... I changed my problem solving/finding method  ;D  and finally found a tool (small C++ program) from here:
http://www.programmersheaven.com/download/41236/download.aspx

I already tested this tool - it looks working as I would like.

Thanks anyway!   :)

« Last Edit: May 15, 2010, 06:19:25 AM by newsflow » IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,951

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #8 on: May 15, 2010, 06:33:05 AM »

am I right?

No.
IP logged


Proud to be European
newsflow
Starter



Posts: 4


« Reply #9 on: May 15, 2010, 07:07:44 AM »

could you decribe few more - please?

I'm always undesrtood that set command sets environment variables and ... that mean changes in area that can cause conflict with other programs if I'm not careful with these "environment" parameters ... so I don't understand how this way is possible to make changes to files that really are only used for the files that I want.

Ok - it is possible to define PATH but this define the path for runtime environment ... it feels few scary way to handle these changes - am I again wrong?
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,951

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #10 on: May 15, 2010, 07:17:11 AM »

could you decribe few more - please?

I'm always undesrtood that set command sets environment variables and ... that mean changes in area that can cause conflict with other programs if I'm not careful with these "environment" parameters ... so I don't understand how this way is possible to make changes to files that really are only used for the files that I want.

Ok - it is possible to define PATH but this define the path for runtime environment ... it feels few scary way to handle these changes - am I again wrong?

Yes, the set command creates variables and assigns values to them. It might be foolish to create a variable with the same name as an already existing one, but you can create your own and use them as you want. They are like variables in BASIC or other programming languages. Temporary holders of data.

You wrote

Quote
I need to write a batch file that will look through all file lines and replace a special substring with another string.

I have now given you the information you need to write such a batch file.
IP logged


Proud to be European
newsflow
Starter



Posts: 4


« Reply #11 on: May 15, 2010, 09:02:03 AM »

Ok - now I understand ... I didn't read enough carefully the original message/question ... I didn't write the original, I just though that this is almost same what I need.

You are right ... for the original request ... so if I could use environment variables but I'm creating a convert tool for midi files (for my own use only). My problem is that there are lot of different midi formats, the drum sets are different (depends on sw producer), there are basicly same parts of drum sets but in different order in keyboard.

So I convert the midi file to text file and the text file so that all drum parts will be in their own area (outside the normal area) after all are moved I'll return them back the correct place for another format ... so I cannot use env-variables for this. Because I need to re-order the parts not only change the value. After that I convert the text-file back to midi format.

I also want to compare both of them ... how them sound ...

Thanks "big fish" - this helps also to understand another way to use env-variables - i'm never used them this way - thanks.
IP logged
marvinengland
Hopeful



Thanked: 10
Posts: 258


« Reply #12 on: May 16, 2010, 12:33:36 PM »

I noticed that this topic is not answered ... in correct way, so ... I describe few more:

I have text-file "test.txt" that I need to modify it - to find the strings and replace them the another - like:

Before:
xxxx xxxx 0, 52, xxx xxx
xxx  xxx  0, 55, xxx xxx
xxx xx  0, 52, xxx xx xxx

After:
xxxx xxxx 0, 21, xxx xxx
xxx  xxx  0, 55, xxx xxx
xxx xx  0, 21, xxx xx xxx



Use sed.exe

http://www.gnu.org/software/sed/



C:\test>type teststr.txt
xxxx xxxx 0, 52, xxx xxx
xxx  xxx  0, 55, xxx xxx
xxx xx  0, 52, xxx xx xxx
Output:
C:\test>
Code: [Select]
sed.exe  s/52/21/g  teststr.txtxxxx xxxx 0, 21, xxx xxx
xxx  xxx  0, 55, xxx xxx
xxx xx  0, 21, xxx xx xxx

C:\test>

p.s. google download sed.exe  for windows

add the location of sed.exe  to the search path variable
« Last Edit: May 16, 2010, 01:26:45 PM by marvinengland » IP logged

USA
Salmon Trout
Sage



Thanked: 546
Posts: 7,951

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #13 on: May 16, 2010, 02:01:32 PM »

OP may not be able to install sed.exe if he or she is using workplace or educational computer.
IP logged


Proud to be European
ASharrad
Newbie



Posts: 1

Experience: Beginner
OS: Unknown

« Reply #14 on: September 28, 2011, 06:01:32 PM »

You could always use this - its free, and will allow you to search and replace a string with another;

Or allow you to search a file for the contents of another file, and replace with another.

And all variations in between :)

Pure DOS version available - works on files up to about 60K less than your DOS free memory, or a Windows 95 onwards command line version also available:
http://www.sharradsoftware.co.uk/downloads.htm

Any questions please ask :)
IP logged
Raven19528
Hopeful



Thanked: 29
Posts: 284

Computer: Specs
Experience: Experienced
OS: Windows 7



« Reply #15 on: September 29, 2011, 10:10:38 AM »

Wow, this thread just keeps getting revived from the grave. Fun.

There is a way to do this in batch, though there are much better tools out there to accomplish the task as shown throughout the thread. The batch method is outlined and not fully coded below:

This command would be used for one file and would need to be embedded in another for command if multiple files were needing to be changed.
for /f "delims=" %%G in (changefile.txt) do (
  set oldstring=%%G
  set newstring=!oldstring:~0,  52=0,  21!
  echo !newstring!>>temp.txt
)
del changefile.txt
ren temp.txt changefile.txt


Again, this could be run on multiple files using this for command embedded into another for command that would determine what is placed in the "changefile.txt" area. Obviously you could put multiple "set newstring=" commands in if there were multiple changes that were needing to be made.

Can we let this thread die now? It has lived a long life and should be allowed to rest in peace.
 ::)
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
Raven19528
Hopeful



Thanked: 29
Posts: 284

Computer: Specs
Experience: Experienced
OS: Windows 7



« Reply #16 on: September 30, 2011, 11:02:16 AM »

for /f "delims=" %%G in (changefile.txt) do (
  set oldstring=%%G
  set newstring=!oldstring:~0,  52=0,  21!
  echo !newstring!>>temp.txt
)
del changefile.txt
ren temp.txt changefile.txt


One error: there is no ~ after the colon in the set newstring line. It should read:
set newstring=!oldstring:0,  52=0,  21!
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / Find and Replace one string with another inside the file using DOS batch « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.114 seconds with 19 queries.