Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Depending on the text after 'X' and before 'Open file' in mypath variable, I want to call a function passing this text as argument in order to replace it by 'foo'. To do it, I have written

@echo off
SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file
if /i %1=="Dashboard" goto label %1
if /i %1=="Dboard" goto label %1

:label 
SET mypath=%mypath:\%1\=foo%
ECHO %mypath%

I notice that echoing mypath at the end of that script outputs

Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file  instead of 
Y:\SUT\ISTQB\Board Airport\X\foo\Open file

I think of the issue is about parameter %1 within 'SET mypath=%mypath:\%1\=foo%', but I don't understand why.

In fact I absolutely need to use parameter %1 because the text which is currently "Dashboard" into mypath variable is not a static text. It can be "Dboard", or anything

Someone could explain me? Thank you in advance

share|improve this question

4 Answers 4

up vote 2 down vote accepted

For string manipulations with variables you need delayed expansion:

@echo off
SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file
if /i %1=="Dashboard" goto label %1
if /i %1=="Dboard" goto label %1

:label 
setlocal enabledelayedexpansion
SET mypath=!mypath:\%~1\=foo!
ECHO %mypath%

..output is:

Y:\SUT\ISTQB\Board Airport\XfooOpen file
share|improve this answer
    
DOING myfile.bat "Dboard" my output is Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file, not the me as you; –  new May 16 '13 at 20:06
    
and its normal to be.YOu want to replace DASHBOARD with FOO but if you pass DBOARD nothing will be replaced. –  npocmaka May 16 '13 at 20:10
    
Yes, if you want replace "Dboard", you must search for "Dboard" and if search failed, nothing can replaced. –  Endoro May 16 '13 at 20:14
    
I'm foolish! It's very good, it's due to a lack of vigilance. Thank you very much –  new May 16 '13 at 20:55
1  
Btw. None of the answers works with a path like C:\Documents & Settings\Dboard\file. Therefore it's always a good idea to enclose SET commands SET "mypath=!mypath:\%~1\=foo!" –  jeb May 17 '13 at 6:53

Excuse me. Sometimes I think I don't really understand the questions (perhaps because my mother tongue is not English).

In your question you said you want to replace "the text after 'X' and before 'Open file' in mypath variable... by 'foo'" and even you said "text which is currently "Dashboard" into mypath variable is not a static text. It can be "Dboard", or anything", but in your code and posterior comments in the answers it seems that you just want to change the strings "Dashboard" or "Dboard" by "foo". This confuses me.

Anyway, this is my solution to your question (not to your comments).

@echo off
setlocal EnableDelayedExpansion

SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file

rem Get the text after 'X' and before 'Open file'
set auxPath=%mypath%
set enclosed=
set text=
:nextPart
   for /F "tokens=1* delims=\" %%a in ("%auxPath%") do (
      if "%%a" equ "Open file" (set enclosed=) else (
      if defined enclosed (set "text=!text!\%%a") else (
      if "%%a" equ "X" (set enclosed=1)
      ))
      set "auxPath=%%b"
   )
if defined auxPath goto nextPart
set text=%text:~1%

rem Replace that text by "foo"
call :Change "%text%"
goto :EOF

:Change
SET mypath=!mypath:\%~1\=\foo\!
ECHO %mypath%
exit /B

For example, the output of previous program is this:

Y:\SUT\ISTQB\Board Airport\X\foo\Open file

But if mypath variable would be this:

Y:\SUT\ISTQB\Board Airport\X\One\Two three\Open file

... the output would be this:

Y:\SUT\ISTQB\Board Airport\X\foo\Open file

... that precisely solves your request.

share|improve this answer
    
+1, best answer except one point: the OP wrote text after "X" not after "X\" :) –  Endoro May 17 '13 at 4:42
1  
@Endoro: Yes. In the context of the question, it seems that both X and Open file strings should be delimited by left-slash characteres (and also the replaced foo string). This point is confirmed by OP's expected output: Y:\SUT\ISTQB\Board Airport\X\foo\Open file that, by the while, is not addressed by anyone of the other answers... –  Aacini May 17 '13 at 5:34
        @echo off
        SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file

        if /i "%~1"=="Dashboard" goto :label 
        if /i "%~1"=="Dboard" goto :label 

        goto :eof
        :label
        rem set temp_var=%~1
        rem call SET mypath=%%mypath:%temp_var%=foo%%
        rem echo %mypath%
   setlocal enabledelayedexpansion 
        set mypath=!mypath:%~1=foo!
        ECHO !mypath!
    endlocal

Could you try this?

share|improve this answer
1  
Intresting issue: we have 40' time and post to the same minute :-) –  Endoro May 16 '13 at 19:42
    
yeah.You are my fiercest competitor here... –  npocmaka May 16 '13 at 19:44
    
I try it and it doesn't change issue! –  new May 16 '13 at 19:53
    
Ok.I've made more changes in your code.This will work only if the parameter is Dashboard as there is no Dboard in initial value of mypath. You can try also the commented part –  npocmaka May 16 '13 at 20:09
    
Issue persists. value of mypath doesn't change! –  new May 16 '13 at 20:20

Your problem was the search/replace using a %1 :-
SET mypath=%mypath:\%1\=foo% has too many % characters upsetting the syntax a bit.

This answer was to use EnableDelayedExpansion to take advantage of the ! syntax, but beware the %mypath% variable will lose its value after endlocal - it only keeps its value within the setlocal block.

One way around this is to write the SET command to a temporary batch file, and call it after the local block :

setlocal EnableDelayedExpansion
  SET mypath=!mypath:%1=foo!
     rem  Create a batch file to perform SET later : 
  echo @SET mypath=%mypath% > "%TEMP%\setmyvar.bat"
endlocal

   rem  Now use the batch file to set the variable :
call "%TEMP%\setmyvar.bat"
ECHO %mypath%

If there's a more elegant way to do this, please let us know :)

share|improve this answer
    
has too many % characters upsetting the syntax a bit. ? OMG! You must see some of my really % upsetting codes! –  Endoro May 16 '13 at 21:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.