使用forfiles时无法执行(错误2)

发布于 2024-10-06 15:26:44 字数 287 浏览 0 评论 0原文

我想要一个单行程序来删除某个文件夹中超过 14 天的所有文件。

命令

这是我的files -px:\logs -s -m*.log -d-14 -c"CMD /C del @file"

,但执行时它为每个文件输出“无法执行(错误 2)”发现。 如果省略 -c "CMD /c del @file " ,它可以正常生成超过 14 天的文件列表。我首先认为这可能是权限问题;但对文件的任何操作都会产生相同的结果,并且我是计算机的管理员。

然而,如果这有区别的话,那就是网络共享。

谢谢

I wanted a one liner to delete all files older than 14 days in certain folder.

This is my command

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @file"

but when executed it outputs "can't execute (error 2)" for every file it finds.
If the -c "CMD /c del @file " is omitted it works fine producing a list of files older than 14 days. I first thought it might be a permission issue; but any action on the file produces the same result and I am admin on the comptuer.

It is however a network share if that makes a diffrence.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天的雪花 2024-10-13 15:26:44

@file 的值是什么?运行代码时当前目录是什么?

winerror.h 表示错误 2,

#define ERROR_FILE_NOT_FOUND             2L

因此我会尝试完全限定要删除的文件的路径。

--编辑:

你可以通过这样做

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @path"

来代替

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @file"

What is the value of @file, and what is the current directory when you're running the code?

winerror.h says that error 2 is

#define ERROR_FILE_NOT_FOUND             2L

so I would try fully qualifying your path to the file that is getting deleted.

--EDIT:

which you can do by doing

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @path"

instead of

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @file"
〆一缕阳光ご 2024-10-13 15:26:44

我找到了答案:

set DP_DIR=C:\Scripts

forfiles -p%DP_DIR% -s -m*.exe -d-3 -c"cmd /c del \"@FILE""

这很好用。

I found the answer to this:

set DP_DIR=C:\Scripts

forfiles -p%DP_DIR% -s -m*.exe -d-3 -c"cmd /c del \"@FILE""

This works fine.

離殇 2024-10-13 15:26:44

我使用的是 1998 年 4 月起的 FORFILES v 1.1。

在我的情况下,如果 @PATH 或 @FILE 包含带有空格的路径,则将无法正确处理。带有空格的路径需要用双引号引起来。由于 forfiles 中的执行命令需要双引号,因此我在批处理文件中使用双引号。 0x22 作为双引号保存到批处理文件中。我发现用双引号明确包围路径和文件有助于可靠地处理文件。

forfiles -pC:\pictures\ -m*.jpg -d-30 -c"cmd /c echo del 0x22@PATH\@FILE0x22" > c:\temp\delold_pics.bat

call c:\temp\delold_pics.bat

I am using FORFILES v 1.1 from April 1998.

In my situation, if the @PATH or @FILE contains a path with a space in it, this will not be handled correctly. Paths with spaces require double quotes around it. Since the executing command in forfiles requires double quotes, this is what I use in my batch files. The 0x22 gets saved to batch file as double quotes. I find that explicitly surrounding the path and file with double quotes make for reliable processing of files.

forfiles -pC:\pictures\ -m*.jpg -d-30 -c"cmd /c echo del 0x22@PATH\@FILE0x22" > c:\temp\delold_pics.bat

call c:\temp\delold_pics.bat
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文