1

I've been trying to use a loop to create multiple aliases without success. I'm not a professional, please bear with me... Does anyone know what's wrong in my code or how I can implement this in the command prompt? I know how to do this in PowerShell but I'm trying to reproduce the code for cmd. Here's the code:

@ECHO OFF

FOR %%G IN (a,b,c,d,e,f,g,h) DO (
    DOSKEY %%GG=cd %%GG:\
)

Here's what I get as a result when running the macro:

2
  • So what do you see or experience with the batch file? Oct 29, 2018 at 1:05
  • I've just added a screenshot.
    – prubini87
    Oct 29, 2018 at 1:12

1 Answer 1

4

You're defining DOSKEY %%GG=cd %%GG:\ (with double Gs) which expands to DOSKEY aG=cd aG:\, DOSKEY bG=cd bG:\... so you must type aG in order to change into the drive aG: (which is not a valid drive letter). Same to other drives

If you run for /? you'll see that variables in for loop have only one letter, just like how command line parameters are composed of a single digit

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.

Moreover to change the current drive you use only the drive letter instead of cd. That means to change to the current directory on A: just run A:. CD A: won't work (as expected). However if you want to force changing both the current drive and current directory then use the /D option

Here's the fixed version

@ECHO OFF

FOR %%G IN (a,b,c,d,e,f,g,h) DO (
    DOSKEY %%G=%%G:
    rem Or DOSKEY %%G=cd /D %%G:
)
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .