0

Want to replace some strings in a text file "list.txt" using batch file commands.

I want to replace C:\ in the file "list.txt" with "adb install C:\" using some batch file commands in windows.

And remove the line containing ".txt" file from "list.txt"

in linux i used to do this with grep command

how to do this?

dkbhadeshiya
  • 147
  • 1
  • 6
  • You could get a port of the standard utilities [GnuWin32](http://gnuwin32.sourceforge.net/) and use grep or sed just like you would on Linux. If you want to use native tools, this post has answers for you: https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir?rq=1 – jpw Feb 23 '14 at 08:33
  • There are several ports of unix commnadline utilities for Windows e.g. here: http://gnuwin32.sourceforge.net/packages.html). Another option is using SwissFileKnife: http://sourceforge.net/projects/swissfileknife/ –  Feb 23 '14 at 08:33
  • here are some solution for the replacing part: http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir?rq=1 For removing lines with a specific string, `findstr /v ` is very useful. – Stephan Feb 23 '14 at 08:36
  • Do you want to use another batch file to perform these tasks? There are two scripts which are similar in function to `sed` and `grep` which are written in the native WSH using jscript. Or do you want a self contained batch solution? – foxidrive Feb 23 '14 at 08:40

3 Answers3

1

try this:

@ECHO OFF &SETLOCAL disableDelayedExpansion
SET "inFileName=infile.txt"
SET "outFileName=outfile.txt"
(FOR /f "delims=" %%a IN ('FINDSTR /n "^" "%FileName%"') DO (
    SET "PrimLine=%%a"
    SETLOCAL enableDelayedExpansion
    SET "Line=!PrimLine:*:=!"
    SET "Line=!Line:C:\=adb install C:\!"
    IF "!Line:txt=!"=="!Line!" ECHO(!Line!
    ENDLOCAL
))>"%outFileName%"
Endoro
  • 37,015
  • 8
  • 50
  • 63
0

Try the following

for /f "usebackq delims=." %x in (`findstr C:\ list.txt`) do echo adb install %x

for /f will pick off the contents of list.txt one at a time

delims=. stops at the first . The drawback is that if there are two . in the filename, it only stops at the first one.

cup
  • 7,589
  • 4
  • 19
  • 42
  • You're assuming things that haven't been revealed about the source file. Additionally a line with `.txt` is to be removed AIUI. – foxidrive Feb 23 '14 at 09:13
  • It is not a perfect solution but it may help the op to do what is required. – cup Feb 23 '14 at 09:16
0

I was looking for a quick and dirty way to do some simple search/replace with built in batch file commands but not a lot of code. This question popped up at the top and the answer from @Endoro is close to what I needed, but it has a few issues. One is a typo (should be %inFileName% not %FileName%), the other is that it outputs C:\=adb install C:\ for blank lines.

Below is modified version of his batch script that fixes both problems and uses environment variables for search and replace values. I did not need the removal of the .txt line so I removed that part. It haven't tested extensively, but it does suit my purposes. Maybe it will help somebody else too.

@ECHO OFF &SETLOCAL disableDelayedExpansion
SET "inFileName=infile.txt"
SET "outFileName=outfile.txt"
SET "SearchVal=C:\"
Set "ReplaceVal=adb install C:\"
(FOR /f "delims=" %%a IN ('FINDSTR /n "^" "%inFileName%"') DO (
    SET "PrimLine=%%a"
    SETLOCAL enableDelayedExpansion
    SET "Line=!PrimLine:*:=!"
    SET "Line=!Line:%SearchVal%=%ReplaceVal%!"
    IF "%SearchVal%=%ReplaceVal%"=="!Line!" ECHO.
    IF NOT "%SearchVal%=%ReplaceVal%"=="!Line!" ECHO(!Line!
    ENDLOCAL
))>"%outFileName%"
Steve In CO
  • 5,746
  • 2
  • 21
  • 32