Hilfe, total unlogisch (Ordner kopieren)
-
Ich bekam vor einiger Zeit eine Fkt. CopyFolder von CodeFinder:
CodeFinder schrieb:
Also...das geht so (bitte auch versuchen zu verstehen
):/* Header: #include <windows.h> #include <shellapi.h> Library: #pragma comment(lib, "shell32.lib") */ bool CopyFolder(const TCHAR* pszSrcPath, const TCHAR* pszDestPath) { if(pszSrcPath == NULL || pszDestPath == NULL) return (false); TCHAR* pszBufFrom = new TCHAR[lstrlen(pszSrcPath) + 2]; TCHAR* pszBufTo = new TCHAR[lstrlen(pszDestPath) + 2]; StrCpy(pszBufFrom, pszSrcPath); StrCpy(pszBufTo, pszDestPath); pszBufFrom[lstrlen(pszBufFrom) - 1] = 0; pszBufTo[lstrlen(pszBufTo) - 1] = 0; SHFILEOPSTRUCT shFileOp; shFileOp.hwnd = NULL; shFileOp.wFunc = FO_COPY; shFileOp.pFrom = pszBufFrom; shFileOp.pTo = pszBufTo; shFileOp.fFlags = FOF_NO_UI | FOF_SILENT; // Hier ggf. andere/weitere ... shFileOp.fAnyOperationsAborted = FALSE; shFileOp.hNameMappings = NULL; shFileOp.lpszProgressTitle = NULL; // siehe Hinweis zu 'shFileOp.fFlags' bool fSuccess = !static_cast<bool>(SHFileOperation(&shFileOp)); delete [] pszBufFrom; delete [] pszBufTo; return (fSuccess); }Beispiel:
if(CopyFolder(TEXT("C:\\DemoFolderSrc"), TEXT("C:\\DemoFolderDest"))) MessageBox(NULL, TEXT("Der Ordner wurde erfolgreich kopiert."), TEXT("Kopiervorgang erfolgreich beendet."), MB_OK);Wenn ich die Fkt. nur einmal aufrufe, klappt sie prima.
Aber sobald ich sie zweimal aufrufe, geht das zweite mal immer schief!!!
bool result; result=CopyFolder(TEXT("C:\\Test1\\Test2"),TEXT("C:\\Backup")); if(result) MessageBox(NULL,"Backup erfolgreich durchgeführt.", "Backup - Erfolg", MB_OK); else MessageBox(NULL,"Es trat ein Fehler auf.", "Backup - Fehler", MB_OK); result=CopyFolder(TEXT("C:\\Test3\\Test4"),TEXT("C:\\Backup")); if(result) MessageBox(NULL,"Backup erfolgreich durchgeführt.", "Backup - Erfolg", MB_OK); else MessageBox(NULL,"Es trat ein Fehler auf.", "Backup - Fehler", MB_OK);Die Ausgabe ist immer:
(Msg-Boxen)
1. Erfolg
2. Misserfolg(Natürlich gibt es die 4 Ordner Test1-Test4 !)
ich hab schon Stunden rumprobiert, aber es muss an der Fkt. liegen??

Weiß jemand wo der Fehler liegt?
-
Hoi, ich darf mich doch gleich mal zu Wort melden
^^ ...Ähm also der Fehler ist (sollte
) kein Fehler (sein):
Wenn Du mal Deine beiden Aufrufe meiner Funktion vergleichst, fällt Dir vllt auf, dass die beiden Quellordner zwar existieren und (viel wichtiger) unterschiedlich sind, der Zielordner ist aber im 2. Aufruf der gleiche wie beim 1. .Hm ich hatte dazu auch ne Lösung ... *grummel* ... aber ich find die grad nicht... *grrr* ... .
Versuchs mal mit dem zusätzlichen Flag FOF_NOCONFIRMATION im Element fFlags der Struktur SHFILEOPSTRUCT. Weiß aber nicht, obs das lößt ... *son Mist* ... .
-
Auch mit FOF_NOCONFIRMATION klappts nicht!
-
Also am gleichem Zielordner liegt es definitiv nicht, denn ich hab es mit versch. Zielordnern ausprobiert und da ist der Fehler immer noch!
:bool result; result=CopyFolder(TEXT("C:\\Test1\\Test2"),TEXT("C:\\Backup1")); if(result) MessageBox(NULL,"Backup erfolgreich durchgeführt.", "Backup - Erfolg", MB_OK); else MessageBox(NULL,"Es trat ein Fehler auf.", "Backup - Fehler", MB_OK); result=CopyFolder(TEXT("C:\\Test3\\Test4"),TEXT("C:\\Backup2")); if(result) MessageBox(NULL,"Backup erfolgreich durchgeführt.", "Backup - Erfolg", MB_OK); else MessageBox(NULL,"Es trat ein Fehler auf.", "Backup - Fehler", MB_OK);
-
Hm ... Was sagt GetLastError (bzw. FormatMessage
) ?
-
pszBufFrom[lstrlen(pszBufFrom) + 1] = 0; pszBufTo[lstrlen(pszBufTo) + 1] = 0;CodeFinder hat da - statt + gemacht.
-
Prüf mal ob beim Aufruf von "SHFileOperation ()" die Verzeichnisnamen (pszBufFrom, pszBufTo) korrekt übergeben wurden.
-
Also das funktioniert bei mir einwandfrei (MS VC 2005 - Team Suite):
#ifdef UNICODE #pragma message ("WARNING: UNICODE is not supported. The UNICODE-Macro was undefined automatically.") #undef UNICODE #endif // UNICODE // C++ Standard-Header: #include <iostream> #include <string> #include <conio.h> // Windows-Header: #include <windows.h> #include <shellapi.h> // Library: #pragma comment(lib, "shell32.lib") inline int CtrlConsoleExit(int iExitCode = 0) { std::cout << "Press any key to exit the application..." << std::endl; while(!kbhit()) ; return (iExitCode); } bool CopyFolder(const TCHAR* pszSrcPath, const TCHAR* pszDestPath) { if(pszSrcPath == NULL || pszDestPath == NULL) return (false); TCHAR* pszBufFrom = new TCHAR[lstrlen(pszSrcPath) + 2]; TCHAR* pszBufTo = new TCHAR[lstrlen(pszDestPath) + 2]; lstrcpy(pszBufFrom, pszSrcPath); lstrcpy(pszBufTo, pszDestPath); pszBufFrom[lstrlen(pszBufFrom) + 1] = 0; pszBufTo[lstrlen(pszBufTo) + 1] = 0; SHFILEOPSTRUCT shFileOp; shFileOp.hwnd = NULL; shFileOp.wFunc = FO_COPY; shFileOp.pFrom = pszBufFrom; shFileOp.pTo = pszBufTo; shFileOp.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; // Hier ggf. andere/weitere ... shFileOp.fAnyOperationsAborted = FALSE; shFileOp.hNameMappings = NULL; shFileOp.lpszProgressTitle = NULL; // siehe Hinweis zu 'shFileOp.fFlags' bool fSuccess = (SHFileOperation(&shFileOp) == FALSE); // to prevent warnings delete [] pszBufFrom; delete [] pszBufTo; return (fSuccess); } int main() { using namespace std; /* /!\ Voraussetzung: Die beiden Ordner "demo1" und "demo2" exisitieren unter "C:\" . */ const string strDesc = "CopyFolder returned: "; const string strDest[] = { "C:\\demo1", "C:\\copy1", "C:\\demo2", "C:\\copy2" }; cout << strDesc << boolalpha << CopyFolder(strDest[0].c_str(), strDest[1].c_str()) << "\n\tSource: " << strDest[0] << "\n\tDestination: " << strDest[1] << endl; cout << strDesc << boolalpha << CopyFolder(strDest[2].c_str(), strDest[3].c_str()) << "\n\tSource: " << strDest[2] << "\n\tDestination: " << strDest[3] << endl; cout << strDesc << boolalpha << CopyFolder(strDest[2].c_str(), strDest[1].c_str()) << "\n\tSource: " << strDest[2] << "\n\tDestination: " << strDest[1] << endl << endl; return (CtrlConsoleExit()); }Ausgabe (Konsolenfenster):
CopyFolder returned: true Source: C:\demo1 Destination: C:\copy1 CopyFolder returned: true Source: C:\demo2 Destination: C:\copy2 CopyFolder returned: true Source: C:\demo2 Destination: C:\copy1 Press any key to exit the application...
-
Team Suite
Das haste doch nur geschrieben um anzugeben

-
rolff schrieb:
Team Suite
Das haste doch nur geschrieben um anzugeben

Hm naja, also
, mir sind ehrlich gesagt schon viele Unterschiede aufgegefallen bzgl. Team Suite und Express Edition. Meine TS. kackt beispielsweise beim Debuggen andauernd ab...das ist mir bei der Express Edition nicht passiert. Und Compiler (bzw. IDE's) haben nunmal bekanntlich ihr Eigenleben
.
Aber wenn Du unbedingt willst/meinst
. Kann ich auch mit leben
- gut sogar
.