Funktion um Dateien zu kopieren, und das Zielverzeichnis automatisch zu erzeugen falls es nicht existiert
-
Hallo,
ich suche eine Funktion, der ich zwei Dateien übergebe (inkl. absolutem Pfad). So ähnlich wie
BOOL WINAPI CopyFile( __in LPCTSTR lpExistingFileName, __in LPCTSTR lpNewFileName, __in BOOL bFailIfExists );Nur hätte ich gerne, dass das Zielverzeichnis, falls es nicht existiert, automatisch erzeugt wird.
Irgendwelche Vorschläge?
-
//**************************************************************************** //* //* CreatePath //* //**************************************************************************** // Erzeugt einen Pfad rekuriv in dem alle Verzeichnisser erstellt werden. // pPath : Ist der name des Pfades. // Am Ende des Pfades kann auch ein '\' oder '/' stehen. // Ergibt 0 bei einem Fehler // 1 wenn der Pfad erzeugt wurde // 2 wenn der Pfad bereits existiert int _cdecl CreatePath(const char *pPath) { int i,iLen,iDrive,iPos,iNew,iStart=0; char cSign,cBuffer[MAX_PATH+16]=""; DWORD dwAttr; if(pPath[0] && pPath[1]==':') // Laufwerk prüfen { iDrive=pPath[0]; if(iDrive>='a' && iDrive<='z')iDrive-=32; if(iDrive< 'A' || iDrive> 'Z')return 0; // Falsche Laufwerksnummer iDrive-='A'; if(!(GetLogicalDrives()&(1<<iDrive))) // Existiert Laufwerk { return 0; } } iLen=GetFullPathNameA(pPath,MAX_PATH,cBuffer,0); if((iLen>=3)&&(cBuffer[iLen-2]==':')&&(cBuffer[iLen-1]=='/' || cBuffer[iLen-1]=='\\')) { return 2; // Pfad ist Laufwerk } if(iLen) // '/' am Ende entfernen if(cBuffer[iLen-1]=='/' || cBuffer[iLen-1]=='\\') { iLen--; cBuffer[iLen]=0; } if(iLen>=2) // Ist Pfad Netzlaufwerk '\\...' if(cBuffer[0]=='\\' && cBuffer[1]=='\\') { for(i=2;i<iLen;i++) { if(cBuffer[i]=='/' )break; if(cBuffer[i]=='\\')break; } if(i>=iLen)return 2; iStart=i+1; } else{ iStart=0; } dwAttr=GetFileAttributesA(cBuffer); // Existiert Pfad bereits ? if(dwAttr!=0xFFFFFFFF) { return (dwAttr&FILE_ATTRIBUTE_DIRECTORY)? 2:0; } iNew = 0; iPos = strcspn(cBuffer+iStart,"\\/")+iStart; while(iPos<iLen) { if(cBuffer[iPos])iPos++; i=strcspn(cBuffer+iPos,"\\/"); if(!i)return 0; iPos+=i; cSign=cBuffer[iPos]; cBuffer[iPos]=0; if(!iNew) { dwAttr=GetFileAttributesA(cBuffer); // Existiert Pfad ? if(dwAttr!=0xFFFFFFFF) { cBuffer[iPos]=cSign; continue; } iNew=1; } if(!CreateDirectoryA(cBuffer,NULL)) // Verzeichnis erzeugen ? { return 0; } cBuffer[iPos]=cSign; }; return 1; }
-
Super, vielen Dank!
-
Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.
Im Zweifelsfall bitte auch folgende Hinweise beachten:
C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?Dieses Posting wurde automatisch erzeugt.
-
Alternativ:
MakeSureDirectoryPathExists: http://msdn.microsoft.com/en-us/library/ms680352(VS.85).aspx
SHCreateDirectory: http://msdn.microsoft.com/en-us/library/bb762130(VS.85).aspx
SHPathPrepareForWrite: http://msdn.microsoft.com/en-us/library/bb762237(VS.85).aspx
-
Das einfachste ist aber wohl
SHFileOperation
...http://msdn.microsoft.com/en-us/library/bb762164
http://msdn.microsoft.com/en-us/library/bb759795Copy and Move operations can specify destination directories that do not exist. In those cases, the system attempts to create them and normally displays a dialog box to ask the user if they want to create the new directory. To suppress this dialog box and have the directories created silently, set the FOF_NOCONFIRMMKDIR flag in fFlags.