删除文件夹内容但不删除文件夹 [英] Deleting Folder Contents but not the folder
问题描述
我有一个文件夹 C:Epson Scans,我想弄清楚如何编写一个脚本来删除文件夹的内容但保持文件夹完好无损.我已经想出了如何删除整个文件夹,我可以重新创建它.但我想知道是否有人知道只删除文件夹内的内容而不实际删除文件夹的方法.对此的任何帮助将不胜感激!
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!
插入工作代码,这样我就可以在多台计算机上循环并一次完成.有人能告诉我为什么代码在我插入的地方不起作用吗?
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
只删除文件,所以 del/S/Q "C:Epson Scans"
删除给定文件夹和子文件夹中的所有文件(由于 /S
).
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
删除文件夹,因此指定 rmdir/S/Q "C:Epson Scans"
也会删除文件夹 Epson Scans
本身.
rmdir
deletes folders, so specifying rmdir /S /Q "C:Epson Scans"
also deletes the folder Epson Scans
itself.
当然你可以在之后执行 mkdir "C:Epson Scans"
重新创建删除的文件夹1,但这不是要求的.所以正确的答案是在 C:Epson Scans
上使用 for/D
循环并删除它包含的每个文件夹,然后使用 del/Q代码>删除文件:
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
请注意,rd
与 rmdir
相同 -- 另请参阅此帖子:MD 和 MKDIR 批处理命令有什么区别?
Note that rd
is the same as rmdir
-- see also this post: What is the difference between MD and MKDIR batch command?
1) 请注意,如果您这样做,某些文件夹属性会丢失,例如所有者.由于 Windows 不区分大小写地处理路径,因此大小写也会丢失.
这篇关于删除文件夹内容但不删除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!