2014-01-17 81 views
2

部分這裏是我迄今爲止...批處理命令提取的可變

@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 
    =================================== 

在這一點上,我需要能夠先於\客戶端解壓路徑名稱的一部分.. 。如下:

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 

我知道如何拉路徑的一部分,減去一定數目的字符從結束,但我只是無法弄清楚如何提取路徑的那部分描述以上。我希望這是有道理的。

Thanx。

+0

有點生疏與批處理,但你可以在循環中嘗試這樣的事情:1.刪除路徑名的最後一個字符; 2.如果最後七個字符==「\ client」,則只返回最後七個字符; 3.如果#2錯誤,返回#1 –

回答

0

<朗-DOS - >

@ECHO OFF 
SETLOCAL 
(
FOR /f "tokens=1*delims=" %%a IN (q21196489.txt) DO (
    SET "line=%%a" 
    CALL :lopend 
) 
)>newfile.txt 

GOTO :EOF 

:lopend 
SET "tail=%line:*\client=\client%" 
CALL SET "line=%%line:%tail%=%%" 
ECHO %line% 
GOTO :eof 

我已經改變了文件名,以適應我的系統。在newfile.txt中的結果是

c:\oracle\product\9.1.20.1 
c:\oracle\product\10.2.0 
c:\oracle\product\10.2.0 
c:\oracle\product\10.2.0 
c:\oracle\product\11.2.53 
2

從現在開始到現在爲止,將其替換爲以下內容。沒有臨時文件需要:-)

@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 
+0

編輯修復bug命令 - 在DIR命令之前添加缺少的單引號。 – dbenham

+0

邪惡! +1我會偷這種技術。 :d – foxidrive