工作命令 - 直接粘贴在命令shell中:
for /f %a in ('dir /b /s ..\*') do @echo %~na | findstr transport_bb_*_*_* | sort
问题1)从.bat文件执行的相同命令导致错误:
| was unexpected at this time.
问题2)另外,希望提到的.bat文件接受参数(目录):
for /f %a in ('dir /b /s %1\..\*') do @echo %~na | findstr transport_bb_*_*_* | sort
尝试撇号,“严重口音”,双倍百分比,使用SET将整个命令放在括号内,仍然无效。
帮助表示赞赏。
谢谢, 巴特
答案 0 :(得分:0)
ad 1)在批处理文件中,您必须加倍%
- > %%
。管道|
您必须用工作流程替换:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "delims=" %%A in ('dir /B /S ..\*') do (
echo %~nA
set "search_in_file=%%A"
findstr transport_bb_*_*_* !search_in_file! | sort
)
ad 2)接受用户输入
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "input=%~1"
SET "search_in_dir=!input!\*"
for /F "delims=" %%A in ('dir /B /S !search_in_dir!') do (
REM echo %%~nA
set "search_in_file=%%A"
REM ECHO findstr "transport_bb_*_*_*" "%search_in_file%"
findstr "transport_bb_*_*_*" "!search_in_file!"
IF ERRORLEVEL 1 (
ECHO "String NOT found in file !search_in_file!"
) ELSE (
ECHO ""String found in file !search_in_file!"
)
)
或更像您的脚本:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "input=%~1"
SET "search_in_dir=!input!\*"
for /F "delims=" %%A in ('dir /B /S !search_in_dir!') do (
set "search_in_file=%%A"
findstr /M "transport_bb_*_*_*" "!search_in_file!" | sort
)
备注:强>
其中findstr参数:
/M Prints only the filename if a file contains a match.
%~1
...接收第一个用户参数并展开它
!....!
现在由于启用了SETLOCAL ENABLEDELAYEDEXPANSION
而界定了变量。