Windows bat文件 - 从目录中删除

时间:2021-03-20 20:19:11

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 solutions how to erase all from directory but have no idea how to work this one out. Could you point to where to find solution?

你能帮我解决下面的问题吗?我经常必须执行相同的任务,即从一个目录中删除几乎所有文件 - 所有文件只有2个(比如1st.file和2nd.file)。我找到了多个解决方案,如何从目录中删除所有,但不知道如何解决这个问题。你能指出在哪里找到解决方案吗?

3 个解决方案

#1


0  

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

将DONOTDELETE.ME替换为您的文件,该文件不会被删除。

@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:

您也可以只对相关文件进行attrib -r并删除之后的只读标志,如下所示:

@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

#2


0  

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=

#3


0  

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.

这有点hackish,但您可以只更改您不想删除的文件的属性...然后在调用erase时排除具有该属性的所有文件。

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

像这样调用以下内容: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.

当然,如果您打算从目录中删除隐藏文件,这将不起作用。

#1


0  

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

将DONOTDELETE.ME替换为您的文件,该文件不会被删除。

@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:

您也可以只对相关文件进行attrib -r并删除之后的只读标志,如下所示:

@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

#2


0  

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=

#3


0  

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.

这有点hackish,但您可以只更改您不想删除的文件的属性...然后在调用erase时排除具有该属性的所有文件。

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

像这样调用以下内容: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.

当然,如果您打算从目录中删除隐藏文件,这将不起作用。