-2

I'm trying to automate an adb test procedure using a batch file. After each run a file, .CloudData.txt is made. I want to preface that file name with a trial number, T1.CloudData.txt, etc. I made this test code:

echo off
set /p loops="Enter a number of iterations: "
for /l %%i in (1,1,%loops%) do (
    set file=//scard//mytest//T%%i.CloudData.txt
    echo %file%
)

So that eventually I can have something along the lines of:

echo off
set /p loops="Enter a number of iterations: "
for /l %%i in (1,1,%loops%) do (
    rem Execute adb commands for test
    set file=//scard//mytest//T%%i.CloudData.txt
    adb shell mv /sdcard/mytest/.CloudData.txt file
)

But the test returns, assuming loops = 3, /sdcard/mytest/T3.CloudData.txt 3 times. To be painfully specific, the number stored in loops is being added where %%i should be. From what I've read in the following post: bash script variable inside variable, I think I should be using an array, but that's impractical if one day I have to run the test for more than iterations than the array permits. How should I proceed?

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85

1 Answers1

0

You need delayed expansion:

echo off
setlocal enabledelayedexpansion
set /p loops="Enter a number of iterations: "
for /l %%i in (1,1,%loops%) do (
    rem Execute adb commands for test
    set file=//scard//mytest//T%%i.CloudData.txt
    adb shell mv /sdcard/mytest/.CloudData.txt !file!
)

The reason for %%i seems to be 3 at all three times (it isn't) is the variable %file%, which was left from a previous run of your script

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you very much! If you wouldn't mind, could you explain the how calling `!file!` differs from calling `%file%`? – alwaysthestudent Aug 24 '17 at 21:04
  • @alwaysthestudent Please read number one batch issue [delayedexpansion](https://ss64.com/nt/delayedexpansion.html) the sligthest research effort would have revealed this. –  Aug 24 '17 at 23:18
  • Thanks for the resource, it was enlightening; I'm sorry for wasting your time. – alwaysthestudent Aug 25 '17 at 00:12