Solved Using batch to keep only specified words in .txt file

January 26, 2014 at 20:44:44
Specs: Windows 7
Here's the batch code i use to replace words in a .txt file. The code below would replace the word xp with vista anywhere in the a.txt file

SetLocal EnableDelayedExpansion
For /F "tokens=* delims=" %%A in (a.txt) Do (
Set TxtLine=%%A
Set TxtLine=!TxtLine:xp=vista!
echo !TxtLine!>>tmp.3)
del a.txt & ren tmp.3 a.txt

What I would like is a code for example that would remove all words in a text file except for the word xp. I was wondering if the above code could be altered in some way to do this. Thanks for your help.

See More: Using batch to keep only specified words in .txt file

Report •

January 27, 2014 at 13:03:03
I suspect I'm missing something. I can't comprehend the utility of making a file that looks like:
Unless you just want to count the "xp"s, in which case there are probably easier ways. Maybe my brain is just running a little lean today...

Report •

January 28, 2014 at 10:25:36
The xp thing I just used as an example. I'll be more specific. What I got is a file that has hundreds of file destinations and I just want a batch that would delete the file name and keep the folder names. Eg:


Need a batch that would result in the text file looking as:


Hope that simplifies the question. Thanks
Hope that simplifies th

Report •

January 28, 2014 at 13:13:15
✔ Best Answer
Ah. Something like this might work if the lines are just filenames (ie: the filenames are not embedded in other text.):

@echo off & setlocal
(for /f "tokens=*" %%a in (a.txt) do echo %%~dpa)>tmp.3
::==== end
Note that if the filename is not preceded by a path, the current working directory gets substituted. Also, filenames and paths are not validated as to existance or not. That could easily be done by adding "if exist" into the body.

message edited by nbrane

Report •

Related Solutions

January 29, 2014 at 13:17:23
Wow, thank you. U solved it so easily.

Report •

Ask Question