Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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 EnableDelayedExpansuionthen 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.

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.

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

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)
Shiftremove the EndLocal statement and leave the For as it is. That emulates the EnableDelayed Expansion directive.

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.

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

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.

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.

![]() |
![]() |
![]() |

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