删除文件夹内容,但不删除文件夹 [英] Deleting Folder Contents but not the folder
问题描述
我有一个文件夹C:\Epson Scans,我试图找出如何写一个脚本,将删除文件夹的内容,但保留文件夹完好无损。我已经想出了如何删除整个文件夹,我可以重新创建它。但我想知道是否有人知道一种方法,只是删除文件夹中的内容,而不是实际删除文件夹。任何帮助这将非常感谢!
编辑:插入工作代码,以便我可以循环通过许多计算机,并一次做。有人可以告诉我为什么代码不工作,我已经插入它?
@echo off
setlocal enabledelayedexpansion
set Delete_success = 0
set total = 0
for / F %% G in(pclist.txt)do(
set / a total + = 1
pushdC:\ Epson Scans|| exit / B 1
for / D %% I in(*)do(
rd / S / Q%%〜I
)
del / Q*
popd
if!ERRORLEVEL!== 0(
set / a Delete_success + = 1
)else(
echo EpsonDelete copy failed failed %% G>> EpsonDelete_FailedPCs.txt
)
)
echo删除成功:%Delete_success%/%total%>> EpsonDelete_FileCopy.txt
del
只删除文件,因此 del / S / QC:\ Epson Scans
指定文件夹和子文件夹中的所有文件(由于 / S
)。
rmdir
删除文件夹,因此指定 rmdir / S / QC:\ Epson Scans
爱普生扫描本身。
当然,您可以执行 mkdirC:\Epson Scans
,然后再次新创建已删除的文件夹 1 ,但没有要求。所以正确的答案是使用 for / D
循环 C:\Epson Scans
包含,然后使用 del / Q
删除文件:
pushdC:\Epson Scans|| exit / B 1
for / D %% I in(*)do(
rd / S / Q%%〜I
)
del / *
popd
注意 rd
与 rmdir
相同 - 另请参阅此帖子:什么是MD和MKDIR批处理命令?
1)注意,如果你这样做,一些文件夹属性会丢失,此外,这种情况会丢失,因为Windows不区分大小写处理路径。
I have a folder C:\Epson Scans, I am trying to figure out how to write a script that will delete the contents of the folder but leave the folder intact. I have figured out how to delete the entire folder and I could recreate it. But I wanted to know if anyone knows a way of just deleting the contents inside the folder and not actually deleting the folder. Any help with this would be greatly appreciated!
Edit: Inserting working code so I can loop through many computers and do it at once. Will someone please tell me why the code is not working where I have inserted it?
@echo off
setlocal enabledelayedexpansion
set Delete_success=0
set total=0
for /F %%G in (pclist.txt) do (
set /a total+=1
pushd "C:\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
if !ERRORLEVEL!==0 (
set /a Delete_success+=1
) else (
echo EpsonDelete copy failed on %%G>>EpsonDelete_FailedPCs.txt
)
)
echo Delete Success: %Delete_success%/%total% >>EpsonDelete_FileCopy.txt
del
deletes files only, so del /S /Q "C:\Epson Scans"
deletes all files in the given folder and sub-folders (due to /S
).
rmdir
deletes folders, so specifying rmdir /S /Q "C:\Epson Scans"
also deletes the folder Epson Scans
itself.
Of course you could execute mkdir "C:\Epson Scans"
afterwards to newly create the deleted folder again1, but this was not asked for. So the correct answer is to use a for /D
loop over C:\Epson Scans
and delete each folder it contains, and then use del /Q
to delete the files:
pushd "C:\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
Note that rd
is the same as rmdir
-- see also this post: What is the difference between MD and MKDIR batch command?
1) Regard that some folder attributes get lost if you do that, for example the owner. Also the case is lost as Windows treats paths case-insensitively.
这篇关于删除文件夹内容,但不删除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!