batch-file - 用于提取变量部分的批处理命令

标签 batch-file

这是我到目前为止所拥有的......

@echo off
@cls

SetLocal EnableDelayedExpansion

del /f /q "c:\setup*.txt" >nul 2>NUL

dir c:\setup.exe /b /s >c:\setup1.txt 2>NUL
rem this gives me a text file like this (without the 'equals' lines):
    ==================================
    c:\oracle\product\9.1.20.1\client_1\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client_1\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client_2\oui\bin\setup.exe
    c:\oracle\product\11.2.53\client_1\oui\bin\setup.exe
    c:\Program Files\Adobe\Reader 11.0\Setup Files\{AC76BA86-7AD7-1033-7B44-AB0000000001}\setup.exe
    c:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft .NET Framework 3.5 SP1\setup.exe
    c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\SetupCache\Client\Setup.exe
    c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\SetupCache\Extended\Setup.exe
    c:\WINDOWS\system32\setup.exe
    c:\WINDOWS\system32\dllcache\setup.exe
    ===================================

find /i "\oui\bin\setup.exe" c:\setup1.txt >c:\setup2.txt 2>NUL
rem this takes the previous text file and searches for the string "\oui\bin\setup.exe" and gives me a text file like this (without the 'equals' lines):
    ===================================

    ---------- C:\SETUP1.TXT
    c:\oracle\product\9.1.20.1\client_1\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client_1\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client_2\oui\bin\setup.exe
    c:\oracle\product\11.2.53\client_1\oui\bin\setup.exe
    ===================================

more /E +2 C:\setup2.txt >C:\setup3.txt
rem this takes the previous text file, strips the first two lines (top line is blank), and gives me a text file like this (without the 'equals' lines):
    ===================================
    c:\oracle\product\9.1.20.1\client_1\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client_1\oui\bin\setup.exe
    c:\oracle\product\10.2.0\client_2\oui\bin\setup.exe
    c:\oracle\product\11.2.53\client_1\oui\bin\setup.exe
    ===================================

此时,我需要能够提取\client... 之前的路径名部分,如下所示:

c:\oracle\product\9.1.20.1\client_1\oui\bin\setup.exe     =>     c:\oracle\product\9.1.20.1
c:\oracle\product\10.2.0\client\oui\bin\setup.exe     =>     c:\oracle\product\10.2.0
c:\oracle\product\10.2.0\client_1\oui\bin\setup.exe     =>     c:\oracle\product\10.2.0
c:\oracle\product\10.2.0\client_2\oui\bin\setup.exe     =>     c:\oracle\product\10.2.0
c:\oracle\product\11.2.53\client_1\oui\bin\setup.exe     =>     c:\oracle\product\11.2.53

我知道如何提取路径的一部分,从末尾减去一定数量的字符,但我就是不知道如何如上所述提取路径的该部分。我希望这是有道理的。

谢谢。

最佳答案

删除目前为止拥有的所有内容,并将其替换为以下内容。不需要临时文件:-)

@echo off
for /f "delims=" %%A in (
  'dir c:\setup.exe /b /s ^| findstr /lie "\\oui\\bin\\setup.exe"'
) do for /f "delims=" %%B in ("%%A\..\..\..\..") do echo %%~fB

关于batch-file - 用于提取变量部分的批处理命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21196489/

相关文章:

batch-file - 如何使用批处理文件关闭指定的打开文件夹

java - 将批处理文件中的数据获取到java程序

batch-file - 批处理文件: sorting folders and files alphabetically

batch-file - 查找所有 Access 数据库,并压缩和修复

batch-file - Windows 命令行内存不足

windows - Windows 批处理文件中区分大小写

bash - 使用 COM 端口读取/写入数据的批处理脚本

git - 我的批处理脚本只能第二次运行

windows - 环境变量是否具有关联性?

windows - 如何使用 COPY 命令附加到文件