# Using XCopy with variables (C++)



## h31s (Jun 26, 2010)

I have a section where the user enters the directory they want to copy, and the directory they want to copy it to.

(cin >> Dir;
cin >> DirTwo

I can't figure out how to get this to work:
system("xcopy Dir DirTwo /e");

Can someone help please? Thanks.


----------



## leroys1000 (Aug 16, 2007)

Could be wrong,but as far as I know,xcopy is a built in system command
and should not need a system call.
Should be......
xcopy Dir DirTwo /e
Might check the C+ help files.
The ; is probably needed to close the statement.
Not a programmer myself,so don't crash your program on my account.
Without all the other stuff.
The /e switch is for copying empty directories,so if the directories aren't empty
you may not need that.
If the directory includes hidden system files like .dll files,you probably
want to use the /h switch to make sure they get copied.
Otherwise
xcopy will ignore them.


----------



## h31s (Jun 26, 2010)

leroys1000 said:


> Could be wrong,but as far as I know,xcopy is a built in system command
> and should not need a system call.
> Should be......
> xcopy Dir DirTwo /e
> ...


I tried the code you gave me, except I replaced the E with an H, and it gives these compile errors:


```
error C2065: 'xcopy' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'Dir'
error C2146: syntax error : missing ';' before identifier 'DirTwo'
error C2065: 'h' : undeclared identifier
```


----------



## leroys1000 (Aug 16, 2007)

OK maybe the system call is needed for a program statement.
May be that the switch is just in the wrong position.
Try this.
system("xcopy /h Dir DirTwo");
I am assuming you are using the actual directory names and locations of course.
But like I say,I'm not a programmer.
If that doesn't straighten it out,maybe someone with more experience can help.


----------



## h31s (Jun 26, 2010)

Nope, didn't work. It compiled fine, though. In the console it says "File not copied. 'Dir' not found."
Thanks for trying though.


----------



## leroys1000 (Aug 16, 2007)

Do the source and target directories exist.
Are you using the actual dicectories and not Dir and DirTwo.
If the target directory doesn't already exist,you may need to
run a command to create it before running xcopy.
Create a directory at the target location with the same name if needed.
That would probably give it the directory to copy to.
If there is a directory in the structure above that one use the parent directory.
So if you want it to go to say the windows directory,use that as a target.
In otherwords,xcopy won't create the new directory,it needs an existing location
to send the original directory to.
Hope that's understandable.

Also,if the source directory name is a long one,it may be truncated
at the command level to conform to standards.
You may want to navigate to the directory in a command prompt
and see if the name has been shortened and use the dir name
as displayed in the command prompt.


----------



## h31s (Jun 26, 2010)

leroys1000 said:


> Do the source and target directories exist.
> Are you using the actual dicectories and not Dir and DirTwo.
> If the target directory doesn't already exist,you may need to
> run a command to create it before running xcopy.
> ...


1. The user types both the directories, which declares Dir and DirTwo before it tries to copy them.
2. Both paths already exist.


----------



## leroys1000 (Aug 16, 2007)

Can you post the exact command you are using?


----------



## h31s (Jun 26, 2010)

To make it all easier, heres the entire thing:


```
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;

int main()
{ 
    char Drive[80];
    char a[1];
    char b[2];
    char Dir[80];
    char DirTwo[81];
    char n[3];
        cout << "Welcome to Hoffer's automatic extraction application!\n\nWould you like to specify where to extract the files,\nor put them in the main drive? A.Specify B.Desktop\n";
    if
        (cin >> b)
    {
        cout << "What is your drive letter?\nExample: If your drive is C:/ then you should type ''C'' without the quotes.\n";
        cin >> Drive;
        cout << "The files will be extracted to \n"
            << Drive
            << ":/SERVER\nIs this okay? (y/n)\n";
    }
        if
        (cin >> n)
        {
            cout << "At this point, you can just type the path of where to extract it.\n";
        cout << "Type the exact path of the folder ''SERVER''. (Include the SERVER file too!)\n";
        cin >> Dir;
        cout << "Type the exact path you want to extract it to.\n";
        cin >> DirTwo;
        cout << "The program will now attempt to extract the files.\n";
        system("xcopy Dir DirTwo /h");
        system("pause");
        return 0;
        }
        }
```
Take note that I still have to add loops, if's, etc...
EDIT: The part you're looking for is at the bottom.


----------



## leroys1000 (Aug 16, 2007)

I get the idea of what you are trying to do,problem is
xcopy may not be the way to go.
I think xcopy needs actual existing directory names and
you are trying to set assigned variable directories.
xcopy can't take the input of variables like dir and dirtwo.
It needs c:\"directory name" "directory name" using the actual locations.
That is why it is generating not found errors.

I don't know if there is a way to take the variable inputs and have the
program generate an appropriate xcopy statement,then run it.


----------



## TheOutcaste (Aug 8, 2007)

I agree with leroys1000, in this statement:

```
System("xcopy /h Dir DirTwo");
```
Dir and DirTwo are being seen as strings here, their values are not being substituted.

I don't know C++ syntax, but for VBS you'd need to put the command line into a variable so the values will be used, then use the variable in the system call:

```
Command = "xcopy /h " & Dir & " " & DirTwo
system(Command);
```
HTH

Jerry


----------



## h31s (Jun 26, 2010)

I am also not great with C++ syntax. I slap that in and modify stuff around when the compile fails and I can't seem to get it to work. It says:


```
1>c:\users\hoffer\desktop\inputoutput\extractor\extractor\main.cpp(34) : error C2065: 'Command' : undeclared identifier
1>(34) : error C2296: '&' : illegal, left operand has type 'const char [10]'
1>(34) : error C2297: '&' : illegal, right operand has type 'char [80]'
1>(35) : error C2146: syntax error : missing ';' before identifier 'system'
1>(35) : error C2065: 'Command' : undeclared identifier
```
(34) and (35) say this:


```
system(Command);
system("pause");
```
It says nothing specifically about the line:

```
Command = "xcopy /h " & Dir & " " & DirTwo"
```
(33)
But it refers to it.


----------



## leroys1000 (Aug 16, 2007)

system(Command);
Are there supposed to be quotes around Command?

Command = "xcopy /h " & Dir & " " & DirTwo"
This looks incomplete.
Should be?
Command = "xcopy /h " & Dir & " " & DirTwo &";
?


----------



## h31s (Jun 26, 2010)

None of the suggestions worked. Isn't there another way to copy a whole folder?


----------



## TheOutcaste (Aug 8, 2007)

Probably need to Dim (or is it referred to as declare?) the Command variable:

```
string Command;
```
The *&* is not the correct operator for concatenating strings, it's for a Bitwise AND. Strings are concatenated with the *+* sign.
Try this instead:

```
Command = "xcopy /h " + Dir + " " + DirTwo;
```
I don't think Command should be quoted in the System call; if it is, it will be seen as a string instead of a variable.


----------



## h31s (Jun 26, 2010)

error C2110: '+' : cannot add two pointers


----------

