Avatar of pudur2007
pudur2007
 asked on

String manipulation using MS-DOS commands

when i do the following command (in a BTAT file)
for /d %%i in (PWD) do set IA_PATH_SOURCE_DIR=%%~fi
i get the full path of the current directory.
for example let us say the fullpath is C:\workspace\projctes\fitNesseRoot\FrontPage\TestSuite\TestCase1

We need to check whether .FrontPage. appears in the path. If it does NOT appear we need to echo an error message and exit. ( note the dot on both sides of FrontPage)

If it apprears then we need to  set two variables:
set testpath=FrontPage\TestSuite
set testcase=TestCase1

what is the  best approach?
Microsoft DOS

Avatar of undefined
Last Comment
Vee_Mod

8/22/2022 - Mon
devil_himself



Will The Path Always be This long

C:\workspace\projctes\fitNesseRoot\FrontPage\TestSuite\TestCase1

or it can vary like this

C:\workspace\projctes\fitNesseRoot\FrontPage\TestSuite\x1

C:\workspace\projctes\fitNesseRoot\FrontPage\TestSuite\TestCase1\x2

C:\workspace\projctes\fitNesseRoot\FrontPage\TestSuite\whateve\ gafa\TestCase1

we can extract the each folder in the path like this


:bof
 
    @echo off & setlocal enableextensions enabledelayedexpansion
    set fpath=C:\workspace\projctes\fitNesseRoot\FrontPage\TestSuite\TestCase1
    set count=0
    set rest=%fpath%
 
:init
   
    :loop
    for /f "tokens=1* delims=\" %%a in ("%rest%") do (
    set /a count+=1
    set fpath[!count!]=%%a
    set rest=%%b
    if defined rest goto loop
    )
      
    for /l %%i in (1,1,%count%) do echo fpath[%%i]=!fpath[%%i]!
    
 
:eof

Open in new window

pudur2007

ASKER
path can vary.. i just gave an example that is all..if is going to be fixed lenggth then the problem is much simplified!! i do not expert advise for that :-)
devil_himself

Here Ya Go
:bof
 
    @echo off & setlocal enableextensions enabledelayedexpansion
    set fpath=C:\workspace\projctes\fitNesseRoot\FrontPage\TestSuite\TestCase1
    set count=0
    set rest=%fpath%
 
:init
 
    echo %fpath% | find /i "FrontPage"
    if errorlevel 1 (
       echo # ERROR & goto :eof
  ) else (
       goto :loop
  )
   
    :loop
    for /f "tokens=1* delims=\" %%a in ("%rest%") do (
    set /a count+=1
    set fpath[!count!]=%%a
    set rest=%%b
    if defined rest goto loop
    )
      
    for /l %%i in (1,1,%count%) do set fpath[%%i]=!fpath[%%i]!
 
    set testpath=%fpath[5]%\%fpath[6]%
    set testcase=%fpath[7]%
 
    echo %testpath%
    echo %testcase%
       
:eof

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
devil_himself

Change

echo %fpath% | find /i "FrontPage"

to

echo %fpath% | find /i ".FrontPage."
pudur2007

ASKER
Hi Devil,
it does not seem to work.. By the way echo %fpath% | find /i "FrontPage" is correct.  There are not dots in the path...

You need to be carful with path names like this
C:\Documents and Settings\user

please use the following for your troubleshooting...

 :bof
    @echo off & setlocal enableextensions enabledelayedexpansion
    for /F "usebackq" %%i in (`pwd`) do set fpath=%%~fi
    echo Full path is %fpath%
    set count=0
    set rest=%fpath%
:init
 
    echo %fpath% | find /i "FrontPage"
    if errorlevel 1 (
       echo # ERROR & goto :eof
  ) else (
       goto :loop
  )
   
    :loop
    for /f "tokens=1* delims=\" %%a in ("%rest%") do (
    set /a count+=1
    set fpath[!count!]=%%a
    set rest=%%b
    if defined rest goto loop
    )
     
    for /l %%i in (1,1,%count%) do set fpath[%%i]=!fpath[%%i]!
 
    set testpath=%fpath[5]%\%fpath[6]%
    set testcase=%fpath[7]%
 
    echo test path is %testpath%
    echo test case is %testcase%
       
:eof
pudur2007

ASKER
Let me explain the requirements clearly.
Suppose the fpath is:
C:\Documents Settings\projects\client\acceptance\fitnesse-fixtures\FitNesseRoot\FrontPage\WebServicesTests\TestCase1
testpath should be = FrontPage\WebServicesTests
testcase should be = TestCase1

Suppose the fpath is:
C:\Documents Settings\projects\client\acceptance\fitnesse-fixtures\FitNesseRoot\FrontPage\WebServicesTests\SecurityTests\TestCase2
testpath should be = FrontPage\WebServicesTests\SecurityTests
testcase should be = TestCase2
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
devil_himself

This is Working
:bof
 
    @echo off & setlocal enableextensions enabledelayedexpansion
    ::%~dp0 is the Windows equivalent of `pwd` in bash.
    for /f "usebackq" %%i in (`cd`) do set fpath=%~dp0
    echo Full path is %fpath%
        
:init
    
    ::check string contains frontpage 
    echo %fpath% | find /i "FrontPage"
    if errorlevel 1 (
       echo # ERROR & goto :eof
  ) else (
       goto :loop1
  )
   
  ::break into last and the rest part
  :loop1     
  for %%a in ("%fpath%") do set testpath=%%~pa & set testcase=%%~na
  :loop2
  for /f "tokens=1,* delims=\" %%a in ("%testpath%") do (
    set first_=%%a
    set testpath=%%b
    
    )
 
  echo %testpath% | findstr /b /i "FrontPage" >nul
    if not errorlevel 1 goto next 
    if defined testpath goto loop2
 
       
:next
   echo test path is %testpath%
   echo test case is %testcase% 
 
:eof

Open in new window

pudur2007

ASKER
As per this example,
C:\Documents Settings\projects\client\acceptance\fitnesse-fixtures\FitNesseRoot\FrontPage\WebServicesTests\TestCase1

testpath is including TestCase1...it should not not
testcase should be TestCaset, but it is blank
ASKER CERTIFIED SOLUTION
devil_himself

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
pudur2007

ASKER
Can you double the script posted here is the same as the one you have it on your machine??

For testpath i get
FrontPage\WebServicesTests\TestCase1

for testcase i get
C:\Documents Settings\projects\client\acceptance\fitnesse-fixtures\
FitNesseRoot\FrontPage\WebServicesTests\TestCase1






Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
devil_himself

I Copied The above Script and Used It

Try changing this line

for /f "usebackq" %%i in (`cd`) do set fpath=%~d

to

set fpath=C:\Documents Settings\projects\client\acceptance\fitnesse-fixtures\FitNesseRoot\FrontPage\WebServicesTests\TestCase1

and test the script !
pudur2007

ASKER
when i did that i got the testpath value correct, but testcase is same as the fpath i set.
pudur2007

ASKER
when i made this change, things are wokring now!!
Thanks for your help!!
for /F "usebackq" %%i in (`cd`) do set fpath=%%~fi
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pudur2007

ASKER
can be closed
pudur2007

ASKER
there is an extra space after FrontPage\WebServicesTests\ for testpath. How can i get rid off it?
pudur2007

ASKER
ok
Your help has saved me hundreds of hours of internet surfing.
fblack61
devil_himself

May I ask why you're trying to delete this question ? didn't i answered it ?
pudur2007

ASKER
good job
Vee_Mod

Force accepted.
Vee_Mod
Experts Exchange Moderator
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.