如何使用脚本从 Windows 中的给定目录中删除空文件夹

Nic*_*tti 3 windows-7 cmd.exe

我使用r.js作为构建工具,但截至今天,该工具无法删除构建目录中的空文件夹。我找到了这两个脚本

for /f "usebackq" %%d in ("dir /ad/b/s | sort /R") do rd "%%d"

for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i">NUL
Run Code Online (Sandbox Code Playgroud)

环顾网络,但我总是得到

%%i was unexpected at this time.
Run Code Online (Sandbox Code Playgroud)

或者

%%d was unexpected at this time.
Run Code Online (Sandbox Code Playgroud)

而且我不知道如何告诉脚本我的目录在哪里。

我的构建脚本是

@echo off
where /q r.js || (
    echo requirejs node package is not installed. You must install node, npm and then run npm install -g requirejs
    goto :eof
)
node r.js -o app.build.js
:end
Run Code Online (Sandbox Code Playgroud)

我需要告诉脚本删除位于里面的所有空目录 ../../js

Den*_*nis 5

由于我从未完全理解的原因for,在批处理文件内部和外部使用不同的语法。

此命令应该在批处理文件中工作

for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i"
Run Code Online (Sandbox Code Playgroud)

在命令提示符(无批处理文件)中,您必须替换%%i%i

for /f "delims=" %i in ('dir /s /b /ad ^| sort /r') do rd "%i"
Run Code Online (Sandbox Code Playgroud)

此外,要抑制The directory is not empty.错误消息,请使用rd "%i" 2>NUL.

一个简单的rd "%i">NUL重定向输出(stdout),而不是错误消息(stderr)。