Results 1 to 10 of 10

Thread: help with c++ - trying to execute a bat

  1. #1
    FLAC aoLhaTer's Avatar
    Join Date
    Mar 2003
    Location
    Rohnert Park, CA
    Posts
    1,122

    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?

  2. #2
    Super Moderator xBrady's Avatar
    Join Date
    Apr 2004
    Location
    USA
    Posts
    3,687
    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.

  3. #3
    FLAC aoLhaTer's Avatar
    Join Date
    Mar 2003
    Location
    Rohnert Park, CA
    Posts
    1,122
    Moved ofstrem below the system() and it worked

    Thanks!

  4. #4
    Super Moderator xBrady's Avatar
    Join Date
    Apr 2004
    Location
    USA
    Posts
    3,687
    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.

  5. #5
    I see dead kittens Quattro's Avatar
    Join Date
    Dec 2003
    Location
    New York
    Posts
    2,922
    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!!!

  6. #6
    Super Moderator xBrady's Avatar
    Join Date
    Apr 2004
    Location
    USA
    Posts
    3,687
    Quote 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.

  7. #7
    FLAC aoLhaTer's Avatar
    Join Date
    Mar 2003
    Location
    Rohnert Park, CA
    Posts
    1,122
    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

  8. #8
    I see dead kittens Quattro's Avatar
    Join Date
    Dec 2003
    Location
    New York
    Posts
    2,922
    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!!!

  9. #9
    FLAC aoLhaTer's Avatar
    Join Date
    Mar 2003
    Location
    Rohnert Park, CA
    Posts
    1,122
    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;
    }

  10. #10
    What can I say? I like serial. Curiosity's Avatar
    Join Date
    Mar 2004
    Location
    Florence Yall, BFKY
    Posts
    2,684
    GetSystemTime() will give you UTC
    GetLocalTime() will give you local or whatever yours is.

Similar Threads

  1. making a BAT file?
    By djmickyg in forum Software & Software Development
    Replies: 10
    Last Post: 06-08-2005, 04:24 AM
  2. Bat Mobile Install
    By Batmanatthenewb in forum Show off your project
    Replies: 38
    Last Post: 05-25-2005, 09:35 AM
  3. Help with a bat file
    By GARB-E in forum Operating System Optimization
    Replies: 1
    Last Post: 02-03-2005, 03:57 PM
  4. Great working tank circuit with backup bat.
    By NightHunter in forum Power Supplies
    Replies: 6
    Last Post: 04-29-2004, 10:51 PM
  5. Can a bat file do this?
    By BLACKOUT in forum Software & Software Development
    Replies: 11
    Last Post: 02-22-2001, 05:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •