0

I have this batch script:

set driveletter=F

call :delext "*.foo"
call :delext "*.bar"
call :delext "*.pdf"


:: funcion delext
@echo off
pause
goto:eof
:delext
  set delext=%1
  del /f/q/s %driveletter%:\"%delext%"
goto:eof

What I need is an "echo" if there is a match with any of the extensions.

For example, if there is a file called test.pdf and since it matches the extension *.pdf, then I would like an echo match pdf in the output (if there is no match do not show anything.)

How can I do that?

1 Answer 1

1

I am a mega-fan of functional batch programming.

My solution here might be a sloppy mess as I haven't slept NEARLY enough but I can assure you that it works and does more or less what you are asking.

This is possibly the most inefficient way to go about this but if I wrote the batch to do all of the extensions per directory, the keeping track if I told you about them gets a bit more complicated but would be faster execution.

Also, the functions could be reduced to very little code if I didn't use variables so you could follow along as to what was happening.

@echo off

:: Set the starting path that we will be searching.
Set pathToSearch=C:\users\yo_mamma
cd /d "%pathToSearch%"
if not "%ERRORLEVEL%"=="0" echo Path %PathToSearch% not found&&exit /b 1


Set ExtensionsToCheck="*.foo" "*.txt" "*.bar" "*.pdf"
call :EnumerateExtensions %ExtensionsToCheck%
goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Function "EnumerateExtensions"
:EnumerateExtensions
if "%~1"=="" goto :EOF
Set fileMatchFound=FALSE
Set searchFileMask=%1
for /r %%p in ('.') do call :EnumerateDirectories "%%p" %searchFileMask%
shift
goto :EnumerateExtensions

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Function "EnumerateDirectories"
:EnumerateDirectories
Set directoryPath=%~DP1
Set fileMask=%~2
for %%f in (%fileMask%) do call :MatchFound "%directoryPath%" "%fileMask%" "%%f"
goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Function "MatchFound"
:MatchFound
if not "TRUE"=="%fileMatchFound%" echo ****** Mask match found:  %~2&&Set fileMatchFound=TRUE
:: echo del %~1%~3 <-- delete the found file here..
goto :EOF
5
  • it works. could you modify it to be recursive? (set pat and search file and folders/subfolders into parh)
    – acgbox
    Feb 18, 2022 at 19:40
  • It is already (of course) recursive as that is what you asked for. Thats what the for /r %%p in ('.') do call :blahblah does. /r walks the directory tree. I used "." implying start at the current directory and walk the directory tree. Feb 18, 2022 at 22:03
  • DO you mean a path variable as to where the operation STARTS from? Sure I can do that... I just need to know if that is what you mean. If we add the path selection, we need to also add that we actually have access to the directory specified or it will walk some "other" path deleting things that you don't want deleted. Bad JUJU.. TRUST ME.. you only make that mistake ONCE! :) Feb 18, 2022 at 22:21
  • In the question a path is set and within that path it should recursively search directories and subdirectories and files. I tried it and it doesn't
    – acgbox
    Feb 19, 2022 at 23:12
  • OK, added a variable called pathToSearch. It works fine on my machine. If you want to be able to pass the path to the batch file instead of hard-coding, that is simple too. Just let me know. Feb 19, 2022 at 23:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .