【问题标题】:Variables and wildcards in batch and cmd.exe批处理和 cmd.exe 中的变量和通配符
【发布时间】:2016-12-07 16:24:05
【问题描述】:

我正在尝试在所有驱动器中搜索文件名。单独使用dir 命令时一切正常:

C:\>dir /s C:\*blabla*
 Volume in drive C has no label.
 Volume Serial Number is 2AB1-3FBD

  Directory of C:\Users\Administrator\Desktop

 02/08/2016  11:21                 0 blabla.txt
           1 File(s)              0 bytes

 Total Files Listed:
           1 File(s)              0 bytes
           0 Dir(s)  28?369?825?792 bytes free

但是在循环中使用它会失败:

FOR /F "skip=1 delims=" %x in ('wmic logicaldisk get caption') do dir /s %x*blabla*

*blabla* Administrator\Desktop>dir /s A:       
The device is not ready.

*blabla* Administrator\Desktop>dir /s C:       
Volume in drive C has no label.

有人可以帮我吗?

【问题讨论】:

  • 我似乎无法复制这一点。我是否正确阅读了调用 dir /s C: 时出现的唯一行是“驱动器 C 中的卷没有标签”还是还有更多?
  • 尝试 FOR /F "skip=1 delims=" %x in ('wmic logicaldisk get caption') do dir /s %x\*blabla*delims 有一个空格作为附加标记。我还在 %x\*blabla* 中添加了一个反斜杠,用于 logicaldisk get caption 只返回驱动器名和冒号,而不是反斜杠
  • 这个更好FOR /F "skip=1 delims= " %x in ('wmic logicaldisk get caption') do echo(%x | find ":">NUL && dir /s %x\*blabla*
  • 试试这个:for /F "skip=1" %X in ('wmic LogicalDisk GET Caption') do for /F %Y in ("%X") do dir /S "%Y\*blabla*";嵌套另一个for /F 循环以避免wmic 命令的Unicode 输出转换导致的伪影...
  • 你也可以试试这个命令Where只需输入Where /?

标签: windows batch-file cmd


【解决方案1】:

感谢您的所有帮助,以及对其他脚本的好主意。

我一直在寻找的答案在哪里:

C:\>FOR /F "skip=1 delims= " %x in ('wmic logicaldisk get caption') do @where /R %x *blabla* 2>NULL
C:\Users\Administrator\Desktop\blabla.txt

【讨论】:

  • 请注意,where 仅在 %path% 变量的文件夹中查找。如果您的文件在其他地方,您需要dir 解决方案之一(我会添加/b 开关)
猜你喜欢
  • 1970-01-01
  • 2010-12-17
  • 2013-09-20
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多