上下文:
-Windows 10
-C++
-批处理文件
-我想在.bat
文件中调用.cpp
文件并获取int
作为返回值
-批处理文件计数并重命名作为参数传递的给定文件夹中的.jpg
文件
-批处理文件代码:
::%1 is the path to the base folder
::%2 is the name of the folder of the images
setlocal enabledelayedexpansion
@echo off
CD /D %1
set cnt=0
for %%f in (%2\*) do (
set newName=000!cnt!
set newName=!newname:~-4!
ren %%f !newName!.jpg
set /a cnt+=1
)
@echo %cnt% files renamed in order
exit /b %cnt%
问题:
我想我已经知道如何传递参数...您需要在调用的.bat文件后放置空格并输入所需的参数。
例如:
要在
L:/baseFolder/water
文件夹中运行我的脚本,我将使用:system(file.bat L:\\baseFolder water)
如何在cpp文件中获取以
cnt
返回的exit /b %cnt%
值作为变量?我是否应该使用
exit
来获取此整数?奖励:如果我想返回多个值怎么办?
最佳答案
MSDN描述了 system()
的用法。我引用有关返回值的部分:
我以某种方式假定批处理文件的返回码是命令解释器的返回码,但是我不确定,也没有找到合适的文档。关于这个。
因此,我做了一个小样本,并在本地进行了尝试。testExitBat.cc
:
#include <Windows.h>
#include <iostream>
int main()
{
int ret = system("testExitBat.bat Hello");
std::cout << "testExitBat.bat returned " << ret << '\n';
return 0;
}
testExitBat.bat
:::%1 an argument
echo "$1: '"%1%"'"
exit /b 123
我在VS2013(Windows 10)上编译并运行了它:
C:\Users\Scheff>echo "$1: '"Hello"'"
"$1: '"Hello"'"
C:\Users\Scheff>exit /b 123
testExitBat.bat returned 123
关于c++ - 如何在C++ Windows中的批处理文件中传递参数并获取返回的退出值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51862091/