0

In U:\mtproject\obj\ folder and sub-folders having *.def files. I need to get those file paths and print in the command prompt. but before print I need to replace the U:\mtproject\obj\ part by E:\backupDir\DefBackup\.

Here is what I tried and

set objdir=U:\mtproject\obj\
set backupdir=E:\backupDir\DefBackup\

for /r %objdir% %%f in (*.def) do (     
        set new_def_path =  %backupdir%!%%f:%objdir%=!
        echo %new_def_path%     
)

It just shows this output. I feel like some issue in the syntax of the string replacement. Any idea what is wrong?

ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • Ah, the classic delayed expansion trap. Add `setlocal enabledelayedexpansion` to the start of your script and change `%new_def_path%` to `!new_def_path!` – SomethingDark Jun 13 '15 at 05:50
  • what is the different between %x% and !x! ? – Nayana Adassuriya Jun 13 '15 at 06:09
  • 1
    Batch variables get replaced at runtime, so instances of %whatever% get replaced with their actual values, while !whatever! variables maintain their quality of being actual variables and are read by batch as literally %whatever% instead of their values. (Note that this is a bit of an oversimplification.) – SomethingDark Jun 13 '15 at 06:15

1 Answers1

0

a) use delayed expanison (as SomethingDark already mentioned)

b) remove the spaces whith your set commands or the spaces will be part of the variablename or value.

setlocal enabledelayedexpansion
set objdir=U:\mtproject\obj\
set backupdir=E:\backupDir\DefBackup\

for /r %objdir% %%f in (*.def) do (     
        set new_def_path=%backupdir%!%%f:%objdir%=!
        echo !new_def_path!     
)

short demonstration of the delayed expansion trap

(interesting: you already use delayed expansion. Just do it consequent)

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91