Search code examples
batch-filewhitelist

Need to exclude certain files from the entire folder using a whitelist text file


I'm having a problem with excluding certain files from the entire folder with using a whitelist text file. Currently I'm working on batching scripting

for /f "tokens=* Delims=" %%x in (whitelist.txt) do
(
  for %%i in ("list\*") do 
  (
   if not "%%i"=="%%x" 
   (
   echo %%i
   )
 )
)

Need some guidance here thanks.


Solution

  • You are iterating the directory for each line in whitelist.txt

    Whithout knowing what is in whitelist.txt, this is an aproximation to the problem

    inside whitelist.txt

    one.txt
    two.txt
    this is data.txt
    

    Then you can do something like

    for /f "tokens=*" %%f in (
        'dir /b ^| findstr /v /b /e /i /l /g:whitelist.txt'
    ) do echo %%f
    

    Generate the listing of files (and folders, if you want to exclude them, add /a-d to dir command) getting only the filenames (/b), and filter with findstr. Parameters are : take the search strings from whitelist.txt (/g:whitelist.txt), content of whitelist.txt are literal strings (/l), ignore case (/i), search strings should match from begin (/b) to end (/e) of line, and only return lines not matching (/v).