08-24-2010 03:40 AM
How about two lines of code?
system ("cmd /c attrib -r b:\*.*); // Remove the read-only protection flag
system ("cmd /c copy /y a:\a\*.* b:\"); // Suppress the 'YES' dialogue on the copy
As a refinement you could of course programmatically create the strings to be passed to the system() function.
JR
08-24-2010 04:57 AM
If you want to perform the copy without actually removing attributes on the destination files you may want to check xcopy command and its options, using it with system () as JR suggested.
In particular you could use /r option which overwrites files with read-only attribute and /k which copies attributes from source to destination files. xcopy is a powerful command that can be tailored in a multitude of ways with its options: use "help xcopy" in a dos window to have a list of all options.
08-24-2010 09:15 PM
Hi, JR and Roberto
thanks very much for you suggestion. it looks dos commands are still very powerful
however, i tested those command and i find that command system may only checking the syntax of command string. i did a simple test . just set the deny access in destination folder for specified login user name. i did not get any error or even warning while copy . how can we deal with this one ?
B.R
Gerry
08-25-2010 03:03 AM - edited 08-25-2010 03:04 AM
I must admit that I do not understand you when you say "command system may only checking the syntax of command string"
What did it happened? Was the copy succesful or not?
Which command have you issued? Can you post it here?
08-25-2010 03:40 AM
i think i have described in previous post , however, it may not so clear.
my question is this once i set the acess permission for destination folder. copy will be failed. however, command system does not give any error. the return value is "0". so my question is how can we check the copy is sucess or failed.
Err = system ("cmd /c xcopy /y/r/k D:\\1\\*.* d:\\2\\");
B.R
Gerry
08-25-2010 05:32 AM
Well, xcopy may return an error that you may trap in a batch file by testing ERRORLEVEL condition...
Or you may redirect the output of the fuction to a file and read back it...
Both of them are not clean and good methods I'm afraid!
Digging with file system permissions is a completely different thing than dealing with read-only attributes and a notably harder beast to beat. Moreover, you may be forced to run your program with administrative privileges to modify permissions in the file system even if using low level windows APIs.
Looking at your problem from a different point of view, access privileges on a folder are normally not subject to rapid changes, so you may probably test them at the beginning of your program by copying a dummy file to the destination folder with CopyFile and checking function return value, prompting the user with a descriptive message in case of errors and leaving him to interactively correct the situation before your program may run (he may need to call IT admins to proceed).
08-25-2010 08:25 PM
Thanks Roberto. acturally, file permission is not the problem in my application. because my program is running administion level. however, i just curious about how can we get error. however, in other language i saw command copyfolder. but it looks it's not available in C code. even C++. i did checked MSDN , all samples regards copy folder function are made by JS or VB.
anyway, this is nice discussing. at least i know i shall not throw away DOS command. some of times there are very useful.
B.R
Gerry
08-25-2010 10:25 PM
additional input it may useful for some person who will use this function
when you copy file from other computer. pls careful with below
e.g
system ("cmd /c xcopy /y/r/k \\commpc\\Douc12\\1\\*.* d:\\2\\"); does not work. you should type
system ("cmd /c xcopy /y/r/k \\\\commpc\\Douc12\\1\\*.* d:\\2\\");
i donot know the reason behind. " xcopy /y/r/k \\commpc\\Douc12\\1\\*.* d:\\2\\" could work well in DOS .
B.R
gerry
09-08-2010 02:28 AM
sorry, i want to post this again. because i found a problem in command xcopy.
xcopy do not support space in folder name. e.g. c:\test data.
with this one. xcopy will be failed.
do someone have idea in this part?
B.R
Gerry
09-08-2010 03:17 AM
When you have path with spaces you must embed the full path in quotes for the commands to interpret it correctly: in your case
xcopy "c:\test data\*.*" k:
will be executed correctly both in a dos window and in system () command.
Regarding the previous question, the backslash has a special meaning in c: it can indicate some control code like \n, \t and so on. To indicate a backslach inside a string you must double it. So if you are to use UNC names inside your string, each initial backslash is to be doubled, for a total of 4 backslashes at sting beginning as you have already noted (this also explain why doubling the slashes inside path names is necessary).