DIGIT-ARC read-only archive · snapshot: July 23, 2026

Problem with system() function in C

12 posts · originally from https://geek.digit.in/community/threads/problem-with-system-function-in-c.101077/

G
gk2k Original post #1
Nov 1, 2008 at 7:39 PM
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
swatkat #2
Nov 1, 2008 at 7:46 PM
Hmmm... This worked for me:
Code:
system( "ren c:\\gk1\\gk1 gk2" );

Give two back-slashes (\\). In your code, the character "g" gets escaped...
G
gk2k #3
Nov 2, 2008 at 7:29 AM
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
QwertyManiac
Nov 2, 2008 at 9:00 AM
Shouldn't there be space between a command and its arguments?
G
gk2k #5
Nov 2, 2008 at 4:47 PM
Which command are you pointing to?
If it's dir/p/a it can be given without space also
G
gk2k #6
Nov 15, 2008 at 10:11 AM
Any help please?
parthbarot
Nov 15, 2008 at 10:42 AM
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 at 3:27 PM
You are supposed to insert the FULL address in both the source and the destination.
parthbarot
Nov 17, 2008 at 3:00 PM
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 at 3:15 PM
^^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 at 3:25 PM
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.
G
gk2k #12
Nov 20, 2008 at 7:48 AM
@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.