Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem in executing .bat file

problem in executing .bat file

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiohelp
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Madan Chauhan

    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

    M Offline
    M Offline
    Michael Schubert
    wrote on last edited by
    #2

    Is there a reason why you cannot run the command directly, like

    system(csRunCommands);

    bypassing the batch file?

    M 1 Reply Last reply
    0
    • M Madan Chauhan

      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

      R Offline
      R Offline
      Rane
      wrote on last edited by
      #3

      Can you try with WinExec instead of ShellExecute? Regards, Rane

      M 1 Reply Last reply
      0
      • M Michael Schubert

        Is there a reason why you cannot run the command directly, like

        system(csRunCommands);

        bypassing the batch file?

        M Offline
        M Offline
        Madan Chauhan
        wrote on last edited by
        #4

        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

        M 1 Reply Last reply
        0
        • M Madan Chauhan

          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

          M Offline
          M Offline
          Michael Schubert
          wrote on last edited by
          #5

          Madan Chauhan wrote:

          the problem is arising due to unicode

          correct. The batch file has to be in single byte ACSII encoding.

          M 1 Reply Last reply
          0
          • M Michael Schubert

            Madan Chauhan wrote:

            the problem is arising due to unicode

            correct. The batch file has to be in single byte ACSII encoding.

            M Offline
            M Offline
            Madan Chauhan
            wrote on last edited by
            #6

            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.

            M 1 Reply Last reply
            0
            • R Rane

              Can you try with WinExec instead of ShellExecute? Regards, Rane

              M Offline
              M Offline
              Madan Chauhan
              wrote on last edited by
              #7

              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.

              1 Reply Last reply
              0
              • M Madan Chauhan

                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.

                M Offline
                M Offline
                Michael Schubert
                wrote on last edited by
                #8

                You could use CStdioFile::WriteString and open the target (batch) file with the flag CFile::typeText.

                M 1 Reply Last reply
                0
                • M Michael Schubert

                  You could use CStdioFile::WriteString and open the target (batch) file with the flag CFile::typeText.

                  M Offline
                  M Offline
                  Madan Chauhan
                  wrote on last edited by
                  #9

                  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.

                  M 1 Reply Last reply
                  0
                  • M Madan Chauhan

                    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.

                    M Offline
                    M Offline
                    Michael Schubert
                    wrote on last edited by
                    #10

                    OK, nice and simple!

                    1 Reply Last reply
                    0
                    • M Madan Chauhan

                      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

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      Madan Chauhan wrote:

                      WriteFile(hBatFile,csRunCommands,csRunCommands.GetLength()*2,&dwReturnVal,NULL);

                      Why are you using a hard-coded 2 rather than sizeof(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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups