dannyr.gez
dannyr.gez

Reputation: 77

Batch: How to exclude file names with extensions from a directory path in a text file?

file.txt:

C:\Program Files\Folder\file.exe

Can be .exe, .dll, etc.

Output Goal:

C:\Program Files\Folder

Code so far:

for /f "usebackq delims=" %%g in (`type file.txt ^| findstr /vc:".exe"`) do ( echo %%g )

Upvotes: 0

Views: 360

Answers (1)

Compo
Compo

Reputation: 38622

The following example assumes that all your listed files all have an extension, (comprising a period, followed by 2, 3, or 4 characters). To clarify, that means it will ignore any files or last leaf directories which have no periods, or end with a period followed by 1, 5, or more than 5, characters; but will pick up last leaf directories which end with a period folowed by 2, 3, or 4 characters:

@For /F Delims^=^ EOL^= %%G In ('%__AppDir__%findstr.exe /ER "\...$ \....$ \.....$" "file.txt" 2^> NUL')Do @For %%H In ("%%~dpG.")Do @Echo %%~dpnxH

Please note that, I've used findstr.exe directly in the example above. If you aren't getting the results intended it is likely that your source file, file.txt, is Unicode. In that case, you should revert to piping the content from the type command, as in your orginal posting:

@For /F Delims^=^ EOL^= %%G In ('Type "file.txt" 2^> NUL ^| %__AppDir__%findstr.exe /ER "\...$ \....$ \.....$"')Do @For %%H In ("%%~dpG.")Do @Echo %%~dpnxH

The code could be made more robust if each of the items from the file, or the ones you require, existed within the system. (But that is currently out of the scope of your very limited question criteria).

Upvotes: 1

Related Questions