problem in executing .bat file
-
Hi all, I am using VISTA and vc++ (using VS-2008). In my application I am creating one .bat file using CreateFile function, after creating it I am writing three commands in to this file using WriteFile. Now I am running it by using Shellexecute() function. The code piece is- CString csRunCommands = _T("bcdedit /set {bootmgr} device boot > d:\\zzz1.txt"); HANDLE hBatFile = CreateFile(_T("D:\\myBat.bat"),GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_ALWAYS,0,NULL); WriteFile(hBatFile,csRunCommands,csRunCommands.GetLength()*2,&dwReturnVal,NULL); CloseHandle(hBatFile); iReturn = (int)ShellExecute(NULL,_T("Open"),csFilePath,NULL,NULL,SW_SHOWNORMAL); csRunCommands is a CString variable that contains the commands to be written. All the code is running without problem. Return value of ShellExecute is 42 ie gretaer than 32 it means file is getting executed. In the command that I am writing into file I have mention pipe that save the output in to one text file (D:\zzz1.txt). But after executing the code I am not seeing any zzz1.txt file in D:\ drive. If I make a .bat file manually(say man.bat) and edit that by writing same command and run it as administrator then it is saving the output of command in to this text file as "The Operation completed successfully". Please suggest me so that I can execute this .bat file by my code. Thanks
Is there a reason why you cannot run the command directly, like
system(csRunCommands);
bypassing the batch file?
-
Hi all, I am using VISTA and vc++ (using VS-2008). In my application I am creating one .bat file using CreateFile function, after creating it I am writing three commands in to this file using WriteFile. Now I am running it by using Shellexecute() function. The code piece is- CString csRunCommands = _T("bcdedit /set {bootmgr} device boot > d:\\zzz1.txt"); HANDLE hBatFile = CreateFile(_T("D:\\myBat.bat"),GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_ALWAYS,0,NULL); WriteFile(hBatFile,csRunCommands,csRunCommands.GetLength()*2,&dwReturnVal,NULL); CloseHandle(hBatFile); iReturn = (int)ShellExecute(NULL,_T("Open"),csFilePath,NULL,NULL,SW_SHOWNORMAL); csRunCommands is a CString variable that contains the commands to be written. All the code is running without problem. Return value of ShellExecute is 42 ie gretaer than 32 it means file is getting executed. In the command that I am writing into file I have mention pipe that save the output in to one text file (D:\zzz1.txt). But after executing the code I am not seeing any zzz1.txt file in D:\ drive. If I make a .bat file manually(say man.bat) and edit that by writing same command and run it as administrator then it is saving the output of command in to this text file as "The Operation completed successfully". Please suggest me so that I can execute this .bat file by my code. Thanks
-
Is there a reason why you cannot run the command directly, like
system(csRunCommands);
bypassing the batch file?
Hi thanks for replying, If I use system() then the command screen will reflect. I want to execute this without any black screen (by hiding the command execution). When I try to run this .bat file by command prompt(opening cmd as administrator) like- D:\myFolder\mybat.bat then it is saying that 'b' is not recognized as an internal or external command. 'b' is the first character in my command that is written in that file. If I am not wrong, the problem is arising due to unicode because I am writing the command in to file through my code using WriteFile function and second parameter of this function is as- csRunCommands = _T("bcdedit /set {bootmgr} device boot > d:\\zzz1.txt"); If I make a bat file in any drive manually and write the same command there then it is getting execute well. Please give me some solution. thanks
-
Hi thanks for replying, If I use system() then the command screen will reflect. I want to execute this without any black screen (by hiding the command execution). When I try to run this .bat file by command prompt(opening cmd as administrator) like- D:\myFolder\mybat.bat then it is saying that 'b' is not recognized as an internal or external command. 'b' is the first character in my command that is written in that file. If I am not wrong, the problem is arising due to unicode because I am writing the command in to file through my code using WriteFile function and second parameter of this function is as- csRunCommands = _T("bcdedit /set {bootmgr} device boot > d:\\zzz1.txt"); If I make a bat file in any drive manually and write the same command there then it is getting execute well. Please give me some solution. thanks
Madan Chauhan wrote:
the problem is arising due to unicode
correct. The batch file has to be in single byte ACSII encoding.
-
Madan Chauhan wrote:
the problem is arising due to unicode
correct. The batch file has to be in single byte ACSII encoding.
Could you please give me the idea that how can I write in ASCII code in to a bat file. or After writing in unicode to a bat file can I convert the matter that the bat file contain in to ASCII. waiting.
-
Hi I just tried it by using WinExec but getting the same problem. I think the problem is arising due to unicode. I am writing in to file in unicode. Could you please tell me that how can I convert the matter of any .bat file from unicode to ASCII. or how can I write the commands in ASCII in to bat file that I am writing in unicode this time. thanks.
-
Could you please give me the idea that how can I write in ASCII code in to a bat file. or After writing in unicode to a bat file can I convert the matter that the bat file contain in to ASCII. waiting.
You could use
CStdioFile::WriteString
and open the target (batch) file with the flagCFile::typeText
. -
You could use
CStdioFile::WriteString
and open the target (batch) file with the flagCFile::typeText
.Thanks for the suggestion. I used it by :: FILE *f=fopen("D:\\myBat.bat","w"); fprintf(f,"%s\n","bcdedit /set {bootmgr} device boot > d:\\zzz1.txt"); fclose(f); And the problem resolved. Thanks again for your support.
-
Thanks for the suggestion. I used it by :: FILE *f=fopen("D:\\myBat.bat","w"); fprintf(f,"%s\n","bcdedit /set {bootmgr} device boot > d:\\zzz1.txt"); fclose(f); And the problem resolved. Thanks again for your support.
OK, nice and simple!
-
Hi all, I am using VISTA and vc++ (using VS-2008). In my application I am creating one .bat file using CreateFile function, after creating it I am writing three commands in to this file using WriteFile. Now I am running it by using Shellexecute() function. The code piece is- CString csRunCommands = _T("bcdedit /set {bootmgr} device boot > d:\\zzz1.txt"); HANDLE hBatFile = CreateFile(_T("D:\\myBat.bat"),GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_ALWAYS,0,NULL); WriteFile(hBatFile,csRunCommands,csRunCommands.GetLength()*2,&dwReturnVal,NULL); CloseHandle(hBatFile); iReturn = (int)ShellExecute(NULL,_T("Open"),csFilePath,NULL,NULL,SW_SHOWNORMAL); csRunCommands is a CString variable that contains the commands to be written. All the code is running without problem. Return value of ShellExecute is 42 ie gretaer than 32 it means file is getting executed. In the command that I am writing into file I have mention pipe that save the output in to one text file (D:\zzz1.txt). But after executing the code I am not seeing any zzz1.txt file in D:\ drive. If I make a .bat file manually(say man.bat) and edit that by writing same command and run it as administrator then it is saving the output of command in to this text file as "The Operation completed successfully". Please suggest me so that I can execute this .bat file by my code. Thanks
Madan Chauhan wrote:
WriteFile(hBatFile,csRunCommands,csRunCommands.GetLength()*2,&dwReturnVal,NULL);
Why are you using a hard-coded
2
rather thansizeof(TCHAR)
?"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch