【发布时间】:2018-03-13 03:22:01
【问题描述】:
将我的脚本拆分为两个批处理文件,因为我不知道如何组合它:
简短,让你有一个想法:
1.bat :使用 args 调用 2.bat,对其输出进行排序并使行唯一
FOR /F ... (2.bat %1 ^| sort) DO (...)
do 部分将行相互比较,并使用 linebuffer 排除相同的行
2.bat :作为 FOR 循环的结果打印字符串
FOR ... DO ECHO
在 bash 中这可能看起来像这样:(管道很容易)
(command) | while read line ; do echo $line ; done | sort | uniq
我真的不知道如何在 Windows 批处理中处理 FOR 循环的最终输出以将其结果用于另一个循环(不使用临时文件进行结果缓冲)
这是应用了 jeb 解决方案的整个代码:
@echo off
setlocal EnableDelayedExpansion
REM *** This "goto" to a label, if there is one embedded in %~0 -
FOR /F "delims=: tokens=3" %%L in ("%~0") do goto :%%L
REM *** The second part calls this batch file, but embedd a label to be called
FOR /F "usebackq" %%A IN (`echo dummy ^| call %~d0\:main:\..\%~pnx0 %1 ^| SORT`) DO (
IF NOT "%%A"=="!buff!" SET str=!str!%%A
SET buff=%%A
)
ECHO %str:.dll=;%
endlocal
GOTO :eof
REM *** This function will be called in the pipe, but runs in batch context
:main
CD %1
:sub
FOR %%A IN (*.dll *.exe) DO dumpbin /DEPENDENTS %%A | FINDSTR /I "DLL" | FINDSTR /V "Dump :"
FOR /D %%B IN (*) DO (
CD %%B
CALL :sub
CD..
)
GOTO :eof
它返回一行,其中包含 文件夹 [arg1] 及其所有子文件夹中所有文件的运行时依赖项:输出字符串的 .dll 部分被剥离为适合我的需要
ADVAPI32;COMCTL32;COMDLG32;GDI32;KERNEL32;ole32;SHELL32;USER32;WININET;
这是 osx 上使用 pev 工具的对应物:
#!/bin/sh
find "${1}" -type f \( -iname "*.dll" -o -iname "*.exe" \) | while read pe ; do
readpe -i "${pe}" | grep -i '.dll' | awk '{print $2}'
done | sort | uniq | sed 's/.[dD][lL][lL]//g;' | tr '\n' ';' ## have the BSD sed release and can't use I with sed
【问题讨论】:
标签: windows batch-file for-loop