0

I have been trying to create a batch script which will delete specific files from a location. The files that are NOT to be deleted are listed in a separate file called list.txt.

So far it works if the entire filename + extension is written in the list.txt like this:

abcapplefruit.jpg 12345numbertext.png moon1234sun.htm

What I would like to have is to write only a part of the filename in the list.txt like this:

apple number sun

And then skip these files containing these words from deletion.

What I have managed to do is the following:

@echo off
    setlocal
    set "folder=C:\Users\abc\Desktop\WindowsBatch\Log"
    set "excludeFile=%folder%\list.txt"
    setlocal enableDelayedExpansion

    :: Check that both the target folder and filter file exist before starting up.

    if not exist "%folder%" echo %~nx0: The target folder doesn't exist. Nothing to do. && echo Target folder             does not exist.
    if not exist "%excludeFile%" echo %~nx0: The list file doesn't exist at the location specified! && echo         List file does not exist.

    for /f "delims=" %%F in ('dir /b /a-d "%folder%\"') do Call :process_file "%%F" "%~1" "%excludeFile%"

    goto :EOF
    :: --------------------------------------------
    :process_file
    :: --------------------------------------------
    set input_file=%~1
    set list_file=%~nx3

    echo Input file: "%input_file%"

    :: Skip list file and this batch file too
    if "%list_file%"=="%input_file%" echo Skip list file && goto :EOF

    ::echo This batch file = Input file: "%this_batch%"=="%input_file%"
    echo list file = input file: "%list_file%"=="%input_file%"

    :: Grep for the include file in the list 
    findstr "%input_file%" "%excludeFile%" 2>&1 1>NUL && goto :EOF

    :: Bail out if the input line was in the list file
    if not errorlevel 1 echo Skip "%input_file%", it is in %list_file%  && goto :EOF

    pause

    del %folder%\%input_file%
    echo %folder%\%input_file%

    pause
  • 1
    `for /f "delims=" %%F in ('dir /b /a-d "%folder%\"'^|findstr /v /g:"%excludeFile%") do …` pipes the `dir` list to `findstr` which reads its strings-to-find from the `/g:file` & outputs the non-matches (`/v`). You may also want to add `/i` to make the match case-insensitive. Also you need to `goto :eof` for the `file missing` reports. – Magoo May 05 '23 at 19:39
  • Question: Is there a way to make it work with forfiles? 'FOR /f "tokens=* delims=" %%A in ('dir /b /a-d "%CurrentFolder%\" ^|findstr /v /g:"%excludeFile%"') DO ( forfiles /p %%A /s /D -2 /c "cmd /c echo @path" ) pause ECHO.' When i try to run this, It gives an error that the path or file is not found. – Peter Laszlo May 11 '23 at 13:02
  • I have never, in 40 years of using MS batch, used `forfiles`. Does `%%A` contain spaces, or does `forfiles` expect a filespec instead of a directoryname? The cure to the first would likely be `forfiles "%%A"` and the second `forfiles %%A\*` or perhaps `forfiles "%%A\*"` for both. – Magoo May 11 '23 at 20:38

0 Answers0