0

I was looking for a Windows CMD equivalent to something like

var=`ls`

in Unix. Is there a way to do so without having to iterate using for /f?

3
  • please specify your question correctly ls equivalent command in dos is dir Commented Nov 29, 2013 at 5:53
  • if it sis a single line output.. then use CD > tmpFile SET /p myvar= < tmpFile Commented Nov 29, 2013 at 6:32
  • Sadly, the answer is No :-( Unless it is a single line, and you are willing to use a temp file as Prakash suggests. Commented Nov 30, 2013 at 11:23

1 Answer 1

2

You can do it with PowerShell (and script it in a .ps1).

PS C:\> $myVar = & dir | format-table -hidetableheaders -property name
PS C:\> echo $myVar

Program Files
Program Files (x86)
Users
Windows
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but the question was about CMD, and PowerShell ≠ CMD.
There are comments about the fact that this is not possible with CMD batch. So, you can't do it vanilla, BUT, if the need is to retrieve something multiline in a variable to reuse it later, in 2013/2014 we can assume that PowerShell is natively available on the machine, and there is many (most of the time, simple) ways to do that with PowerShell. So, instead of leaving the asker with just a "NO", we give him some new possibilities to do the thing he wants to, without installing third party software.
I agree that "No" may not be very good as the answer, but the difference between PowerShell and CMD is such that I cannot help feeling that suggesting a PowerShell answer to a CMD question is like suggesting a C solution to a Pascal problem. (And I am talking not so much about difference in capabilities as about difference in syntax between the two shells, because that requires new knowledge.)
You are absolutely right and I sincerely apologize for suggesting any alternative. The correct and only possible answer to this question is : "NO, you can't do that. Don't try anything else, shutdown your computer, watch the floor and cry."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.