【问题标题】:assigning a piped command to the variable batch将管道命令分配给变量批处理
【发布时间】:2021-02-04 07:59:20
【问题描述】:

我在分配一个计算目录中文件出现次数的命令时遇到问题。 我发现我应该这样做 - (^ |) 但是当我这样做并回显变量时,没有显示任何内容,并且应该出现很多次。

我的代码:

@echo off
setlocal enableextensions

for /f "tokens=*" %%i in ('dir /b C:\Users\AD10FC\IdeaProjects\collateral\db-resources\release | findstr /B "!first%!-" | find /c /v ""') do set VAR=%%i

echo %VAR%

【问题讨论】:

  • 这是^|,而不是^ |
  • !first%! 是什么意思——应该是%first%,还是!first!?如果是后者,请将EnableDelayedExpansion 添加到setlocal 命令...

标签: batch-file variables


【解决方案1】:

您的代码中无需使用findstr.exe 来过滤前导字符串,因为您可以直接在Dir 中过滤它们:

@Echo Off
SetLocal EnableExtensions
Set "first=LeadingString"
For /F "EOL=? Delims=" %%G In ('"Dir /B /A:-D "%UserProfile%\IdeaProjects\collateral\db-resources\release\%first%-*" | "%SystemRoot%\System32\find.exe" /V /C """') Do Set "VAR=%%G"
Echo(%VAR%
Pause

对于您正在执行的特定任务,您可能会发现以下方法更适合您:

@Echo Off
SetLocal EnableExtensions
Set "first=LeadingString"
For /F %%G In ('""%SystemRoot%\System32\xcopy.exe" "%UserProfile%\IdeaProjects\collateral\db-resources\release\%first%-*" . /LQH"') Do Set "VAR=%%G"
Echo(%VAR%
Pause

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 2013-04-18
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2011-02-26
    相关资源
    最近更新 更多