I have a folder named Demos1
with files inside that all have a .replay
extension.
I have a batch script in parent folder of Demos1
as well as a file that has the names of the files i ONLY want to keep in a text file named namediff.txt
.
I wants to recursiveley scan each file name in namediff.txt
against the files found inside of the directory and if the file names (one per line) in namediff.txt
does not match a file found in the directory, delete that file in Demos1
and leave only the file names that did match in the txt file.
This is what I have so far.
PUSHD "%~dp0Demos1"
:--------------------------------------------------------------------------------------------
FOR /F "TOKENS=*" %%A IN ('DIR /B "*.replay"') DO (
FOR /F "TOKENS=*" %%G IN ('%%~nxA') DO (
IF NOT "%%~nxA"=="%%~nxG" ECHO DEL "%%~nxG"
)
)
CodePudding user response:
FOR /F "TOKENS=*" %%A IN ('DIR /B /a-d "*.replay" ^|findstr /x /i /v /g:namediff.txt') DO echo del "%%A"
Should do that. Findstr
finds lines that /x
exactly match /i
case-insensitive /g:filecontainingstringstomatch
and /v
produces non-matches to the list of files generated by the dir
.
The ^
is required to escape the pipe, which tells cmd
that the pipe is part of the command to be executed, not the for
.
The /a-d excludes directory-names matching - just a belt-and-braces approach.
The del
is simply echo
ed for testing purposes. Remove the echo
keyword when verified to delete the files.