如何将命令行变量的值返回到立即窗口 [英] How to return value of command line variable to immediate window
问题描述
情况:
我想从Windows命令提示符中返回字符串变量的实际值。
I want to return the actual value of a string variable from Windows Command Prompt.
命令
cd C:\Users\User\Desktop\Testfolder && (for /f "eol=: delims=" %F in ('dir /b /od *.csv') do @set "newest=%F" ) && echo %newest%
在命令提示符窗口中执行时,正确返回值,例如 Test2.csv
when executed in command prompt window, correctly returns the value e.g. "Test2.csv"
在下面的代码中,我无法将此值返回到Excel的立即窗口中;相反,我得到的命令是%newest%
In the code below I am unable to return this value to the immediate window in Excel; instead, I get the command I issued, which was "%newest%"
代码:
Public Sub Test
Debug.Print CreateObject("wscript.shell").exec("cmd /c cd C:\Users\User\Desktop\Testfolder && (for /f ""eol=: delims="" %F in ('dir /b /od *.csv') do @set ""newest=%F"" ) && echo %newest%").StdOut.ReadAll
End Sub
命令提示符窗口中的结果:
即时窗口中的结果:
我尝试过的操作
我查看了大量资源;我最后在参考文献中列出了其中的一些。各种尝试包括:
I have looked at a large number of resources; a few of which I listed in references at end. Various attempts include:
1)我尝试了转义,但没想到能成功,它是:
1) I have attempted escaping, which I didn't expect to see work, with:
echo %%newest%% '<==== give %Test2.csv% in CLI and %%newest%% in immediate
2)添加等待,例如:
2) Adding waits, for example:
Debug.Print CreateObject("wscript.shell").exec("cmd /c cd C:\Users\User\Desktop\Testfolder && (for /f ""eol=: delims="" %F in ('dir /b /od *.csv') do @set ""newest=%F"" ) && PING localhost -n 2 >NUL && echo %newest%").StdOut.ReadAll
3)插入文本文件并读回:
3) Piping to a text file and reading back:
CreateObject("wscript.shell").exec ("cmd /c cd C:\Users\User\Desktop\Testfolder && (for /f ""eol=: delims="" %F in ('dir /b /od *.csv') do @set ""newest=%F"" )&& echo %newest% > C:\Users\User\Desktop\Testfolder\fileInfo.txt") '.StdOut.ReadAll
Debug.Print CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\UsersUser\Desktop\Testfolder\fileInfo.txt", ForReading).ReadAll
在最后一个示例中,有一次将实际值写到文件中,但之后继续写%newest%
In the last example, on one occasion this wrote out the actual value to the file but after that continued to write "%newest%"
很显然,我不是从命令行中获取已执行语句的结果。
Clearly I am not picking up the result of the executed statement from the command line.
如何我要返回实际值吗?
其他信息:
这是一系列更改到指定目录的命令;循环覆盖该文件夹中的csv文件;查找最近修改的文件并返回其名称。
This is a series of commands which change to a specified directory; loops overs the csv files in that folder; finds the most recently modified file and returns its name.
参考文献:
- Get Return Value of Shellexecute
- Reading output from shell
- Setting and using variable within same command line in windows cmd
- Scripting - Waits
推荐答案
因此,将@FlorentB归功于使用环境变量扩展标志的答案。将此合并到最终答案中,得到以下结果,该结果从指定目录返回了最后修改的文件的文件名。
So credit to @FlorentB for the answer which was to use the environment variable expansion flag. This, incorporated into final answer, gave the following which returned the filename of the last modified file from a specified directory.
CreateObject("wscript.shell").exec("cmd /V /C cd " & myDir & " && (for /f ""eol=: delims="" %F in ('dir /b /od *.csv') do @set ""newest=%F"" ) && echo !newest!").StdOut.ReadAll
这篇关于如何将命令行变量的值返回到立即窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!