如何用WinExec来执行DOS命令?

cbz 2003-02-18 08:49:41
大家好,下面的做法为什么不行呢?
WinExec("copy 111.txt + 222.txt /b",SW_HIDE);
...全文
1990 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
penu 2003-02-26
  • 打赏
  • 举报
回复
怎么不结贴?:(
penu 2003-02-20
  • 打赏
  • 举报
回复
注意:98用command,2000用cmd.
penu 2003-02-20
  • 打赏
  • 举报
回复
假设要合并的文件为1.txt,2.txt,2.txt合并到1.txt中。

=======
1、如果两个文件在同一个目录下(假设都在C:\temp目录下):

SetCurrentDir("c:\\temp");
WinExec("command /c copy 1.txt+2.txt /b",SW_HIDE);

=======
2、如果两个文件不在同一个目录下,
假设 1.txt 在 C:\temp
2.txt 在 D:\temp

SetCurrentDir("c:\\temp");
WinExec("command /c copy 1.txt+D:\\temp\\2.txt /b",SW_HIDE);


明白了吗?
penu 2003-02-19
  • 打赏
  • 举报
回复
正确的写法是:

2000下:

SetCurrentDir("c:\\");
WinExec("cmd /c copy 1.txt+2.txt /b",SW_HIDE);

98下:

SetCurrentDir("c:\\");
WinExec("command /c copy 1.txt+2.txt /b",SW_HIDE);

此处假设你要复制的文件所在目录为C:\,要合并的文件分别为1.txt和2.txt,合并后文件名仍为1.txt。你的程序中应作相应修改。

如果你合并到一个新文件3.txt,则应为:
2000:
WinExec("cmd /c copy 1.txt+2.txt 3.txt /b",SW_HIDE);
98:
WinExec("command /c copy 1.txt+2.txt 3.txt /b",SW_HIDE);

penu 2003-02-19
  • 打赏
  • 举报
回复
楼主:

你的语句
WinExec("copy 111.txt + 222.txt /b",SW_HIDE);
有误:

111.txt + 222.txt 之间不能有空格,应该为:
111.txt+222.txt
cscer 2003-02-19
  • 打赏
  • 举报
回复
呵呵~

不错!

学习……
cbz 2003-02-19
  • 打赏
  • 举报
回复
to penu(懒猫) :
不知道是否能在WinExec的命令行里面用路径呢? 我在我这里试了好像不行,
WinExec("cmd /c copy c:\\1.txt+c:\\aaa\\2.txt /b",SW_HIDE);
pepsi1980 2003-02-18
  • 打赏
  • 举报
回复
建议用CreateProcess(),ShellExecute()

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c:\\MyProgram.exe";
ShExecInfo.lpParameters = ""; //可以加参数
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

以下是一个使用CreateProcess()函数的例子:
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
{

PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;

// Set up members of STARTUPINFO structure.
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.lpReserved = NULL;
siStartInfo.lpReserved2 = NULL;
siStartInfo.cbReserved2 = 0;
siStartInfo.lpDesktop = NULL;
siStartInfo.dwFlags = 0;


// Create the child process.
CreateProcess(

NULL,
strCmdLine.c_str(),
NULL, // process security attributes
NULL, // primary thread security attributes
0, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION

// Wait for the processs to finish
DWORD rc = WaitForSingleObject(
piProcInfo.hProcess, // process handle
INFINITE);
}

cbz 2003-02-18
  • 打赏
  • 举报
回复
to cdws222(二当家) ;
并不是dos命令只能用system来执行的,由于讨厌system出现的黑屏,才用WinExec来运行的
cdws222 2003-02-18
  • 打赏
  • 举报
回复
人家明明是WinExec嘛干什么让人家运行DOS命令?
运行DOS命令的函数是system("copy 111.txt + 222.txt /b");
cbz 2003-02-18
  • 打赏
  • 举报
回复
咦,用楼上的方法试试,行了,不过,不清楚为什么不能再命令中加上路径呢?
如下不行:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
WinExec("cmd /c copy c:\\111.txt + c:\\222.txt /b",SW_HIDE);
}
而非得:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SetCurrentDir("c:\\");
WinExec("cmd /c copy zbc.txt + gsx.txt /b",SW_HIDE);
}
呢?
解决了,马上结贴
COKING 2003-02-18
  • 打赏
  • 举报
回复
win98 :command /c dos命令 或 command /k dos命令

win2000 :cmd /c dos命令 或 cmd /k dos命令


guanri 2003-02-18
  • 打赏
  • 举报
回复
你的dos命令就错了

13,871

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧