Computing.Net > Forums > Windows NT > Variable Within Variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Variable Within Variable

Reply to Message Icon

Name: astroraptor
Date: March 16, 2005 at 19:46:53 Pacific
OS: Windows 2000 Pro
CPU/Ram: 1.6GHz/512Mb
Comment:

I'm trying to shorten a command in a backup batch file I made by putting a variable within a variable but it's not working. There are 5 users and I figured that I'd do a FOR /L and start at 1 with an increment of 1 and ending at 5. So I figured I type: %user%%n% and it works, but alas, no. Can anyone help me out? This would make things easier to add or remove users rather than scrambling through code.

FOR /L %%n IN (1,1,5) DO XCOPY %profiles%\%user%%n%\Personal\*.doc A:\%user%%n%

This is for NT 4.0 Workstation, just to let you know. %profiles% and %user1% %user2% etc, have been set earlier on in the batch. Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: March 17, 2005 at 01:16:03 Pacific
Reply:

To use "Variable inside Variable" you must enable the "Deferred Expansion" and then use the ! symbol for variables; so start your batch coding

@Echo Off
SetLocal EnableDelayedExpansuion

then code you For /L statement as
For /L %%n in (1,1,5) Do XCOPY %profiles%\!user%%n!\Personal\*.doc A:\!user%%n!

and set as your last command before exiting

EndLocal

Tath should achieve your goal.



0

Response Number 2
Name: astroraptor
Date: March 17, 2005 at 07:43:17 Pacific
Reply:

Thanks! I made a test batch and tried it out. Works like a charm, in Windows 2000 anyway. Must work in NT 4.0 then if you replied in that section, hehe.


0

Response Number 3
Name: astroraptor
Date: March 17, 2005 at 20:35:50 Pacific
Reply:

It doesn't work in Windows NT 4.0. SetLocal EnableDelayedExpansion doesn't seem to be a valid option in NT 4.0.


0

Response Number 4
Name: IVO
Date: March 18, 2005 at 00:00:23 Pacific
Reply:

Sorry, I apologize for my previous post; I suggested a Win 2K/XP method supposing to be valid under NT 4.0 too, as these systems share the same kernel, but SetLocal was added later in the environment development.

Never mind however! Start your batch coding

Echo Off
If not "%1"=="][" (
Cmd /V:On /C %0 ][ %* & GoTo :EOF)
Shift

remove the EndLocal statement and leave the For as it is. That emulates the EnableDelayed Expansion directive.


0

Response Number 5
Name: astroraptor
Date: March 19, 2005 at 14:16:05 Pacific
Reply:

Unfortunately that doesn't work. :( Thanks.


0

Related Posts

See More



Response Number 6
Name: IVO
Date: March 20, 2005 at 07:37:23 Pacific
Reply:

It should work, as Cmd /V:On is the standard method to enable the Delayed Expansion under native NT 4.0.

Please report the error(s) you got so I can refine my tip.


0

Response Number 7
Name: astroraptor
Date: March 20, 2005 at 10:17:01 Pacific
Reply:

There are no errors, it just doesn't work. Maybe I have to use something else rather than "!" for !user%%n!


0

Response Number 8
Name: IVO
Date: March 21, 2005 at 03:32:47 Pacific
Reply:

Well or better... BAD! I went through an accurate search on that issue and discovered the Delayed Expansion was introduced with Windows 2000, so that directive is unknown to Windows NT 4.0: nothing to do to work around.

Too many years since I operated NT 4.0 workstations! I erroneously figured out that usefull option was on my hand at NT time.

Sorry again to waste your time.


0

Response Number 9
Name: astroraptor
Date: March 21, 2005 at 11:35:25 Pacific
Reply:

Well, you're not wasting my time. I appreciate you taking your time. I'll try something that I found on the net but if that doesn't work, then I'll have to do things the long way.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows NT Forum Home


Sponsored links

Ads by Google


Results for: Variable Within Variable

usrname variables in Logon scripts www.computing.net/answers/windows-nt/usrname-variables-in-logon-scripts/790.html

How to automatically set PATH environment variable for NT? www.computing.net/answers/windows-nt/how-to-automatically-set-path-environment-variable-for-nt/5670.html

Passing date as variable in FTP script www.computing.net/answers/windows-nt/passing-date-as-variable-in-ftp-script/9672.html