I need a simple (but full) example of how to changed directory with a win32 app

My code (shortened) looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <windows.h>

main()
{
   using std::cout;
   using std::cin;
   using std::endl;
   

   int a;

   cout << "================================";
   cout << "==                            ==";
   cout << "==    For 32-bit enter 1      ==";
   cout << "==    For 64-bit enter 2      ==";
   cout << "==                            ==";
   cout << "================================";


   cout << "\n\n\t=======> ";
   cin >> a;

   
   //If for 32-bit directory
   if (a = 1)
      {
         
         system("cls");
         system("cd c:\\ ");
         //The space and the '\\' is so 
         //that cpp doesn't add the " to it
         system("cd program file");
         system("md MyProAlpha1.0");
      }


   //else for a (x86) directory
   else
      {
         system("cls");
         system("cd c:\\ ");
         //The space and the '\\' is so 
         //that cpp doesn't add the " to it
         system("cd program file (x86)");
         system("md MyProAlpha1.0");
      }

   system("pause");
   return 0;
}



But the problem is that the cd (change directory command, doesn't seem to work.

any tip?
Last edited on
Why not use a batch script for this?

You should also be using the Win32API or 3rd Party Library to get the location of Program Files, and then using a file library for creating the directory. Doing it through system commands is a waste of time.

You can also put all of that into a single call.
1
2
system("mkdir C:\\Program Files\\MyProAlpha1.0");
// Notice Program FileS not Program File 


Do NOT use system();, and if you wanted to use a batch file, you should have written the file from the C++.
EXAMPLE :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ofstream myfile ("c:\\x.bat");
if (myfile.is_open())
{

	myfile << "@echo off""\n";
  myfile << "echo HELLO\n";
  myfile << "pause \n";
myfile << "exit\n";
  myfile.close();
system("start c:\\x.bat");
}
else
{
cout << "FAIL";
}

Poor example, and I don't have a compiler on this computer so not 100% sure if it works yet.
Plus if you wanted to make folder you could use CreateDirectory("C:/X", NULL); //create folder
Hope this helps.
Zaita, I'm very new to C++, I'm not sure how to use a batch file. But I'm for learning. and what is Win32SPI?
LittleQuick, I would use your code, but I don't understand what any of it is. Again, I'm still very new, I just know what I need C++ for. However accomplishing my task is much harder. I'd be interested in learning that code, if you have the time and desire to break it down a little. Thanks either way though.
We're here to offer advice on your work, not give you full out solutions. Read through http://www.cplusplus.com/doc/tutorial/files/ to better understand LittleQuick's code.

A batch file has no relation to C++. What I am trying to say is, the problem you're trying to solve is not something that you'd really need to go through all of the hassle of writing an application for when you could write a small script to do it easily. However; if you were going to use C++ then you might as well use something like the Boost library and the WindowsAPI to do it properly.
Thanks. Last question is there a way to execute an xcopy or copy from the current directory to another without having to know the current directory.

Copy folder MyProgramAlpha1.0 >> program files (with unknown directory)
But the .exe is in the MyprogramAlpha1.0

I'm really sorry for being a pain.

When I first stated learning C the first thing I read in multiple places was
"C++ or any programing language is for people who can think logically, more than rationally" I'm starting to understand what they meant :(
This sound's like your trying to build yourself an installer. Why don't you just download one of the free installation building applications?

You also need to do some of the very very basic tutorials in C++. Things that are fundamental to writing even the most simple applications. I suggest you check out the tutorials section of this website.
Topic archived. No new replies allowed.