2

I need to store an entire directory listing into a variable, then pass said variable as a parameter to another script. Either directly or by first storing the output of dir to a text file, then doing something like this :

dir \path\todir > temp.txt
set /p VAR=<temp.txt

But the above only reads one line. Can someone help?

onlyf
  • 767
  • 3
  • 19
  • 39
  • Variables only have one line. –  Oct 10 '16 at 11:49
  • 2
    `set /P` reads the first line of any redirected or piped input; to read all lines you need a [`for /F`](http://ss64.com/nt/for_F.html) loop and to concatenate each read line individually, by generating the line-break with [some syntax hack](http://ss64.com/nt/findstr-linebreaks.html); note that the overall length of an environment variable is limited to 8190 bytes, including the variable name (from post-XP versions of Windows onward). However, ask yourself whether it is really necessary to store all lines to a variable, or if there are simpler and more convenient possibilities... – aschipfl Oct 10 '16 at 12:02
  • 1
    Like @achipfl said : What will the other script do with the ouput od `DIR` ? – SachaDee Oct 10 '16 at 12:29
  • 1
    You should [Edit](http://stackoverflow.com/posts/39957309/edit) your question and add the source code of the other script and explain more your end goal ! – Hackoo Oct 10 '16 at 12:42

1 Answers1

0

Since you didn't provide us any other information about your end goal ;

I post something that perhapes give you any ideas to store your variables easily with an array like this code :

@echo off
set "folder=%userprofile%\Desktop"
setLocal EnableDelayedExpansion
REM Populate the array with existent sub-folders in this folder
for /f "tokens=* delims= " %%a in ('Dir /b /a:d "%folder%"') do (
    set /a N+=1
    set "Folder[!N!]=%%a"
)
Rem Populate the array with existent files in this folder
for /f "tokens=* delims= " %%a in ('Dir /b /a:-d "%folder%"') do (
    set /a M+=1
    set "File[!M!]=%%a"
)
::*****************************************************************
:Display_Folders
cls & color 0E
echo Just showing only Folders :
echo(
For /L %%i in (1,1,%N%) do (
    echo Folder[%%i] = !Folder[%%i]!
)
echo(
echo Hit any key to show only files :
pause>nul
::*****************************************************************
:Display_Files
cls & color 0B
echo Just showing only Files :
echo(
For /L %%j in (1,1,%M%) do (
    echo File[%%j] = !File[%%j]!
)
echo(
echo Hit any key to exit :
Pause>nul & exit
Hackoo
  • 18,337
  • 3
  • 40
  • 70