-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?

4
  • Just a note: This is not PowerShell but actually Command Shell. You should edit your title and tags to target your question to the right people! Commented Aug 24, 2017 at 20:35
  • Thanks! Sorry for the inconvenience. Commented Aug 24, 2017 at 20:38
  • It's not bash either, its batch. I'll fix it. Commented Aug 24, 2017 at 20:43
  • Thanks, and again, sorry. Commented Aug 24, 2017 at 20:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! If you wouldn't mind, could you explain the how calling !file! differs from calling %file%?
@alwaysthestudent Please read number one batch issue delayedexpansion the sligthest research effort would have revealed this.
Thanks for the resource, it was enlightening; I'm sorry for wasting your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.