如何在Windows中的命令提示符下删除特定目录中的文件/子文件夹

本文翻译自: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楼

参考:https://stackoom.com/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 OFF

Set dir=path-to-dir

Echo Deleting all files from %dir%
del %dir%\* /F /Q

Echo 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. ...删除给定目录下的所有文件和文件夹,但不删除目录本身。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值