Announcement

Collapse
No announcement yet.

help with c++ - trying to execute a bat

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with c++ - trying to execute a bat

    I wrote a small C++ app that first runs a batch script then rewrites that same batch script to include a new date

    The date part is fine and works but the batch script refuses to run and I get no errors


    Source
    Code:
    #include <iostream>
    #include <fstream>
    #include <Windows.h>
    #include <stdio.h>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    	SYSTEMTIME st;
    	GetSystemTime(&st);
    	ofstream outputFile("sync.bat");
    
    	int test = system("sync.bat");
    	if( test == -1)
    	{   cout << "Cannot execute command"; exit(1); }
    
    	outputFile << "ROBOCOPY \"G:\\Music\\Full Albums\" J:\\Music /S /Z /MAXAGE:";
    
    	outputFile << st.wYear;
    	if(st.wMonth < 10)
    		outputFile << 0;
    	outputFile << st.wMonth;
    	if(st.wDay < 10)
    		outputFile << 0;
    	outputFile << st.wDay;
    
    	outputFile << " /W:300";
    
    	outputFile.close();
    
    	return 0;
    }
    sync.bat
    Code:
    ROBOCOPY "G:\Music\Full Albums" J:\Music /S /Z /MAXAGE:20050910 /W:300

    What am i doing wrong here?
    2004 G35 Coupe project

  • #2
    When you say refuses to run I assume you mean that the system command always returns -1 right? If so try moving the 'ofstream outputFile("sync.bat");' line some time after the system(... command. That may be opening the file for writing and not giving you access to write it.


    EDIT: On the last line I meant give you access to execute it not write it.
    AMD XP 2600+/512MB RAM/120GB hard drive
    Opus 150W/DVD/GPS/7" Lilliput TS/802.11g/Bluetooth
    Installed.


    -GPSSecure- - GPS Tracking
    -AltTabber2.2.2- - Handy touchscreen utility.

    Comment


    • #3
      Moved ofstrem below the system() and it worked

      Thanks!
      2004 G35 Coupe project

      Comment


      • #4
        No problem
        AMD XP 2600+/512MB RAM/120GB hard drive
        Opus 150W/DVD/GPS/7" Lilliput TS/802.11g/Bluetooth
        Installed.


        -GPSSecure- - GPS Tracking
        -AltTabber2.2.2- - Handy touchscreen utility.

        Comment


        • #5
          quick edit, brady.
          Installed
          Asus A7N8X-VM - AMD Mobile Athlon 2400
          512 Ram - 60GB HD - Opus 150w
          Lilliput 7" - Rikaline 6010
          [00000000000001100010-] 98% Completed

          Check Out My Install!!!

          Comment


          • #6
            Originally posted by Quattro
            quick edit, brady.
            I don't know what you're talking about
            AMD XP 2600+/512MB RAM/120GB hard drive
            Opus 150W/DVD/GPS/7" Lilliput TS/802.11g/Bluetooth
            Installed.


            -GPSSecure- - GPS Tracking
            -AltTabber2.2.2- - Handy touchscreen utility.

            Comment


            • #7
              Do you know how I would shift the time down from UTC to Pacific time? It says its the 13th right now and thats screwing things up
              2004 G35 Coupe project

              Comment


              • #8
                Its a difference of 7 hours from UTC and pacific, so if you can compare your time with the UTC it would be easy. So if is still before 07:00 just subtract 1 from the day.
                Installed
                Asus A7N8X-VM - AMD Mobile Athlon 2400
                512 Ram - 60GB HD - Opus 150w
                Lilliput 7" - Rikaline 6010
                [00000000000001100010-] 98% Completed

                Check Out My Install!!!

                Comment


                • #9
                  Doh! That was too easy, thanks


                  Fiinished source if anyone cares
                  Code:
                  #include <iostream>
                  #include <fstream>
                  #include <Windows.h>
                  #include <stdio.h>
                  #include <cstdlib>
                  
                  using namespace std;
                  
                  int main()
                  {
                  	SYSTEMTIME st;
                  	GetSystemTime(&st);
                  
                  	int test = system("sync.bat");
                  	if( test == -1)
                  	{   cout << "Cannot execute command"; exit(1); }
                  
                  	ofstream outputFile("sync.bat");
                  
                  	outputFile << "ROBOCOPY \"G:\\Music\\Full Albums\" J:\\Music /S /Z /MAXAGE:";
                  
                  	if(st.wHour < 7)
                  		st.wDay -= 1;
                  
                  	outputFile << st.wYear;
                  	if(st.wMonth < 10)
                  		outputFile << 0;
                  	outputFile << st.wMonth;
                  	if(st.wDay < 10)
                  		outputFile << 0;
                  	outputFile << st.wDay;
                  
                  	outputFile << " /W:300";
                  
                  	outputFile.close();
                  
                  	return 0;
                  }
                  2004 G35 Coupe project

                  Comment


                  • #10
                    GetSystemTime() will give you UTC
                    GetLocalTime() will give you local or whatever yours is.

                    Comment

                    Working...
                    X