Thread: Newbie Question: CMD Line Batch File to C++

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Newbie Question: CMD Line Batch File to C++

    I have just started to find the need to create cmd line batch files and I want to convert them to an .exe file for security reasons. I don't want to use a ".bat to .exe" converter because I would rather learn how to rewrite batch files in C++ myself.

    The batch file I am using tells DriveImage XML to start under the schedualed tasks (Windows), where to place the backup file, add a date to the file name and has the required switches at the end.

    Code:
    "C:\Program Files (x86)\Runtime Software\DriveImage XML\dixml.exe" /bc /t"D:\Backup\DIXML %date:~4,2%-%date:~7,2%-%date:~10%" /r- /s- /c2 /v
    As I said before, I want to learn how to do this myself. Unfortunately, google did not come up with favorable results when I searched for an answer. I want to say that most of the results either were ads for ".bat to .exe" type programs or people asking how to run multiple .bat files within (insert programming language).

    Is there a specific book or topic that covers this particular subject at hand (rewriting batch files for C++)?

    Thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Batch files do specific operations. You can reproduce them by means of a program but you'd have to:
    1) Know or learn all the possible instructions and what each of them do ( or those you'll use ) that go in the file
    2) Find a way to implement it in the language of your choise. The goal is to have the *.exe file do exactly what the *.bat does.
    2a) Know or learn the language of your choise ( preferably inside out )
    2b) Know or learn how to implement those batch file instructions

    You will need compiler-like behaviour. Search for compiler building tuts.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    The simplest solution is to use system() function: http://www.cplusplus.com/reference/c...tdlib/system/:

    Code:
    #include <cstdlib>
    
    int main(void)
    {
        std::system("\"C:\\Program Files (x86)\\Runtime Software\\DriveImage XML\\dixml.exe" /bc /t\"D:\Backup\DIXML %date:~4,2%-%date:~7,2%-%date:~10%\" /r- /s- /c2 /v");
        return 0;
    }
    Although this will do what it is supposed to, it works only for simple commands (echo, start, etc). For example, using this function you cannot reproduce a batch which uses conditional jumps, labels, and other more advanced instructions.
    You also have to know that this solution will not give you much security. The user can still open the .EXE in a hex editor, look for the string, and modify it without much problem. The security is related to protecting the command line string (you can try to encrypt it) or even the whole executable.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    GReaper - I just signed up for a six week online class "C++ for the Absolute Beginner" to give me a good start on the basics. From there I hope to do some self paced learning after finishing the course.

    kmdv - Thank you for the simplest solution for my sample code. As you pointed out, it can be opened by a hex editor but that is one more step that needs to be done than just Right Click - Edit and then adding more commands to the batch (something I just read in Seven Deadliest Microsoft Attacks). I am still learning how to crawl when it comes to the other side of point & click

    Thank you both for taking the time to answer my first of probably many questions.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    I am having a bit of a problem with the system function() simple solution code...

    Dev-C++ 4.9.9.2
    New .cpp file
    Typed, not copied, the code from the previous post.
    When I went to compile it, I there were some errors.
    Searched google, changed the code until I received no more errors when compiling to the following:

    Code:
    #include <cstdlib>
    
    int main(void)
    {
        std::system("\"C:\\Program Files (x86)\\Runtime Software\\DriveImage XML\\dixml.exe\" /bc /t\"\"D:\\Backup\\DIXML %date:~4,2%date:~7,2%-%date:~100%\" /r- /s- /c2 /v");
        return 0;
    }
    After running the code, the CMD.exe flashed very fast so I placed a Cursor on each line and then ran the debug until I saw the following:

    'C:\Program' is not recognized as an internal or external command, operable program or batch file.
    From what little knowledge I have, it stops because of the space after C:\Program. I just don't know how to fix that problem at the moment...

    Back to google.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Try adding another "\ to the beginning like so:

    Code:
    #include <cstdlib>
     
    int main(void)
    {
        std::system("\"\"C:\\Program  Files (x86)\\Runtime Software\\DriveImage XML\\dixml.exe\" /bc  /t\"\"D:\\Backup\\DIXML %date:~4,2%date:~7,2%-%date:~100%\" /r- /s- /c2  /v");
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Adding another "\ worked

    Now I will spend some time taking it all a part to see why it worked so I understand it for future reference.

    Thanks!!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can take that string and print it to the console using std::cout or use a debugger to see what the actual string is. Whatever that actual string is is exactly what the system will try to execute. So running it inside a cmd process, and you should be able to spot if there are any errors in the string. It should also give you a better understanding of whatever command line it is executing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Elysia, I was not able to get the std::cout working. When I start the .exe, the CMD window is blank on the inside as opposed to when I used the batch file.

    After a bit of head scratching, I finally was able to get DriveImage XML to do what it was suppose to do. After fixing a couple of typos and making a "backup" directory, everything worked just like the batch file.

    Here is my final code that starts DriveImage XML with the Task Scheduler

    Code:
    #include <cstdlib>
    int main(void)
    {
        std::system("\"C:\\Program Files (x86)\\Runtime Software\\DriveImage XML\\dixml.exe\" /bc /tD:\\Backup\\DIXML %date:~4,2%-%date:~7,2%-%date:~10% /r- /s- /c2 /v");
        return 0;
    }
    Thanks once again to everybody that helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Batch File? [Newbie help]
    By Shtick in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2010, 10:28 AM
  2. Batch file
    By HunterCS in forum Tech Board
    Replies: 4
    Last Post: 05-19-2007, 12:21 AM
  3. Quick, newbie question: Multiple line string
    By crummy in forum C# Programming
    Replies: 2
    Last Post: 03-10-2005, 06:58 AM
  4. Batch file or VisualBasic file to send to USB port
    By Rod McDevitt in forum Windows Programming
    Replies: 1
    Last Post: 03-28-2004, 03:09 AM
  5. Batch file?
    By Silentsharp in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2002, 06:23 PM