Showing a detailed report after deleting of specified files scattered on all volumes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 147
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Showing a detailed report after deleting of specified files scattered on all volumes

#1 Post by DOSadnie » 09 Jun 2025 10:25

Two years ago I had a problem of doing the task of scanning all pre-choosen volumes and deleting from them temporary Excel [XLSM] files that sometimes get left over after usage of that program [after crash of operating system most likely]. And with the help of some good people from this forum I did come up with some solutions [viewtopic.php?f=3&t=10853]

But now I have a more advanced code for doing that, which code informs user precisely what is happening and in the end of what has been done:

Code: Select all

@echo off

chcp 65001 >nul

setlocal enabledelayedexpansion

set "PATTERN OF FILES=~$*.XLSM"
set "LIST OF VOLUMES TO BE SCANNED=C D E F G H I J K L M N O P Q R S T U V W X Y Z" rem This is not a full list
:: set "LIST OF VOLUMES TO BE SCANNED=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" rem This is the full list i.e. from A to Z
set "FILES_DELETED="
set "FILES_NOT_DELETED="
set "VOLUMES_INACCESSIBLE="
set "VOLUMES_OMITTED="
set "VOLUMES_PROCESSED="

echo  Deleting hidden files matching this pattern:
echo.
echo          %PATTERN OF FILES%
echo.
echo.
echo.
echo  List of volumes:
echo.

for %%v in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
    set "found=0"
    for %%d in (%LIST OF VOLUMES TO BE SCANNED%) do (
        if "%%v"=="%%d" set "found=1"
    )
    if "!found!"=="1" (
        if exist "%%v:\" (
            echo          %%v + scannable
        ) else (
            echo          %%v - absent
        )
    ) else (
        echo          %%v • omitted
        set "VOLUMES_OMITTED=!VOLUMES_OMITTED!%%v "
    )
)

echo.
echo.
echo.

set /p "=Process is in progress..." <nul

for %%d in (%LIST OF VOLUMES TO BE SCANNED%) do (
    set "drive=%%d:\"
    if exist "!drive!" (
        set "VOLUMES_PROCESSED=!VOLUMES_PROCESSED!%%d "
        for /f "delims=" %%f in ('dir "!drive!%PATTERN OF FILES%" /a:h /b /s 2^>nul') do (
            del /ah /q "%%f" >nul 2>&1
            if exist "%%f" (
                set "FILES_NOT_DELETED=!FILES_NOT_DELETED!%%f|"
            ) else (
                set "FILES_DELETED=!FILES_DELETED!%%f|"
            )
        )
    ) else (
        set "VOLUMES_INACCESSIBLE=!VOLUMES_INACCESSIBLE!%%d "
    )

)

<nul set /p=[1A[2K

echo.

:: 
:: This below excess of pause signs after >> Volumes scanned:<< is a workaround needed for the above process of overwriting of the >>Process is in progress...<< line to work correctly
::

echo  Volumes scanned:        
echo.
if defined VOLUMES_PROCESSED (
    echo          !VOLUMES_PROCESSED!
) else (
    echo          [NONE]
)

echo.
echo.
echo.

echo  Volumes blacklisted from being accessed:
echo.
if defined VOLUMES_OMITTED (
    echo          !VOLUMES_OMITTED!
) else (
    echo          [NONE]
)

echo.
echo.
echo.

echo  Volumes not accessible or missing:
echo.
if defined VOLUMES_INACCESSIBLE (
    echo          !VOLUMES_INACCESSIBLE!
) else (
    echo          [NONE]
)

echo.
echo.
echo.

echo  Deleted files:
echo.
if defined FILES_DELETED (
    for %%F in ("!FILES_DELETED:|=" "!") do (
        if "%%~F" NEQ "" echo          %%~F
    )
) else (
    echo          [NONE]
)

echo.
echo.
echo.

echo  Deletion failed for:
echo.
if defined FILES_NOT_DELETED (
    for %%F in ("!FILES_NOT_DELETED:|=" "!") do (
        if "%%~F" NEQ "" echo          %%~F
    )
) else (
    echo          [NONE]
)

endlocal

echo.
echo.
echo.

pause
[I allowed myself to start a new thread, because the title of the original one mentions a coding issue and does not inform descriptively of the overall goal of the script I was working on]

Post Reply