开发者

Windows bat file - deletion from directory

Could you please help me with following problem? Very often I must do the same task which is deleting almost all files from one directory - all files but 2 of them (let say 1st.file and 2nd.file). I found multiple solut开发者_如何学JAVAions how to erase all from directory but have no idea how to work this one out. Could you point to where to find solution?


Replace DONOTDELETE.ME with your file and that file won't be deleted.

@echo off
for /r . %%a in (*.*) do (
    IF NOT "%%~nxa" == "DONOTDELETE.ME"  (
         DEL "%%a"    )
)

You could also just do attrib -r to the files in question and remove the read-only flag afterwards like so:

@echo off
cd \myFiles
attrib myFiles\DONOTDELETE1.ME +r
attrib myFiles\DONOTDELETE2.ME +r
del myFiles\*.* /q /s /a-r
attrib myFiles\DONOTDELETE1.ME -r
attrib myFiles\DONOTDELETE2.ME -r
for /f %%a in ('dir myFiles/ad /b') do echo rd myFiles\%%a /q/s


Something like this might get you started.

@ECHO OFF
SET RemoveFolder=C:\Temp

MV %RemoveFolder%\1st.file 1st.file
MV %RemoveFolder%\2nd.file 2nd.file
RD /S /Q %RemoveFolder%
IF NOT EXIST %RemoveFolder% MD %RemoveFolder%
MV 1st.File %RemoveFolder%
MV 2nd.File %RemoveFolder%

SET RemoveFolder=


This is a little hackish, but you could just change the attributes of the files you do not want to delete...then exclude all files that have that attribute when you call erase.

Calling the follwing like this: TestDelete.Bat "c:\DeleteAllFilesExcept", LeaveFile1.txt, LeaveFile2.txt

ATTRIB +H %2
ATTRIB +H %3

erase %1 /A-H

ATTRIB -H %2
ATTRIB -H %3

Of course, this will not work if you are intending to delete hidden files from the directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜