Variable in a variable
Moderator: DosItHelp
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Variable in a variable
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
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
Re: Variable in a variable
@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.
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Variable in a variable
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
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
Re: Variable in a variable
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
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Variable in a variable
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
Re: Variable in a variable
test this on some dummy files and folders:
EDITED: changed the xcopy portion.
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
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Variable in a variable
Bad news. Doesn't work. 

Re: Variable in a variable
Try it now. I edited it.
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Variable in a variable
Still not working. Says something like Invalid Drive Specification.
Thanks for the effort, though

Re: Variable in a variable
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?
c:\windows
or something like that is what is expected.
Did you alter the destination drive letter?
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Variable in a variable
Yes... I changed it to C:\, since I didn't have a D:\ drive. Was it a mistake? Can you explain? Sorry, though.
Re: Variable in a variable
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.
Copy and paste them to a reply, if you can't figure it out from there.
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Variable in a variable
Works fine, but what about the part where it actually copies the files? Not easy, eh? :p
Re: Variable in a variable
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.
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Variable in a variable
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.