遇到.bat文件问题

时间:2011-10-31 18:18:34

标签: windows command-line batch-file command cmd

我有迷你递归功能,可以找到并隐藏所有.mta文件。它看起来像那样

For /r %1 in (*.mta) do attrib +h "%1"

当我尝试从命令提示符手动执行它时,它可以正常工作

enter image description here

我创建了.bat文件。代码看起来像那样

D:
For /r %1 in (*.mta) do attrib +h "%1"
pause 

试图执行此文件。开始'这个屏幕

enter image description here

代码出了什么问题?

2 个答案:

答案 0 :(得分:4)

尝试:

For /r %%i in (*.mta) do attrib +h "%%i"

(并将文件命名为.cmd,它看起来更现代。)

请勿使用引用脚本参数的%1,因此will not work。在命令行使用%,在批处理脚本中使用%%

答案 1 :(得分:0)

%1是传递给批处理文件的命令行参数,在这种情况下恰好是空的。这意味着您的for循环语法不正确。请改用其他变量名称,例如%f:

for /r %f in (*.mta) do attrib +h "%f"

如果你使用过你的版本,请不要这样做:

c:\> yourbatch.bat f
然后%f会扩展到'f'而你会得到:

for /r f in (*.mta) do attrib +h "f"

这也是不正确的。

相关问题