Problem with system() function in C

Status
Not open for further replies.

gk2k

gkbhat.blogspot.com
I want to run a program that has to rename a folder H:\gk1\gk1 to gk2
When I run that command using
system("move H:\gk1\gk1 gk2");

it reports an error that "Bad command or file name" .
I even tried the rename command it give "Invalid path or filename"
The specified folder exists and no other application is using it.
If I run the same command from the command prompt it executes.
Please help me..I'm struck with my prog.
 

swatkat

Technomancer
Hmmm... This worked for me:
Code:
system( "ren c:\\gk1\\gk1 gk2" );

Give two back-slashes (\\). In your code, the character "g" gets escaped...
 
OP
G

gk2k

gkbhat.blogspot.com
I tried that it gives error "Invalid Path or filename"
Even the cd command gives"Invalid directory"
I've attached my code here
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
system( "dir/p/a");
system( "cd gk1" );
system( "dir/p/a");
system( "rename c:\\gk1\\gk1 gk2" );
getch();
return 0;
}
I execute this programme in C:\ directory
 
Last edited:
OP
G

gk2k

gkbhat.blogspot.com
Which command are you pointing to?
If it's dir/p/a it can be given without space also
 

parthbarot

In the zone
i think only 2 things...

1. Must use #include <stdlib.h>
2. If command runs on DOS then must run through system command...
and it will move ( move H:\gk1\gk1 gk2 ) in the current directory... i tried...it worked... :)

regards.
 

parthbarot

In the zone
i dnt think so.... because it will make the new folder 'gk2' in the current folder itself...so it does't matter i think... :)

regards.
 
^^Exactly. But what is that your intention in the first place ?

And one more question: Doesn't the system() function accept an array of characters as an arguement and executes the statement in the array as a command in the shell ? So this is an OS dependant question which has little to do with programming error and more to do with the OS he uses.
 
If you are running it from within the compiler IDE the system() function won't work. Compile the program and then run the exe from commandline. You can find out where the output exe is produced in the configuration dialog of your compiler. I assume you are using the same age old Turbo C++ (Yuck!) IDE so you can go to Options>Directories and look at the value of Output Directory. if nothing is there then the output executable will be created in the bin directory of your compiler installation folder.
 
OP
G

gk2k

gkbhat.blogspot.com
@MetalheadGautham
I just want to write a program to rename a folder taking old and new folder name as argument.I didn't get a built in c function to rename the folder.

Some commands like "dir" works.I use tc with windows xp.
@Krazy_About_Technology
I run the exe through the command line only.
 
Status
Not open for further replies.
Top Bottom