BoyUnderTheMoon
BoyUnderTheMoon

Reputation: 771

Batch variable name in another variable

Assume I have a folder that contains other folders that I don't necessarily know about:

|Folder
|      |-- SubFolder1
|      |-- SubFolder2
|      |-- SubFolder3

I essentially want to map another location for the sub folders. So I create another batch file containing these sub folder names that can have another location set to them, e.g:

TYPE NUL > folders.cmd
FOR /D %%i IN (*) DO (@ECHO SET %%i=>>folders.cmd)

which produces:

SET SubFolder1=
SET SubFolder2=
SET SubFolder3=

Then if I was to open that file and set some values like so:

SET SubFolder1=C:\test1
SET SubFolder2=C:\test2
SET SubFolder3=C:\test3

How would I now access the variables/values in my batch file (especially when I may not know what they are).

I thought maybe I could do something like:

CALL folders.cmd
FOR /D %%i IN (*) DO (
    @ECHO %%%i%%%
)

But this appears to be the incorrect way to do it.

Upvotes: 1

Views: 2599

Answers (3)

abelenky
abelenky

Reputation: 64672

If your file folders.cmd contains:

SET SubFolder1=C:\test1
SET SubFolder2=C:\test2
SET SubFolder3=C:\test3

I would use this code:

for /F "tokens=3 delims== " %%a in (folders.cmd) do echo %%a

Which would generate output:

C:\test1
C:\test2
C:\test3

A more complete command would be:

for /F "tokens=2,3 delims== " %%a in (folders.cmd) do echo %%a maps to %%b

With output:

SubFolder1 maps to C:\test1
SubFolder2 maps to C:\test2
SubFolder3 maps to C:\test3

Upvotes: 0

Aacini
Aacini

Reputation: 67206

The concept you use in this question is called array. You may use the methods described at this post in order to access array elements. For example:

SETLOCAL ENABLEDELAYEDEXPANSION

CALL folders.cmd
FOR /D %%i IN (*) DO (
    ECHO !%%i!
)

... or:

CALL folders.cmd
FOR /D %%i IN (*) DO (
    CALL ECHO %%%%i%%
)

EDIT: Output of CALL example added

C:\> dir /B
folders.cmd
SubFolder1
SubFolder2
SubFolder3
test.bat

C:\> type folders.cmd
SET SubFolder1=C:\test1
SET SubFolder2=C:\test2
SET SubFolder3=C:\test3

C:\> type test.bat
@echo off
setlocal

CALL folders.cmd
FOR /D %%i IN (*) DO (
    CALL ECHO %%%%i%%
)

C:\> test.bat
C:\test1
C:\test2
C:\test3

Upvotes: 1

Mofi
Mofi

Reputation: 49085

Don't mix loop variables with environment variables as they are two different types of variables.

It is totally unclear what you finally want to achieve. But for your example the following batch code produces folders.cmd with what you finally want.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
DEL folders.cmd 2>nul
FOR /D %%i IN (*) DO (
    SET "FolderName=%%i"
    SET "FolderNumber=!FolderName:~-1!
    ECHO SET "%%i=C:\test!FolderNumber!">>folders.cmd
)
TYPE folders.cmd
ENDLOCAL
PAUSE

The output is:

SET "SubFolder1=C:\test1"
SET "SubFolder2=C:\test2"
SET "SubFolder3=C:\test3"

The second SET gets last character from current folder name.

Or perhaps better for your very unclear task:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
DEL folders.cmd 2>nul
SET "FolderCount=0"
FOR /D %%i IN (*) DO (
    SET /A FolderCount+=1
    ECHO SET "%%i=C:\test!FolderCount!">>folders.cmd
)
TYPE folders.cmd
ENDLOCAL
PAUSE

This code writesC:\test with an incrementing number into other batch file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • pause /?
  • set /?
  • setlocal /?
  • type /?

Upvotes: 0

Related Questions