November 16, 200520 yr I'd like to delete all *.bmp files in %WinDir% but boot.bmp in my cleanup.cmd.whats the best way to do this?
November 16, 200520 yr REN %WinDir%\boot.bmp boot.pmbdel %WinDir%\*.bmp /F /QREN %WinDir%\boot.pmb boot.bmpIt'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 November 16, 200520 yr by gosherm
November 16, 200520 yr 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 as1) it is cleaner and2) it has less overheadEdit: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 November 16, 200520 yr by gosherm
November 16, 200520 yr Author 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.bmpOh.. 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 wereC:\>test2.cmdFinner ikke C:\BlåFinner ikke C:\Bobler.bmpFinner ikke C:\Fjær.bmpFinner ikke C:\Fluefisker.bmFinner ikke C:\Jade.bmpFinner ikke C:\Kaffekopp.bmpFinner ikke C:\Pastell.bmpFinner ikke C:\Rododendron.bFinner ikke C:\SantaFinner ikke C:\StormFinner ikke C:\Ullteppe.bmp Edited November 16, 200520 yr by BoardBabe
November 16, 200520 yr %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.pmbREN %WinDir%\boot2.bmp boot2.pmbdel %WinDir%\*.bmp /F /QREN %WinDir%\boot.pmb boot.bmpREN %WinDir%\boot2.pmb boot2.bmp Edited November 16, 200520 yr by gosherm
November 16, 200520 yr 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" ) ))
November 17, 200520 yr Author 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.bmpCould not find C:\Pastell.bmpetc.Maybe it should be del /f /q "%WinDir%\%%i"?
November 17, 200520 yr Author Hehe I was Thank's for the help!PS. the (del) /f is not needed as the files are not write protected. Edited November 17, 200520 yr by BoardBabe
November 19, 200520 yr Author This seems to work alsofor /f "usebackq tokens=*" %%i in (`dir /b "%winDir%\*.bmp"`) do if %%i neq boot.bmp del /q "%WinDir%\%%i" Edited November 19, 200520 yr by BoardBabe
Create an account or sign in to comment