Frage

I am trying to write a bat script which will delete every file with a specific extension (for example '.log') from all drives, including external ones which are attached.

For example, if no external drives are attached the script will remove all '.log' files under C:\ and other mounted partitions at that time.

If there are external drives attached, the script will remove all '.log' files under all the drives.

[Not essential] It would be really nice, if the script could preserve .log files larger than 1MB for example. Also, it would be awesome if the script could automatically detect new attached drives and delete '.log' files from them too.

I know I am asking a lot, but your knowledge has impressed me many many times!

Thank you very much in advance!

War es hilfreich?

Lösung

You just need a for loop to do this (taking 09stephenb's code as the guts :))

@echo off
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do (
    echo INFO: Removeing all log files from %%x\
    del %%x\*.log /s /f /q
)
echo INFO: Done
pause

Andere Tipps

Is this what you want.

@echo off
pushd C:\
echo removeing all log files
del \*.log /s /f /q
echo done & pause
exit

It will search for all files with a .log extension. Just make sure you don't need any of them. You will need to run the batch file as a Admin. Replace pushd C:\ with pushd [your drive]:\.

To use all drives:

@echo off
echo removeing all log files
pushd A:\
del \*.log /s /f /q
pushd B:\
del \*.log /s /f /q
pushd C:\
del \*.log /s /f /q
pushd D:\
del \*.log /s /f /q
pushd E:\
del \*.log /s /f /q
pushd F:\
del \*.log /s /f /q
pushd G:\
del \*.log /s /f /q
pushd H:\
del \*.log /s /f /q
pushd I:\
del \*.log /s /f /q
pushd J:\
del \*.log /s /f /q
pushd K:\
del \*.log /s /f /q
pushd L:\
del \*.log /s /f /q
pushd M:\
del \*.log /s /f /q
pushd N:\
del \*.log /s /f /q
pushd O:\
del \*.log /s /f /q
pushd P:\
del \*.log /s /f /q
pushd Q:\
del \*.log /s /f /q
pushd R:\
del \*.log /s /f /q
pushd S:\
del \*.log /s /f /q
pushd T:\
del \*.log /s /f /q
pushd U:\
del \*.log /s /f /q
pushd V:\
del \*.log /s /f /q
pushd W:\
del \*.log /s /f /q
pushd X:\
del \*.log /s /f /q
pushd Y:\
del \*.log /s /f /q
pushd Z:\
del \*.log /s /f /q
echo done & pause
exit
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top