2010-08-09 16:42:36 +0000 2010-08-09 16:42:36 +0000
43
43
Advertisement

如何通过命令提示符删除给定文件夹中的所有文件/子文件夹?

Advertisement

我想在Windows 7中删除一个批处理文件中的所有文件和子文件夹,并保留最上面的文件夹。基本上就是清空文件夹。请问有什么命令行指令可以实现?

Advertisement
Advertisement

答案 (11)

54
54
54
2010-08-09 16:46:23 +0000

你可以使用del/S标志(告诉它从所有子目录中删除所有文件):

del /S C:\Path\to\directory\*
23
23
23
2013-10-05 04:44:01 +0000

最好的解决方案:例如,我想删除父目录中的所有文件和子目录,比如说 “C:Users/Desktop/New文件夹"。 由于当前工作目录是父目录,即 "新文件夹",rmdir命令不能删除这个目录。

9
Advertisement
9
9
2014-07-03 12:38:29 +0000
Advertisement

导航至父目录:

pushd "Parent Directory"

删除子文件夹:

rd /s /q . 2>nul
6
6
6
2015-04-27 07:21:36 +0000

你可以使用rmdir来删除文件和子文件夹,如:

rmdir /s/q MyFolderPath

然而,在结构中使用del在rmdir之前,会明显快很多,特别是当你的结构中的子文件夹很多的时候,使用del在rmdir之前,如:

del /f/s/q MyFolderPath > nul
rmdir /s/q MyFolderPath
5
Advertisement
5
5
2013-07-31 18:23:00 +0000
Advertisement
rmdir "c:\pathofyourdirectory" /q /s

不要忘记使用引号,对于/q /s,它将删除所有的存储库,并且不需要提示。

3
3
3
2014-02-05 16:39:49 +0000

如果你想删除一个文件夹中的所有文件,包括所有的子文件夹,而不是依靠一些错误条件来保持根文件夹的完整性(就像我在另一个答案中看到的那样),你可以有一个像这样的批处理文件:

@echo off

REM Checking for command line parameter
if "%~1"=="" (

    echo Parameter required.
    exit /b 1

) else (

    REM Change directory and keep track of the previous one
    pushd "%~1"

    if errorlevel 1 (

        REM The directory passed from command line is not valid, stop here.
        exit /b %errorlevel%

    ) else (

        REM First we delete all files, including the ones in the subdirs, without confirmation
        del * /S /Q

        REM Then we delete all the empty subdirs that were left behind
        for /f %%D IN ('dir /b /s /a:d "%~1"') DO rmdir /S /Q "%%D"

        REM Change directory back to the previous one
        popd

        REM All good.
        exit /b 0
    )

)

然后你可以简单地调用:

empty_my_folder.bat "C:\whatever\is\my folder"
3
Advertisement
3
3
2017-02-02 19:20:53 +0000
Advertisement

只要在你的bat文件中放入这三个说明就可以快速、方便地完成:

mkdir empty_folder
robocopy /mir empty_folder "path_to_directory"
rmdir empty_folder
1
1
1
2016-11-10 17:16:53 +0000

要删除文件:

del PATH_TO_FILE

要删除文件夹中的所有文件:

rmdir /s /q PATH_TO_FOLDER

要删除特定文件夹中的所有文件(不是删除文件夹本身),有点复杂。所以需要两个命令:

del /q PATH_TO_FOLDER\*.*
for /d %i in (PATH_TO_FOLDER\*.*) do @rmdir /s /q "%i"

你可以创建一个脚本来删除任何你想要的文件(文件夹或文件),就像这样del /s *.*

@echo off
setlocal enableextensions

if "%~1"=="" (
    echo Usage: %0 path
    exit /b 1
)

:: check whether it is folder or file
set ISDIR=0
set ATTR=%~a1
set DIRATTR=%ATTR:~0,1%
if /i "%DIRATTR%"=="d" set ISDIR=1

:: Delete folder or file
if %ISDIR%==1 (rmdir /s /q "%~1") else (del "%~1")
exit /b %ERRORLEVEL%

使用示例:

mydel.bat "path\to\folder with spaces"
mydel.bat path\to\file_or_folder
1
Advertisement
1
1
2014-02-13 18:06:34 +0000
Advertisement

当我在文件夹名称中有空格的时候,这个方法对我来说效果更好。

@echo off
REM ---- Batch file to clean out a folder
REM Checking for command line parameter
if "%~1"=="" (

echo Parameter required.
exit /b 1

) else (
echo ***********************************************************************************
    echo ***Deleting all files, including the ones in the subdirs, without confirmation*** 
    del "%~1\*" /S /Q
echo ***********************************************************************************
    REM Deleting all the empty subdirs that were left behind
FOR /R "%~1" %%D IN (.) DO (
    if "%%D"=="%~1\." (
    echo ***Cleaning out folder: %~1*** 
    ) else (
    echo Removed folder "%%D"
    rmdir /S /Q "%%D"
    )
) 

    REM All good.
    exit /b 0

)
0
0
0
2020-02-17 22:41:30 +0000

user340956很痛苦地接近了这个解决方案,但你知道他们说的接近是什么意思…..

说白了,rd /s /q c:\foobar除了删除目标目录的内容外,还删除了目标目录,但你不一定要删除目录本身,有时你只想删除目录的内容,而不去管目录。deltree命令可以做到这一点,但Micrsoft以其无限的 “智慧 "删除了这个命令,并没有将其移植到Windows上。它可能是一个命令行脚本,而不是直接编写一个实际的可执行文件。它不设置任何环境变量,也不使用任何循环。

dt.bat (孩子们也可以用dt.cmd;管他呢,我老了,我用.bat):

:: dt is a Windows-compatible version of the deltree command
:: Posted to SuperUser by Synetech: https://superuser.com/a/1526232/3279

@echo off
goto start

:start
    if ["%~1"]==[""] goto usage
    pushd "%~1" 2>nul
    if /i not ["%cd%"]==["%~1"] goto wrongdir
    rd /s /q "%~1" 2>nul
    popd
goto :eof

:usage
    echo Delete all of the contents of a directory
    echo.
    echo ^> %0 DIR
    echo.
    echo %0 is a substitute for deltree, it recursively deletes the contents
    echo (files and folders) of a directory, but not the directory itself
    echo.
    echo DIR is the directory whose contents are to be deleted
goto :eof

:wrongdir
    echo Could not change to the target directory. Invalid directory? Access denied?
goto :eof

以下是它的工作原理:

1.它检查命令行参数是否被传递,如果没有,就打印出使用信息并退出。它使用pushd来保存当前目录,然后切换到目标目录,将任何错误重定向到nul,以获得更干净的命令行体验(和更干净的日志)。它检查当前目录是否与目标目录一致,如果不一致,则打印错误信息并退出。这样可以避免在pushd命令失败的情况下,不小心删除了前一个目录的内容(如。 - 这个检查是不区分大小写的,所以在Windows上是安全的,但不适用于任何大小写敏感的文件系统,比如那些*nix系统使用的文件系统,即使在Windows下也是如此。使用非短文件名会有一点不方便,但安全总比抱歉好,尤其是SFN并不完全可靠,也不总是可预测的(甚至可能完全禁用)。关于这一点的一些注意: - 因为目标目录是当前的目录,系统对它有一个开放的文件柄,因此它不能实际删除它,所以它保持原样,这也是我们想要的行为。 - 因为在目标目录的内容被删除后,它才会尝试删除目标目录,所以现在目标目录应该是空的(除了任何同样有打开的文件句柄的东西之外)。 5.最后,它使用C:\Users\Bob Bobson\foobar返回到之前的当前目录,结束脚本。

0
0
0
2018-05-07 18:43:18 +0000

我是这样做的。

  1. 导航到你要删除文件的文件夹里面。
  2. 键入:del * 3.Y为是。
  3. 完成
Advertisement

相关问题

3
19
10
28
8
Advertisement