Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? Filter file-contents based on "IF EXIST"

Feb
64
1
I have a file that contains names of other files or dirs. Now I want to create a 2nd file with the reduced list of files that actually exist.

I'm pretty sure that I'll manage to write a loop to do this, but I'd like to level up my TC-knowledge and was wondering if it wouldn't be nice to do this with a TPIPE and perhaps /run - but the doc still scares me - so I thought I'll ask the Pros before trying to get it done all by myself... ;-)
 
Assuming your file that contains the names is named foo.txt:
Code:
(type foo.txt & dir /b) | tpipe /dup=1,0,1,100,0,"%%1:s"

That creates list of names to stdout and passes that to tpipe. Tpipe will filter the list and only output those lines that have duplicates. The DIR /B command will list the files that exist.

-Scott
 
It doesn't look like you can specify a length of -1 or 0 to say use the whole line. So set a length you know is long enough. I used 100 in my example, but you could make it 32768 and that works too.
 
Thanks - that is very nice - but the problem (and the point I probably have not made clear enough) is that the files mentioned in that file are fully qualified names, so I can't compare against the dir-result from the current dir, that wouldn't return everything needed.
 
Use DIR /F then instead of /B. If you need to parse an entire tree then add /S. E.g. DIR /SB or DIR /SF
 
Or take a more direct approach ...
Code:
 do name in @files.txt ( if exist "%name" echo %name )
... or something like that.
 
Back
Top
[FOX] Ultimate Translator
Translate