calling a batch file from C++

Hello..

My system lately's been restarting a lot and hence I'm missing out on my wonderfully scheduled night UL downloads.. so I wrote this c++ code that i want to run on start up..

It basically checks the time and then runs a batch file accordingly..

The only problem is that i'm unable to get the system() function to work properly.. I get a -1 error code..

here is a snippet..

Code:
# include<iostream.h>

# include<stdlib.h>

int callprog()

{

	int error;

	error = system("C:\\windows\\system32\\notepad.exe");

	cout<<"any errors? "<<error<<endl;

	return (0);

}

This is the test code, In the end I just want to call a batch file that has the connect and open Utorrent, Dl manager...

what am i doing wrong?? tks!
 
yeah, thats what i'v done..

in scheduled tasks,

open Azureus @2am

and close Azureus @ 8am

then on router, scheduled connect at 2:02 and disconnect at 7:58 ( router running tomato.. )

but now, due to frequent powercuts, my system keeps restarting at night.. hence in the morning none of my dl's are done... so, wanted to make a prog, that runs at startup, and checks the time.. if the time is in between 2am and 8am, it starts azureus, runs my telnet script that starts wan..

I just finished writing a batch file for that... I started with C++, but was unable to use the system() function.. was getting -1 error..
 
sure.. this is just the rough draft.. will polish it tonight.. :)

Code:
#include <iostream.h>

#include <conio.h>

#include <time.h>

#include <stdlib.h>

int gethour(time_t now)

{

	tm* localtim = localtime(&now);

	return localtim->tm_hour;

}

int timecheck(int hour)

{

	if ((hour>1) && (hour<8))

	return (1);

	else

	return (0);

}

int callprog()

{

	int error;

	error = system("C:\\torrent.bat");

	cout<<"any errors? "<<error<<endl;

	return (0);

}

void main()

{       clrscr();

	int c;

	time_t now = time(0);

	int hour=gethour(now);

	c=timecheck(hour);

	if (c==1)

	callprog();

	else cout<<"No NightUL right now..";

	getch();

}
 
I think the problem is in the string you pass to system().

system() returns -1 when there is some error and in this case,the string (path) might not exist.

try this:

what is the default path in your command prompt?
It must be something like C:\users\yourusername right?

copy your torrent.bat file in the same location above and then make this change to your cpp file.

Code:
int callprog()
{
	int error;
	error=system("torrent.bat");
	cout<<"any errors? "<<error<<endl;
	return (0);
}

P.S. In your timecheck() function, it should be
Code:
if ((hour>[B][COLOR="Black"]2[/COLOR][/B]) && (hour<8))
:)
 
oops.. yeah.. I was playing around with the time to test it out.. :) tks!

and about the path, I tried notepad.exe, calc.exe, dir, ipconfig etc.. which should run from the default path right? also, wont the full path work??

btw, the batch file is on the other comp.. I'll paste that in a bit.. thats about 4 lines of code.. so think I'll use that.. but still want to get this code to work..
 
well if none of them are working then i don't think there is a problem with the system() function. was the code working earlier on the same comp?

also do this:

Code:
if(system(NULL))
{
     cout<<"cmd is fine"<<endl;
}
else
{
     cout<<"cmd is not fine"<<endl;
}

passing NULL to system() checks if everything is fine with the command prompt and returns TRUE if it is.

one more thing, are on a 64 bit OS by any chance?
 
i think the hour>1 part was correct in the first place... with hour>2 it will only start from 3am onwards losing you an entire hour of UL time :D the hour<8 is fine already i believe...
 
hehe.. yeah.. the hour part took me more time to finalise than the code itself.. :p >1 or >=2..

Nope.. good ole 32bit xp home.. :) and no.. first time I'm writing this code on this comp.. not used the system function before either.. I just did a fresh install of xp doubt that matters though..
I'll try the NULL thing.. but here's what I did in batch and seems to be working pretty well under testing conditions :)

Timecheck.bat
Code:
@echo off
echo The time is %TIME%,

IF %TIME:~0,2% GTR 1 IF %TIME:~0,2% LSS 8 (
"C:\Program Files\tst10\start.bat"
"C:\Program Files\uTorrent\uTorrent.exe"
pause)

pause
I'v purposely put the "the time is and the pause because I don't like black screens flashing for an instant during bootup...
also, this is start.bat
Code:
@echo off
cd "C:\Program Files\tst10"
tst10.exe /r:start.txt
and start.txt is
Code:
192.168.1.1 23
WAIT "Router login:"
SEND "root\m"
WAIT "Password:"
SEND "password\m"
WAIT "#"
SEND "service wan start\m"
WAIT "#"
SEND "exit"

that would cover it right?? :)
 
vrd said:
well if none of them are working then i don't think there is a problem with the system() function. was the code working earlier on the same comp?

also do this:

Code:
if(system(NULL))
{
     cout<<"cmd is fine"<<endl;
}
else
{
     cout<<"cmd is not fine"<<endl;
}

passing NULL to system() checks if everything is fine with the command prompt and returns TRUE if it is.

one more thing, are on a 64 bit OS by any chance?

I just checked.. Cmd is fine.. but I still get -1 error.. I tired cmd this time.. and c:\\windows\\system32\cmd.exe
btw, I'm using Turbo C++ v3.. could that be a problem??
 
m-jeri said:
Update the compiler to a latest one. or better... yse VS 6.0... i think most of ur pblms will be solved.

Yeah.. I shall do that tonight.. :)

@KingKrool casuse it's only 4MB :D and was kinda over my dllimit.. so needed a small compiler :p
 
Back
Top