批处理脚本 - dir /b 在输出文件中包含目录名称和空行

Batch script - dir /b is including directory names and empty lines in output file

我 运行 这个脚本列出超过 8 天的备份文件并在服务器中删除它们,最近它表现得很奇怪。请看下面

::Find backup files older than 8 days
::/p Specifies the path from which to start the search.
::/s Instructs the forfiles command to search into subdirectories recursively.
::/d Selects files with a last modified date within the specified time frame.
::/c Carries out the command specified by String and then stops.
::/b Displays a bare list of directories and files, with no additional information.

PUSHD \SERVER_NAME\M$\MSSQL
FORFILES /P Backup /s /d -8 /C "cmd /c dir /B @file">K:\ScriptLogs\OutPut
POPD

这最近工作正常,直到我在 \SERVER_NAME\M$\MSSQL\Backup 中创建了更多文件夹,如 FULL、DIFF、LOG as

\SERVER_NAME\M$\MSSQL\Backup\FULL
\SERVER_NAME\M$\MSSQL\Backup\DIFF
\SERVER_NAME\M$\MSSQL\Backup\LOG

并且 K:\ScriptLogs\OutPut 文件现在包含文件夹名称 FULL,如下所示

FULL
SERVER_NAME_DB_DIFF_2021_06_10_040135.diff
SERVER_NAME_DB_DIFF_2021_06_11_040124.diff
SERVER_NAME_DB_DIFF_2021_06_12_040213_1.diff

我无法理解为什么它在输出列表中包含 FULL(如果我的命令有误,它也应该包含 DIFF 和 LOG)。

当我修改如下代码时

PUSHD \SERVER_NAME\M$\MSSQL
FORFILES /P Backup /s /d -8 /C "cmd /c dir /A-D /B @file">K:\ScriptLogs\OutPut
POPD

它给出如下错误,输出文件现在不包括 FULL(因为 /A-D 排除了目录)

K:\DBScripts>PUSHD \SERVER_NAME\M$\MSSQL

W:\MSSQL>FORFILES /P Backup /s /d -8 /C "cmd /c dir /A-D /B @file" 1>K:\ScriptLogs\OutPut
File Not Found
File Not Found

W:\MSSQL>POPD

我关心的是为什么它在旧代码版本中包含完整目录名称,而代码 运行 在新版本中给出错误。感谢您的帮助。

感谢@compo ....

PUSHD \SERVER_NAME\M$\MSSQL
FORFILES /P Backup /S /D -%NUMBER_OF_DAYS% /C "cmd /Q /D /C \"If @IsDir==FALSE For %%G In (@File) Do Echo %%~G\"" 1>K:\ScriptLogs\OutPut-%dtStamp%
POPD

这解决了问题。