Can I store the output of a command in a variable in batch scripting?

浪尽此生 提交于 2020-02-07 06:42:13

问题


I want to dir a folder and get in a variable all the names of the *.csv files.

Does anyone know how can I do this?


回答1:


Using Bash you can try:

dirlist=`ls -1 *.csv`
echo "$dirlist"

Under windows you can read the following Q and A:

Windows batch files: How to set a variable with the result of a command?

Something like:

for /f "delims=" %%a in ('dir') do @set foobar=%%a

Change 'dir' to what you want. I have no windows machine close by, so cannot test.




回答2:


I apologize for a duplicate response, but StackOverflow has reputation counters that impede me from certain actions.

When executing directly on the command-prompt, use the following form:

for /f "delims=" %a in ('whoami') do @set myaccount=%a

The form in previous answer is intended when used in a .bat or .cmd file.



来源:https://stackoverflow.com/questions/6607989/can-i-store-the-output-of-a-command-in-a-variable-in-batch-scripting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!