Jump to content

Delte all %WinDir%\*.bmp BUT ONE.


Recommended Posts


REN %WinDir%\boot.bmp boot.pmb
del %WinDir%\*.bmp /F /Q
REN %WinDir%\boot.pmb boot.bmp

It's cheap, but you'll be able to figure out what's going on when you look at it again a year from now ;)

Edited by gosherm
Link to comment
Share on other sites

Ok, how about this?

for /F "usebackq" %%i in (`dir /b %WinDir%\*.bmp`) do (
if %%i equ boot.bmp (
REM Found boot.bmp
) else (
del /f /q %%i
)
)

My suggestion is to still use the first solution as

1) it is cleaner and

2) it has less overhead

Edit:

Hmm... I suppose this could actually be simplified a little bit to:

for /F "usebackq" %%i in (`dir /b %WinDir%\*.bmp`) do (
if %%i neq boot.bmp (
del /f /q %%i
)
)

Edited by gosherm
Link to comment
Share on other sites

Now how about if there are TWO files i'd not like to delete. (Just for learning how this works...)

eg. boot1.bmp and boot2.bmp

Oh.. and it did'nt work ;)

It searched for the file in the location where the batch were executed.

I put a testbatch on C:\ and the result were

C:\>test2.cmd

Finner ikke C:\Blå

Finner ikke C:\Bobler.bmp

Finner ikke C:\Fjær.bmp

Finner ikke C:\Fluefisker.bm

Finner ikke C:\Jade.bmp

Finner ikke C:\Kaffekopp.bmp

Finner ikke C:\Pastell.bmp

Finner ikke C:\Rododendron.b

Finner ikke C:\Santa

Finner ikke C:\Storm

Finner ikke C:\Ullteppe.bmp

Edited by BoardBabe
Link to comment
Share on other sites

%windir% is going to be your windows folder (i.e. c:\windows), not the drive root (%systemdrive%). It shouldn't have removed any files from C:\.

As far as multiple files, this is where the second solution gets messy because the IF statement can't handle complex statements (i.e. if (a=b OR a=c) then). So, you're left with:

for /F "usebackq" %%i in (`dir /b %WinDir%\*.bmp`) do (
if %%i neq boot.bmp (
if %%i neq boot2.bmp (
del /f /q %%i
)
)
)

Whereas the first solution gives you:

REN %WinDir%\boot.bmp boot.pmb
REN %WinDir%\boot2.bmp boot2.pmb
del %WinDir%\*.bmp /F /Q
REN %WinDir%\boot.pmb boot.bmp
REN %WinDir%\boot2.pmb boot2.bmp

Edited by gosherm
Link to comment
Share on other sites

Hmm... it shouldn't be... though I did find one problem.

Try this as a test:

for /F "usebackq tokens=*" %%i in (`dir /b %WinDir%\*.bmp`) do (
if %%i neq boot.bmp (
if %%i neq boot2.bmp (
echo deleting "%%i"
) else (
echo saving "%%i"
)
) else (
echo saving "%%i"
)
)

and this as the final:

for /F "usebackq tokens=*" %%i in (`dir /b %WinDir%\*.bmp`) do (
if %%i neq boot.bmp (
if %%i neq boot2.bmp (
del /f /q "%%i"
)
)
)

Link to comment
Share on other sites

Now it does exclude boot.bmp, but it's still something wrong with the path. I put this in test.cmd on c:\ and the result of #2 is (translated):

Could not find C:\Jade.bmp

Could not find C:\Pastell.bmp

etc.

Maybe it should be

del /f /q "%WinDir%\%%i"

?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...