Link to home
Start Free TrialLog in
Avatar of 9772885
9772885

asked on

C++ CopyFile UNC Failure

I have been trying to copy a file fom my local drive to a UNC path but it doesnt copy, it works if i try to copy from local to local but not to s server

ive tried doubling up the back slashes etc like the below

\\\\?\\UNC\\

does anyone have any further information on this?
Avatar of Altwies
Altwies

You must make sure that you share the directory and set the path/file access security on the remote directory to allow you read write and create access. This usually means that you have to add a user group or "Anyone" to the access privledge list and set the appropriate check boxes corresponding to the privledge level you want to assign.
Avatar of jkr
What does 'GetLastError()' return?
Avatar of 9772885

ASKER

Im very new to C++ so havnt learnt much yet and im not sure how to apply the function you have suggested. I am able to create new files in the destination folder if i open in explorer so shouldnt be a permissions problem


/*Refresh Control*/

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <cstdlib>
#include <istream>
#include <iterator>
#include <fstream>
using namespace std;



int main ()
{

// declare a variable
int i;
int j;

for(i= 0; i < 1; i=0)

    {
      int i;
      printf ("Checking if processor is available...");
      if (system(NULL)) puts ("Ok");
      else exit (1);
      printf ("Executing Refresh...\n");
      i = system ("c:/folder/Refresh.acsauto");                                              
      printf ("RC: %d.\n",i); //Display Return Code


    for(int i=0; i<300000000;i++); //Wait For 5 Seconds
            printf ("Wait Finished");
            
            
            //Copy File
            CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",1);
         

         

    }
return 0;
MessageBox(NULL, "An Error Has Occured", "An Error Has Occured", MB_OK);
      
      
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 9772885

ASKER


ive made that change and set up a test script to try and get the copy to work but its still not copying across, there wasnt a file to overwrite so the old code should still have worked. Do i have this set correctly is there a way to check if there is an error it seems to run through with no problems, should there be a return code i can check?


#include <windows.h>

int main ()
{
            //Copy File
            CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",0);
}
To check for errors, you could do the following:
#include <windows.h>
#include <stdio.h>

int main ()
{
            //Copy File
       BOOL bRC = CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",0);

        if (!bRC) printf("Error: %d\n", GetLastError());
}

Open in new window

Avatar of 9772885

ASKER

I have now tried this, a blank cmd box briefly pops up for a few secinds and then disapears with no error msge and nor is the file copied accross
In that case, try the following, so the error should be readable:
#include <windows.h>
#include <stdio.h>

int main ()
{
            //Copy File
       BOOL bRC = CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",0);

        if (!bRC) printf("Error: %d\n", GetLastError());

        Sleep(20000);
}

Open in new window

Avatar of 9772885

ASKER

Hi there was no error message just a blank screen
If there was no error message, the file should have been copied successfully...
Avatar of 9772885

ASKER

It is now working thank you all for your help.