简体   繁体   中英

use DIR command output in batch

I use command :

dir D:\ /b /A:D-H

and output is for example:

  • Photos
  • Wallpapers
  • My Personal Data

and.. I want to set varialeble on this folders as:


set SOMEFOLDER=folder1


set SOMEFOLDER2=folder2

and ect..

How can I do this?

You can use the following for a batch file in the same directory as the folders:

@echo off
setlocal enabledelayedexpansion
set num=0
for /d %%i in (*) do set /a num+=1&set SOMEFOLDER!num!=%%i
echo SOMEFOLDER1 = %SOMEFOLDER1%
echo SOMEFOLDER2 = %SOMEFOLDER2%
pause

If you need the batch file in a different directory or want to change some options, use this instead:

@echo off
setlocal enabledelayedexpansion
set num=0
for /f "tokens=*" %%i in ('dir D:\ /b /a:D-H') do set /a num+=1&set SOMEFOLDER!num!=%%i
echo SOMEFOLDER1 = %SOMEFOLDER1%
echo SOMEFOLDER2 = %SOMEFOLDER2%
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM