0

I have a batch file which changes the direction to a specific toolchain and executes one command like this:

cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin

avr-objcopy -O binary C:\Users\cinar\Desktop\hextobin\GccApplication.elf C:\Users\cinar\Desktop\hextobin\GccApplication.bin

I want to do this with my C application. I found this topic, tried the system(); command and it works partially. I can call this:

system("cd");

and get the direction back. But I can not change it with this command:

system("cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin");

This caused a compile warning about unknown escapes, so i added \ to escapes and tried this:

system("cd C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");

I was able to compile and run this but that didn't change the direction.

Is there any possibility to execute my commands with system()? As I just want to change the direction and execute one command, I wanted to keep it simple.

Update: I found this topic afterwards: system("cd <path>") in a C program

Then solved my query with this:

chdir("C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");

system("avr-objcopy -O binary C:\\Users\\cinar\\Desktop\\ff.elf C:\\Users\\cinar\\Desktop\\ff.bin");
Community
  • 1
  • 1
abdullah cinar
  • 543
  • 1
  • 6
  • 20
  • Btw, your `system("cd something");` *did* change the current directory, but only the current directory for the subshell spawned by `system`. It worked like this: your program spawned a subshell, and executed the command `cd something` in the subshell. The subshell's current directory changed. Then the subshell exited and your program continued running. But changing the subshell's current directory was useless because it exited just after! Your program's current directory didn't change. Using `chdir`, you change your program's current directory instead. (But yes, CreateProcess is what you need) – CherryDT May 23 '16 at 10:08
  • I achieved my purpose with `chdir` and it looks like it is working good. I moved my executable file to another directory to find out if it fails, but it worked well. Do you think `chdir` can cause a fail in some cases? I'm currently not using `CreateProcess`. – abdullah cinar May 23 '16 at 10:38
  • If you use `chdir`, you should at least change back the working directory after you spawned the subprocess. The thing is, the `CreateProcess` method starts the subprocess with a certain working directory you pass to the `CreateProcess` function. The `chdir` function changes your own program's working directory, then spawns the subprocess (which inherits your program's current working directory). However, you now changed your own program's current dir., so the next time you do anything with relative paths (e.g. `open('myfile.txt')`) it can fail, so you should save the old dir and change it back – CherryDT May 23 '16 at 10:45
  • Oh, now I can understand what you mean. I had already had a trouble at my further paths after I changed my direction at the beginning of my code. I had solved that problem by adding the first directory to all of my new paths. But you are right, I could change the direction after I completed my work. That would be more meaningful. Thanks! – abdullah cinar May 23 '16 at 10:56
  • 1
    "The chdir function" in my comment above should have read "The chdir *method*" of course. `chdir` doesn't spawn anything. – CherryDT May 23 '16 at 11:00

1 Answers1

3

Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.

Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().

For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.

Also

system("dir Users\\whatEverNextFolder > test.txt");
LPs
  • 16,045
  • 8
  • 30
  • 61
Asim Mushtaq
  • 445
  • 5
  • 14
  • I should search for the CreateProcess option then. Thanks. Also I found this related question: http://stackoverflow.com/questions/24722368/systemcd-path-in-a-c-program – abdullah cinar May 23 '16 at 08:01
  • 1
    Yes CreateProcess Options and the answers in that thread are also followable – Asim Mushtaq May 23 '16 at 08:05