使用forfiles时无法执行(错误2)
我想要一个单行程序来删除某个文件夹中超过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@file 的值是什么?运行代码时当前目录是什么?
winerror.h 表示错误 2,
因此我会尝试完全限定要删除的文件的路径。
--编辑:
你可以通过这样做
来代替
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
so I would try fully qualifying your path to the file that is getting deleted.
--EDIT:
which you can do by doing
instead of
我找到了答案:
这很好用。
I found the answer to this:
This works fine.
我使用的是 1998 年 4 月起的 FORFILES v 1.1。
在我的情况下,如果 @PATH 或 @FILE 包含带有空格的路径,则将无法正确处理。带有空格的路径需要用双引号引起来。由于 forfiles 中的执行命令需要双引号,因此我在批处理文件中使用双引号。 0x22 作为双引号保存到批处理文件中。我发现用双引号明确包围路径和文件有助于可靠地处理文件。
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.