13

I have to store the ouput of DIR in a variable.

This was asked before e.g. here or here or here.

They all provide an answer more or less similar to what I'm using right now:

%= Find the *.sln file and write its path to the file temp.temp =%
DIR /s/b *.sln > temp.temp

%= Read out the content of temp.temp again to a variable =%
SET /p texte=< temp.temp

%= Remove the temp.temp file =%
DEL temp.temp

%= Get only the filename without path =%
FOR %%A IN ("%texte%") DO (
    SET Name=%%~nxA
)

But in my case I'm sure that the ouput of DIR /s/b *.sln will allways be a one-liner. To me it looks a bit ugly to have to
a) store the ouput in an external file and
b) run a FOR loop over it though I already know it will only have one line.

Is there any direct/simpler way to do this?

derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

17
for /f "delims=" %%a in ('dir /s /b *.sln') do set "name=%%a"

indeed is the most efficient method (you can process the output of a command directly, no need for a temporary file).
%name% will contain the full qualified file name of your file (or the last of the files, if there are more than one)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • `delims` is in the end the variable which I called `texte` right? And I guess I still can use `name=%%~nxA` to only get the filename? – derHugo Nov 23 '17 at 08:24
  • 1
    `delims=` sets delims to "no delimiter". By default, `for /f` is processed with `tokens=1` and delimiters of space, comma, tab. If you leave it to standards, it will give you the very first word of the output only: Try (directly on command line) `for /f %a in ("hello world") do @echo %a` vs `for /f "delims=" %a in ("hello world") do @echo %a` – Stephan Nov 23 '17 at 08:28
  • And yes: `? name=%%~nxA` of course is possible (Note: `for` variables are case sensitive: `%%a` is not `%%A` – Stephan Nov 23 '17 at 08:29
  • Thanks a lot! This makes my script way more "uncomplicated". And its finally an answer to this question ;) – derHugo Nov 23 '17 at 08:35
  • I'm using this in my own scripts, but I've found that when the filename includes an exclamation character, the variable %%a will strip those characters out, thus rendering the filename wrong. Any workaround? Oddly it works fine in the CLI. – Ramon Royo Oct 19 '20 at 18:54
  • 1
    @RamonRoyo It's neither `%%a` nor the `for`, which strips exclamation marks. It's delayed expansion. Switch it off before (`setlocal disabledelayedexpansion`) – Stephan Oct 19 '20 at 19:33
  • Stephan, been looking for a solution to that for a while. Thanks for taking the time to answer. – Ramon Royo Oct 19 '20 at 19:50
  • @Stephan, I also using this in my script, but when the folder name have space, it not able to find the file inside the folder. Example for the folder name E:\parent\new folder\new.txt. Any solution that might help? – afifi Jan 07 '21 at 09:39
  • @afifi: My script above processes spaces correctly. Can you show me the complete line that makes trouble? – Stephan Jan 07 '21 at 10:08
  • @Stephan my code as below `set /p yy=Please enter cycle Year (YYYY) : set /p mm=Please enter cycle Month (MM) : set /p dd=Please enter cycle Date (DD) : set /p bn=Please enter Bill No (xxxx) : rem change to the correct directory cd /d E:\ echo. Echo Searching for File Cycle%yy%%mm%%dd%_%bn% ECHO. for /f "delims=" %%a in ('dir /b /s *%yy%%mm%%dd%_%bn%_P_*') do set "name=%%a" IF EXIST %name% GOTO SETFILE TIMEOUT /T 2 >nul GOTO EOP :SETFILE For %%A in ("%NAME%") do ( Set Path=%%~dpA Set Filename=%%~nxA ) echo Found: echo Path : %Path% echo Filename : %Filename%` – afifi Jan 08 '21 at 03:02
  • 1
    @afifi: things to quote: `in ('dir /b /s "*%yy%%mm%%dd%_%bn%_P_*"') do` and `IF EXIST "%name%" GOTO` – Stephan Jan 08 '21 at 18:28