C. Wallcraft
C. Wallcraft

Reputation: 23

.bat remove strings from multiple .txt files

I have 10 text files in the same folder with lists like the following:

abc0012345
abc0012346
abc0012347
abc0012348
abc0012349
abc0012350
abc0012351
abc0012352

I have a remove.txt file containing the strings I want to remove, it contains abc0012347 and abc0012351 and I also want to remove the blank space they will leave behind

With my code below I can list if the strings can be found in the 10 text files but I'm having trouble removing the strings

:: /i   `: Specifies that the search is not to be case-sensitive. 
:: /g: file   `: Gets search strings from the specified file. 
Rem "List of systems before removing from tag files"
echo off
c:
cd C:\StringFiles
findstr /I /G:"remove.txt" *.txt
pause

rem the following line removes specific entry from text file but I need it to look in the remove.txt file for the strings to remove and then loop through all text files
rem type C:\E\TagFiles\2.txt | repl "stc0012345+" "" X | repl "\r?\n?\s*$" "" M > C:\E\TagFiles\2.out.txt`

Upvotes: 2

Views: 1104

Answers (2)

Ani Menon
Ani Menon

Reputation: 28209

Replace text within files (using powershell):

(Get-Content <file_name>) | ForEach-Object { $_ -replace <pattern>, <replacement-string> } | Set-Content <file_name>

Example execution : powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt"

Explanation of example:

  • powershell starts up powershell.exe, which is included in Windows 7
  • Command "... " is a command line arg for powershell.exe containing the command to run (gc myFile.txt) reads the content of myFile.txt (gc is short for the Get-Content command)
  • replace 'foo', 'bar' simply runs the replace command to replace foo with bar | Out-File myFile.txt pipes the output to the file myFile.txt

Refer: how-can-you-find-and-replace-text-in-a-file

To execute powershell in .bat file :

Remove powershell.exe from the file and save it as a .ps1 then create a .bat file and write powershell.exe -file myscript.ps1

Note : Powershell.exe should be part of your PATH statement. Default location of powershell : "C:\WINDOWS\system32\WindowsPowerShell\v1.0"

Upvotes: 1

dbenham
dbenham

Reputation: 130819

If your FINDSTR command is properly listing only lines that should be removed, then all you need is the /V option to print all lines that don't match.

You also need a FOR loop to iterate the files. You can write out the preserved content to a new file, and then use MOVE to replace the original with the new. You also want to make sure you don't modify your "remove.txt".

for %%F in (*.txt) do if /i "%%F" neq "remove.txt" (
  findstr /vig:"remove.txt" "%%F" >"%%F.new"
  move /y "%%F.new" "%%F" >nul
)

REPL.BAT is not a good candidate for this problem, as it does not have a built in capability to load search or replace strings from another file.

If you like to use REPL.BAT for other tasks, then I suggest you graduate to the newer JREPL.BAT utility. It has all the capability of REPL.BAT, plus a bunch more. However, it is still not a good choice for this problem.

Upvotes: 2

Related Questions