本文翻译自:How to delete files/subfolders in a specific directory at the command prompt in Windows
Say, there is a variable called%pathtofolder%
, as it makes it clear it is a full path of a folder.假设有一个名为%pathtofolder%
的变量,因为它很清楚它是文件夹的完整路径。
I want to delete every single file and subfolder in this directory, but not the directory itself.我要删除此目录中的每个文件和子文件夹,但不删除目录本身。
But, there might be an error like 'this file/folder is already in use'... when that happens, it should just continue and skip that file/folder.但是,可能会出现类似“此文件/文件夹已在使用中”的错误...当发生这种情况时,它应该继续并跳过该文件/文件夹。
Is there some command for this?有一些命令吗?
#1楼
参考:/question/8FOF/如何在Windows中的命令提示符下删除特定目录中的文件-子文件夹
#2楼
The simplest solution I can think of is removing the whole directory with我能想到的最简单的解决方案是使用以下命令删除整个目录
RD /S /Q folderPath
Then creating this directory again:然后再次创建此目录:
MD folderPath
#3楼
您可以通过使用以下命令删除所有内容和父文件夹本身来做到这一点:
RMDIR [/S] [/Q] [drive:]path
#4楼
这将删除文件夹和文件,并将文件夹留在后面。
pushd "%pathtofolder%" && (rd /s /q "%pathtofolder%" 2>nul & popd)
#5楼
Use:采用:
del %pathtofolder%\*.* /s /f /q
This deletes all files and subfolders in%pathtofolder%
, including read-only files, and does not prompt for confirmation.这将删除%pathtofolder%
所有文件和子文件夹(包括只读文件),并且不会提示您进行确认。
#6楼
@ECHO OFFSet dir=path-to-dirEcho Deleting all files from %dir%del %dir%\* /F /QEcho Deleting all folders from %dir%for /d %%p in (%dir%\*) Do rd /Q /S "%%p"@echo Folder deleted.exit
...deletes all files and folders underneath the given directory, but not the directory itself....删除给定目录下的所有文件和文件夹,但不删除目录本身。