PUT命令输出到变量 [英] Put command output into a variable

查看:116
本文介绍了PUT命令输出到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个目录与下列文件

  image1.txt
image2.txt
image3.txt

我想(第一让文件由数据进行排序,oldset日期)获得最早的文件:

  DIR / B / OD C:\\测试\\图像.TXT |? FINDSTR ^ 1

这伟大工程手动输入到CMD.EXE的时候。现在,(在一个批处理脚本)我想要把这个命令的输出中的一个变量。我怎样才能做到这一点?谢谢!

更新:
不知道是否有没有USNG一个循环的直接途径?


解决方案

 对于('DIR / B / OD C / F %% A:\\测试\\图像* .TXT | FINDSTR ^ 1')做设置myVar的= %%一

您可以做到这一点通过For循环,尝试在命令行中,我只是测试它,它工作正常。

输出:

 设置myVar的= image1.txt

在执行set命令行上可以看到:

  myVar的= image1.txt
NUMBER_OF_PROCESSORS = 2

Given a directory with the following files

image1.txt
image2.txt
image3.txt

I want to get the oldest file (let the files be sorted by data, oldset date first):

dir /b /od c:\test\image?.txt | findstr ^1

That works great when manually typing it into cmd.exe. Now (in a batch script) I want to put the output of this command in a variable. How can I do this? Thank you!

Update: Wondering if there is a direct way without usng a loop?

解决方案

For /F %%A in ('"dir /b /od C:\test\image*.txt|findstr ^1"') do set myVar=%%A

You could do it through For loop, try that in command line, I just tested it and it works fine

Output:

set myVar=image1.txt

On executing Set on command line you can see:

myVar=image1.txt
NUMBER_OF_PROCESSORS=2

这篇关于PUT命令输出到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆