1. 程式人生 > >程序方式執行CMD命令及使用CMD命令一次建立多級錄

程序方式執行CMD命令及使用CMD命令一次建立多級錄

void exceCmd(LPCTSTR cmdLines)
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(STARTUPINFO);
	ZeroMemory( &pi, sizeof(pi) );

	//這兩句一定要,否則會顯示出黑框框.
	si.wShowWindow = SW_HIDE;
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

	TCHAR cmdBuf[0x100]={0};
	_stprintf_s(cmdBuf,TEXT("cmd /k %s"),cmdLines);
	// Start the child process. 
	if( !CreateProcess( NULL,   // No module name (use command line)
		cmdBuf,        // Command line
		NULL,           // Process handle not inheritable
		NULL,           // Thread handle not inheritable
		FALSE,          // Set handle inheritance to FALSE
		0,              // No creation flags
		NULL,           // Use parent's environment block
		NULL,           // Use parent's starting directory 
		&si,            // Pointer to STARTUPINFO structure
		&pi )           // Pointer to PROCESS_INFORMATION structure
		) 
	{
		OutputDebugString(TEXT("失敗"));
		return;
	}

	// Wait until child process exits.
	WaitForSingleObject( pi.hProcess, INFINITE );
	// Close process and thread handles. 
	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );
}
BOOL makeDir(LPCTSTR strPath)
{
	CString Str=strPath;
	if(Str.GetLength()>3)
	{
		if( Str.ReverseFind('.') > 0 )
		{
			int Index = Str.ReverseFind('\\');
			Str = Str.Left(Index);
		}
		Str = TEXT("md ")+Str;
		exceCmd(Str);
		return TRUE;
	}
	return FALSE;
}