Variable in a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Variable in a variable

#1 Post by john924xps » 11 Sep 2012 21:51

Set test num=0
Set num=1
Set bkupdir%num%=C:\Windows
REM bkupdir is a path

Set testnum=%testnum% + 1
REM supposed to keep looping until detects a valid variable
REM now testnum is 1
If defined bkupdir%testnum% (
Echo %bkupdir%testnum%%
REM supposed to say the value of variable bkupdir1
REM but unfortunately two percentage symbols make it visible In dos and unusable as a variable



Do you get a brief hint of wut I'm trying to ask?
I typd this on an iPad so its kinda... Messy.
THANKS

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable in a variable

#2 Post by foxidrive » 11 Sep 2012 22:16

@echo off
setlocal EnableDelayedExpansion
set var1=c:\windows
set num=1
echo !var%num%!
pause


Tell us what you want to do, not how you think it should be done. You'll usually get better answers.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Variable in a variable

#3 Post by john924xps » 11 Sep 2012 22:55

Sorry, and thanks for the tip. I want the console to keep on asking for new variable values. Bkupdir1, bkupdir2, etc... I cant just predfine the variables since i dont know how many variables theyre planning to input. Then I want the program to stop requesting when the user types in QUIT. Bt later on, I want the program to echo out all of the bkupdir variables with values. Like maybe bkupdir9 won't contain any content, so the program would stop echoing at variable number 8. But the way to check when to stop is to set up a testnum variable. Where the program keeps adding 1 to testnum, until the "if" statement can no longer detect a valid variable. This is the code snippet I plan to use,

Set /a testnum=%testnum%+1
If define %bkupdir%testnum%%

What I thought it would do was first evaluate testnum (1) which would mean

If define %bkupdir1%

Since the testnum has already been evaluated and the computer could evaluate the full value of bkupdir.

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable in a variable

#4 Post by foxidrive » 11 Sep 2012 23:51

Try this:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set num=0
for /L %%a in (1,1,1000) do set "bkupdir%%a="

:loop
set /a num=num+1
set /p "bkupdir%num%=Type bkupdir%num% (enter to quit) :"
if defined bkupdir%num% goto :loop
set bkupdir
pause

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Variable in a variable

#5 Post by john924xps » 12 Sep 2012 00:57

Thanks. It worked fine. But I've got another question. If I want to xcopy all the bkupdirs (yes, all of the bkupdir variables contain directory paths). How could I make the xcopy function keep copying the content in the directories in the bkupdir variables into a folder named backup? Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable in a variable

#6 Post by foxidrive » 12 Sep 2012 01:29

test this on some dummy files and folders:

EDITED: changed the xcopy portion.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set num=0
for /L %%a in (1,1,1000) do set "bkupdir%%a="

:loop
set /a num=num+1
set /p "bkupdir%num%=Type bkupdir%num% (enter to quit) - "
if defined bkupdir%num% goto :loop
set bkupdir
pause

set num=0
:loop2
set /a num=num+1
if defined bkupdir%num% (
for %%a in ("!bkupdir%num%!") do (
echo xcopy "%%~a\*.*" "d:\backup\%%~pnxa\" /s/h/e/k/f/c
)
)
if defined bkupdir%num% goto :loop2
pause

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Variable in a variable

#7 Post by john924xps » 12 Sep 2012 02:43

Bad news. Doesn't work. :(

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable in a variable

#8 Post by foxidrive » 12 Sep 2012 03:30

Try it now. I edited it.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Variable in a variable

#9 Post by john924xps » 12 Sep 2012 04:08

Still not working. Says something like Invalid Drive Specification. :( Thanks for the effort, though

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable in a variable

#10 Post by foxidrive » 12 Sep 2012 09:16

What did you enter in the first input line?

c:\windows

or something like that is what is expected.
Did you alter the destination drive letter?

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Variable in a variable

#11 Post by john924xps » 12 Sep 2012 21:36

Yes... I changed it to C:\, since I didn't have a D:\ drive. Was it a mistake? Can you explain? Sorry, though.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable in a variable

#12 Post by foxidrive » 13 Sep 2012 04:14

Try the code above now. It will only ECHO the xcopy commands to the screen.

Copy and paste them to a reply, if you can't figure it out from there.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Variable in a variable

#13 Post by john924xps » 13 Sep 2012 08:11

Works fine, but what about the part where it actually copies the files? Not easy, eh? :p

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable in a variable

#14 Post by foxidrive » 13 Sep 2012 08:37

john924xps wrote:Works fine, but what about the part where it actually copies the files? Not easy, eh? :p


I asked you to copy and paste the commands if you couldn't figure it out.

I'm not psychic and cannot see your screen.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Variable in a variable

#15 Post by john924xps » 14 Sep 2012 07:58

No no no, I copied it and tested it. For bkupdir1, I typed in C:\Windows. Then pressed enter, it showed my variables' values, enter again, but this time it said something like Invalid Drive Specification. SOOO sorry for bothering you.

Post Reply