Problem Executing "FOR" Batch Command Via system()

Hello everyone,

I am having a problem adding a, much disapproved of, system() line into my c++ program.

I have the following:

1
2
3
4
5
6
7
#include <stdlib.h>

int main()
{
    system("for /R DIR %a in (*) do (ren "%a" "%~na")");
    return 0;
}


I receive errors during compilation in VC.

If I change "%a" and "%~na" to %a and %~na, compilation is successful, but the batch command does not work properly as it will not process filenames with spaces unless the "" are around the %a.

Any help would be greatly appreciated.
I always get confused with quoted strings within shell scripts within a system call.

Did you try escaping the inner quotes?

system("for /R DIR %a in (*) do (ren \"%a\" \"%~na\")");
you can just pad a string with the characters you need exactly and call system on the string, if the above escapes don't work out.
@doug4

that worked sir! Thank you so much for your speedy response. I can't thank you enough.

@johnnin

I will definitely keep taht in mind for future situations. I appreciate your help as well.
Topic archived. No new replies allowed.