# Problem with system() function in C



## gk2k (Nov 1, 2008)

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 (Nov 1, 2008)

Hmmm... This worked for me:

```
system( "ren c:\\gk1\\gk1 gk2" );
```

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


----------



## gk2k (Nov 2, 2008)

I tried that it gives error "Invalid Path or filename"
Even the cd command gives"Invalid directory"
I've attached my code here

```
#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


----------



## QwertyManiac (Nov 2, 2008)

Shouldn't there be space between a command and its arguments?


----------



## gk2k (Nov 2, 2008)

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


----------



## gk2k (Nov 15, 2008)

Any help please?


----------



## parthbarot (Nov 15, 2008)

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.


----------



## MetalheadGautham (Nov 15, 2008)

You are supposed to insert the FULL address in both the source and the destination.


----------



## parthbarot (Nov 17, 2008)

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.


----------



## MetalheadGautham (Nov 17, 2008)

^^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.


----------



## Krazy_About_Technology (Nov 17, 2008)

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.


----------



## gk2k (Nov 20, 2008)

@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.


----------

