How do i edit a file in ms-dos

March 28, 2012 at 04:58:47
Specs: Windows 7
i have a file with lots of info in,

I need a ms-dos .bat file that searches the file for a string, once found, it replaces the string with a new string provided as a .bat file argument, then saves the changes

See More: How do i edit a file in ms-dos

Report •

March 28, 2012 at 06:16:53
If there is only a file why not use a text editor?

Report •

March 30, 2012 at 00:30:25
because im going to have to do this multiple times a day and a program would make my life easier...

Report •

March 30, 2012 at 05:18:49
Plenty of suggestions here: . Do you really mean MS-DOS or do you mean Windows command line? (The latter seems more likely.)

Report •

Related Solutions

March 30, 2012 at 13:04:38
Is there a chance of the same string to be repeated in the file? Can't the file be in written correctly in the beginning.

Report •

March 31, 2012 at 04:25:07
Provided as-is.
Some tailoring is needed to fit your environment:


SET /P old=String to replace: &REM Preserve trailing space
SET /P new=Replacement string: &REM Preserve trailing space

SET file=%~dp0\license.txt
SET backup=%file%.bak

REN "%file%" "license.txt.bak"

FOR /F "tokens=1* delims=:" %%A IN ('TYPE "%backup%"^|FINDSTR /n "^"') DO (
    SET line=%%B
    IF DEFINED line SET line=!line:%old%=%new%!
    >>"%file%" ECHO(!line!

TYPE "%file%"

If "old" and "new" are always the same, replace the SET /P with simple SETs that don't prompt the user for input.

Change SET file= to represent the file you need to modify several times a day. I used a software license agreement that happened to be in the same directory as the .bat file as a sample file to test with.

Update "license.txt.bak" to properly represent the (backup) file name that should be created.

Remove the TYPE "%file%" if you don't care to see the file after being modified. I put it there as a debugging tool.

When your only tool is a hammer, every problem looks like a nail.

Report •

Ask Question