0

I am having a struggle with loop using "for" and to set the variable for later action.

So briefly explaining about a situation I am facing, I have files named "HI_001_1813414.nii.gz" & "HI_001_1813414_T1.nii.gz" , and I want to set each two files' name as a variable, so that I can use it for later action.

Here is my batch script.

path=C:\Users\user\Desktop\dsi_studio_64
for /f "delims=" %%x in ('dir *_T1.nii.gz /b /d') do (

set fname=%%x 

set name=%fname: ~-7%

set dwi_fname = %fname:_T1 =% 

call dsi_studio.exe --action=reg --from=%%x --to=%dwi_fname% --output=%%x_norm.nii.gz --reg_type=0 > "%%x.log.txt"
 )

The main problem here is: the variable I set shows only the last one (I checked it with echo).

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Jean Kim
  • 3
  • 1

1 Answers1

0

You cannot set values in for loops unless you run this command first:
SETLOCAL enabledelayedexpansion
Also you will need to refer to %var% as !var! while inside the loop.

  • This is incorrect. Call set and %%exspansion%% also achieves variable Modification. – T3RR0R Apr 10 '20 at 05:46
  • Well, you don't need delayed expansion or `call` to set a variable, you just need one of them to *expand* (read) it... – aschipfl Apr 10 '20 at 11:32
  • Thank you! SETLOCAL enabledelayedexpansion helps me to get into the loop and set the variables well :) – Jean Kim Apr 13 '20 at 07:47