user3208239
user3208239

Reputation: 761

Batch command to extract portion of variable

Here's what I have so far...

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

At this point, I need to be able to extract the portion of the path names prior to the \client... as follows:

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

I know how to pull a portion of the path, minus a certain number of characters from the end, but I just can't figure out how to extract that portion of the path as described above. I hope this makes sense.

Thanx.

Upvotes: 2

Views: 94

Answers (2)

dbenham
dbenham

Reputation: 130839

Scratch everything you have so far, and replace it with the following. No temp file required :-)

@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

Upvotes: 2

Magoo
Magoo

Reputation: 80033

< lang-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

I've changed the filenames to suit my system. The result in newfile.txt is

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

Upvotes: 0

Related Questions