Remove file extensions with "echo match extension" in a Batch script?

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like