Board logo

标题: [文件管理] dlls.cmd 查看或打包文件的所有 dll 库依赖 [打印本页]

作者: CrLf    时间: 2015-12-14 02:08     标题: dlls.cmd 查看或打包文件的所有 dll 库依赖

本帖最后由 CrLf 于 2015-12-14 02:12 编辑

用于查看文件的一些基础信息:架构、子系统、dll 依赖
主要用于查看 dll 依赖,交叉引用的情况也可识别
是 Batch-CN 目前使用的打包工具
http://www.bathome.net/s/tool/?key=dlls&down=dlls
所依赖的第三方工具均已打包

dlls.cmd  查看或打包文件的所有 dll 库依赖  by CrLf [bbs.bathome.net]

dlls.cmd  FILES [/HELP] [/RAR]

    FILES   指定一个或多个要处理的 exe 或 dll
    /RAR    打包相关依赖文件为 Batch-CN 的格式
    /HELP   显示这个帮助

依赖第三方工具: Peinfo / ldd / ldd64 / rar

作者: CrLf    时间: 2015-12-14 02:08

本帖最后由 CrLf 于 2015-12-14 17:23 编辑
  1. @setlocal enabledelayedexpansion
  2. @set echo=off
  3. @for /f "delims=" %%a in ('echo') do @(
  4. if "%%a"=="ECHO 处于打开状态。" @set echo=on
  5. if "%%a"=="ECHO is on." @set echo=on
  6. )
  7. @echo off
  8. path .;!path!
  9. if /i "%~1" == "" goto :Usage
  10. for %%a in ("%~1" "%~2" "%~3" "%~4" "%~5" "%~6" "%~7" "%~8" "%~9") do (
  11. if /i "%%~a" == "/?" goto :Usage
  12. if /i "%%~a" == "/HELP" goto :Usage
  13. )
  14. set "RAR="
  15. for %%a in ("%~1" "%~2" "%~3" "%~4" "%~5" "%~6" "%~7" "%~8" "%~9") do (
  16. if /i "%%~a" == "/RAR" set RAR=true
  17. )
  18. for %%a in ("%~1" "%~2" "%~3" "%~4" "%~5" "%~6" "%~7" "%~8" "%~9") do (
  19. if exist %%~fa (
  20. set "file=%~1"
  21. set "file~dpn=%~dpn1"
  22. call :main
  23. )
  24. )
  25. goto :End
  26. :End
  27. (
  28. echo !echo!
  29. endlocal
  30. exit /b
  31. )
  32. :main
  33. setlocal
  34. set "count=0"
  35. set "error="
  36. path !file!\..;!path!
  37. for /f "skip=1 tokens=1,3" %%a in ('@"%~dpn0\peinfo" "!file!" /Subsystem /Machine') do (
  38. set "%%a=%%b"
  39. )
  40. echo 文件名: !file!
  41. if /i !Machine! == 0x0000 (echo 架构:UNKNOWN
  42. ) else if /i !Machine! == 0x014c (echo 架构:Intel 386.
  43. ) else if /i !Machine! == 0x0160 (echo 架构:big-endian
  44. ) else if /i !Machine! == 0x0166 (echo 架构:MIPS little-endian
  45. ) else if /i !Machine! == 0x0168 (echo 架构:MIPS little-endian
  46. ) else if /i !Machine! == 0x0169 (echo 架构:MIPS little-endian WCE v2
  47. ) else if /i !Machine! == 0x0184 (echo 架构:Alpha_AXP
  48. ) else if /i !Machine! == 0x01a2 (echo 架构:SH3 little-endian
  49. ) else if /i !Machine! == 0x01a3 (echo 架构:SH3DSP
  50. ) else if /i !Machine! == 0x01a4 (echo 架构:SH3E little-endian
  51. ) else if /i !Machine! == 0x01a6 (echo 架构:SH4 little-endian
  52. ) else if /i !Machine! == 0x01a8 (echo 架构:SH5
  53. ) else if /i !Machine! == 0x01c0 (echo 架构:ARM Little-Endian
  54. ) else if /i !Machine! == 0x01c2 (echo 架构:THUMB
  55. ) else if /i !Machine! == 0x01d3 (echo 架构:AM33
  56. ) else if /i !Machine! == 0x01F0 (echo 架构:IBM PowerPC Little-Endian
  57. ) else if /i !Machine! == 0x01f1 (echo 架构:POWERPCFP
  58. ) else if /i !Machine! == 0x0200 (echo 架构:Intel 64
  59. ) else if /i !Machine! == 0x0266 (echo 架构:MIPS
  60. ) else if /i !Machine! == 0x0284 (echo 架构:ALPHA64
  61. ) else if /i !Machine! == 0x0366 (echo 架构:MIPS
  62. ) else if /i !Machine! == 0x0466 (echo 架构:MIPS
  63. ) else if /i !Machine! == 0x0284 (echo 架构:ALPHA64
  64. ) else if /i !Machine! == 0x0520 (echo 架构:Infineon
  65. ) else if /i !Machine! == 0x0CEF (echo 架构:CEF
  66. ) else if /i !Machine! == 0x0EBC (echo 架构:EFI Byte Code
  67. ) else if /i !Machine! == 0x8664 (echo 架构:AMD64 (K8)
  68. ) else if /i !Machine! == 0x9041 (echo 架构:M32R little-endian
  69. ) else if /i !Machine! == 0xC0EE (echo 架构:CEE
  70. ) else (echo Error: 获取 pe 信息失败 1>&2 & exit /b 1)
  71. if /i !Subsystem! == 0x0002 (echo 子系统:GUI
  72. ) else if /i !Subsystem! == 0x0003 (echo 子系统:CLI
  73. ) else (echo 子系统:UNKNOWN[!Subsystem!]
  74. )
  75. for /f "delims==" %%a in ('set dll[ 2^>nul') do set "%%a="
  76. echo;
  77. echo 依赖:
  78. if /i !Machine! == 0x014c (
  79. call :lddlist ldd file "%~2"
  80. ) else if /i !Machine! == 0x8664 (
  81. call :lddlist ldd64 file "%~2"
  82. )
  83. if !count! == 0 (
  84. echo [无依赖]
  85. ) else if defined error (
  86. echo;
  87. echo Error: 未能取得所有 dll,跳过打包流程
  88. ) else if defined RAR (
  89. echo;
  90. echo !file!
  91. copy /y "!file!" "!file~dpn!\"
  92. echo;
  93. if not exist !file~dpn!.cmd call :writecmdfile>!file~dpn!.cmd
  94. echo ---------------------------------------------------------------------------
  95. "%~dpn0\rar" a -m5 -s "!file~dpn!.rar" "!file~dpn!.cmd" "!file~dpn!"
  96. )
  97. endlocal
  98. exit /b
  99. :Usage
  100. set "myname=%~nx0"
  101. echo !myname!  查看或打包文件的所有 dll 库依赖  by CrLf [bbs.bathome.net]
  102. echo;
  103. echo !myname!  FILES [/HELP] [/RAR]
  104. echo;
  105. echo     FILES   指定一个或多个要处理的 exe 或 dll
  106. echo     /RAR    打包相关依赖文件为 Batch-CN 的格式
  107. echo     /HELP   显示这个帮助
  108. echo;
  109. echo 依赖第三方工具: Peinfo / ldd / ldd64 / rar
  110. goto :End
  111. :lddlist
  112. for /f "skip=1 tokens=1,2 delims=>" %%a in ('@"%~dpn0\%~1" "!%~2!"') do (
  113. if not defined dll[%%~nxa] (
  114. for /f "tokens=*" %%c in ("%%b\..") do (
  115. if defined RAR echo;
  116. if /i %%~dpc == c:\windows\ (
  117. echo %%~nc : %%b
  118. if defined RAR echo 跳过
  119. ) else (
  120. set "dllpath=%%b"
  121. for /f %%d in ("%%a") do (
  122. set "dllname=%%~nx$path:d"
  123. set "dllpath=%%~f$path:d"
  124. )
  125. if defined dllpath (
  126. set /a count+=1
  127. echo !dllname! : !dllpath!
  128. if defined RAR (
  129. md "!file~dpn!" 2>nul
  130. if exist !file~dpn!\nul copy /y "!dllpath!" "!file~dpn!\"
  131. )
  132. call :lddlist %1 dllpath "%~3"
  133. ) else (
  134. set" error=true"
  135. echo %%~nxb : 未在 %%path%% 中找到相应文件
  136. )
  137. )
  138. )
  139. set dll[%%a]=true
  140. )
  141. )
  142. exit /b
  143. :writecmdfile
  144. echo @"%%~dp0\%%~n0\%%~n0" %%*
  145. exit /b
复制代码

作者: codegay    时间: 2015-12-14 03:50

亩产万斤。
作者: hfxiang    时间: 2022-6-21 11:06

本帖最后由 hfxiang 于 2022-6-21 11:07 编辑

利用楼主的工具及思路,做了个测试pe文件 Subsystem及Machine 的脚本,以便得知所下载的应用其使用环境。请指教。

示例:peinfo.cmd 7za.exe 7za64.exe ldd.exe ldd64.exe

第 1 个文件:
"G:\Program Files\depends_dlls\depends\7za.exe"
Machine = [0x014C] - Intel 386.
Subsystem = [0x0003] - CLI

第 2 个文件:
"G:\Program Files\depends_dlls\depends\7za64.exe"
Machine = [0x8664] - AMD64 (K8)
Subsystem = [0x0003] - CLI

第 3 个文件:
"G:\Program Files\depends_dlls\depends\ldd.exe"
Machine = [0x014C] - Intel 386.
Subsystem = [0x0003] - CLI

第 4 个文件:
"G:\Program Files\depends_dlls\depends\ldd64.exe"
Machine = [0x8664] - AMD64 (K8)
Subsystem = [0x0003] - CLI



::------------------------------------以下部分为脚本内容,请以ANSI格式保存为PeInfo.cmd------------------------------------

@setlocal enabledelayedexpansion
@rem 保存echo状态
@set echo=off
@for /f "delims=" %%a in ('echo') do @(
        if "%%a"=="ECHO 处于打开状态。" @set echo=on
        if "%%a"=="ECHO is on." @set echo=on
)
@echo off
echo;正在执行:&&echo;"%~0" %*&&echo;

@rem 设置文件计数器
set /a nnn=0

@rem 判断参数是否包含文件夹
set "IsFolder="
for %%a in (%*) do (
        set "Dir_a=%%~aa"
        if defined Dir_a if "[!Dir_a:~0,1!]"=="[d]" set "IsFolder=%%~a"
)

@rem 添加文件查询路径(主要为%~dp0及%~dp1)
echo;%path%|find.exe /i "%~dp0;">nul 2>&1 || path %~dp0;%path%
if defined IsFolder echo;%path%|find.exe /i "%IsFolder%;">nul 2>&1 || path %IsFolder%;%path%

@rem 设置工作目录
cd /d "%~dp0"
if defined IsFolder cd /d "%IsFolder%"

@rem 测试peinfo.exe文件路径
set "FilePath="
for %%a in ("peinfo.exe") do (
        if not "[%%~$path:a]"=="[]" for %%b in ("%%~$path:a") do (
                set "Dir_a=%%~ab"
                if defined Dir_a if not "[!Dir_a:~0,1!]"=="[d]" set "FilePath=%%~fb"
        )
)
if not defined FilePath  echo;&&echo;“peinfo.exe”指令文件(应用程序)不在PATH环境变量指定的路径中,请先安装“peinfo.exe”应用程序后再重试&&echo;请按任意键退出脚本&&goto :ExitBatch
set "peinfo_path=%FilePath%"

if defined IsFolder (
        @rem echo;文件夹优先级高于文件,本次只处置文件夹 %IsFolder% 下的 exe 文件
        for /r "%IsFolder%" %%a in (*.exe) do if exist "%%~fa" call :Detach_file "%peinfo_path%" "%%~fa"
) else (
        @rem echo;本次处理的是参数列表中的文件
        for %%a in (%*) do if exist "%%~fa" call :Detach_file "%peinfo_path%" "%%~fa"
)

:ExitBatch
:End
(
        echo !echo!
        if /i %nnn% EQU 0 echo;未找到指定文件,请做相应检查后重试!!!&&echo;
        endlocal
        echo;脚本运行结束,请按任意键(建议按“空格键”)退出。
        pause>nul
        exit /b
)

:Detach_file
call :Pe_Info "%~f1" "%~f2" Machine Subsystem
set /a nnn+=1
echo;
echo;第 !nnn! 个文件:
echo;"%~f2"
echo;
echo;Machine = %Machine%
echo;Subsystem = %Subsystem%
echo;
echo;校验指令:
echo;"%~f1" "%~f2" /Subsystem /Machine
echo;
exit /b

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Pe_Info <Find_PE_info_app> <PE_info_to_be_found> [Machine] [Subsystem]
:: 说明: 查找应用程序Machine及Subsystem;
::  %1        输入:Find_PE_info_app    -- 工具程序PeInfo.exe全路径
::  %2        输入:PE_info_to_be_found -- 待查找应用程序全路径
::  %3        输出:Machine             -- CPU架构
::  %4        输出:Subsystem           -- 子系统
setlocal enabledelayedexpansion
set "Machine="
set "Subsystem="
for /f "skip=1 tokens=1,3" %%a in ('@"%~f1" "%~f2" /Subsystem /Machine 2^>nul') do set "%%a=%%b"

if /i "[%Machine%]" == "[0x0000]" (set "Machine=[%Machine%] - UNKNOWN"
) else if /i "[%Machine%]" == "[0x014c]" (set "Machine=[%Machine%] - Intel 386."
) else if /i "[%Machine%]" == "[0x0160]" (set "Machine=[%Machine%] - big-endian"
) else if /i "[%Machine%]" == "[0x0166]" (set "Machine=[%Machine%] - MIPS little-endian"
) else if /i "[%Machine%]" == "[0x0168]" (set "Machine=[%Machine%] - MIPS little-endian"
) else if /i "[%Machine%]" == "[0x0169]" (set "Machine=[%Machine%] - MIPS little-endian WCE v2"
) else if /i "[%Machine%]" == "[0x0184]" (set "Machine=[%Machine%] - Alpha_AXP"
) else if /i "[%Machine%]" == "[0x01a2]" (set "Machine=[%Machine%] - SH3 little-endian"
) else if /i "[%Machine%]" == "[0x01a3]" (set "Machine=[%Machine%] - SH3DSP"
) else if /i "[%Machine%]" == "[0x01a4]" (set "Machine=[%Machine%] - SH3E little-endian"
) else if /i "[%Machine%]" == "[0x01a6]" (set "Machine=[%Machine%] - SH4 little-endian"
) else if /i "[%Machine%]" == "[0x01a8]" (set "Machine=[%Machine%] - SH5"
) else if /i "[%Machine%]" == "[0x01c0]" (set "Machine=[%Machine%] - ARM Little-Endian"
) else if /i "[%Machine%]" == "[0x01c2]" (set "Machine=[%Machine%] - THUMB"
) else if /i "[%Machine%]" == "[0x01d3]" (set "Machine=[%Machine%] - AM33"
) else if /i "[%Machine%]" == "[0x01F0]" (set "Machine=[%Machine%] - IBM PowerPC Little-Endian"
) else if /i "[%Machine%]" == "[0x01f1]" (set "Machine=[%Machine%] - POWERPCFP"
) else if /i "[%Machine%]" == "[0x0200]" (set "Machine=[%Machine%] - Intel 64"
) else if /i "[%Machine%]" == "[0x0266]" (set "Machine=[%Machine%] - MIPS"
) else if /i "[%Machine%]" == "[0x0284]" (set "Machine=[%Machine%] - ALPHA64"
) else if /i "[%Machine%]" == "[0x0366]" (set "Machine=[%Machine%] - MIPS"
) else if /i "[%Machine%]" == "[0x0466]" (set "Machine=[%Machine%] - MIPS"
) else if /i "[%Machine%]" == "[0x0284]" (set "Machine=[%Machine%] - ALPHA64"
) else if /i "[%Machine%]" == "[0x0520]" (set "Machine=[%Machine%] - Infineon"
) else if /i "[%Machine%]" == "[0x0CEF]" (set "Machine=[%Machine%] - CEF"
) else if /i "[%Machine%]" == "[0x0EBC]" (set "Machine=[%Machine%] - EFI Byte Code"
) else if /i "[%Machine%]" == "[0x8664]" (set "Machine=[%Machine%] - AMD64 (K8)"
) else if /i "[%Machine%]" == "[0x9041]" (set "Machine=[%Machine%] - M32R little-endian"
) else if /i "[%Machine%]" == "[0xAA64]" (set "Machine=[%Machine%] - ARM64"
) else if /i "[%Machine%]" == "[0xC0EE]" (set "Machine=[%Machine%] - CEE"
) else (set "Machine=[-%Machine%-] - UNKNOWN"
)

if /i "[%Subsystem%]" == "[0x0002]" (set "Subsystem=[%Subsystem%] - GUI"
) else if /i "[%Subsystem%]" == "[0x0003]" (set "Subsystem=[%Subsystem%] - CLI"
) else (set "Subsystem=[-%Subsystem%-] - UNKNOWN"
)

endlocal&set "%3=%Machine%"&set "%4=%Subsystem%"&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::------------------------------------脚本内容到此为止,请以ANSI格式保存为PeInfo.cmd------------------------------------




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2