Kopieren von dateien schlägt fehl



  • Hallo,

    ich versuche mich am Kopieren von einem Verzeichnis.
    Das Problem dabei ist, dass ich in dem Zielverzeichnis
    auf einen Ordner kopieren will, der erst noch angelegt werden muss.

    Mal klappt es, dann wieder nicht...
    Hier mal etwas Code:

    BROWSEINFO bi;
    	ZeroMemory(&bi, sizeof(BROWSEINFO));
    	TCHAR szDiplayName[MAX_PATH];
    	ZeroMemory(szDiplayName, MAX_PATH);
    	LPITEMIDLIST lpStartFolder = NULL;
    	SHGetSpecialFolderLocation(GetSafeHwnd(), 0, &lpStartFolder);
    
    	bi.hwndOwner = GetSafeHwnd();
    	bi.pszDisplayName = szDiplayName;
    	bi.lpszTitle = _T("Zielpfad");
    	bi.ulFlags = BIF_USENEWUI;
    	bi.pidlRoot = lpStartFolder;
    
    	LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
    	LPMALLOC pMalloc = NULL;
    	SHGetMalloc(&pMalloc);
    
    if(pIDList != NULL){
    		SHGetPathFromIDList(pIDList, zielpfadx.GetBuffer(MAX_PATH));
    		zielpfadx.ReleaseBuffer();
    		pMalloc->Free(pIDList);
    	}
    	pMalloc->Free(lpStartFolder);
    	pMalloc->Release();
    
    //Achtung: hier ist der Unterordner, der im Ziel noch nicht existiert.
    //Mal wird er beim Kopieren angelegt, und dann mal wieder nicht ?!
    
    	zielpfadx+="\\Docs\\";
    	zielpfadx+='\0';
    
    int yesno;
    CString beibehalten="Soll dieser Pfad als Quelle beibehalten werden?";
    
    	yesno = AfxMessageBox(beibehalten,MB_YESNO|MB_ICONQUESTION);
    	CString quelle;
    if(yesno == IDNO)
        {
    	MessageBox("Wählen Sie den Quellordner aus","Hinweis:",MB_ICONINFORMATION);
    
    	BROWSEINFO bix;
    	ZeroMemory(&bix, sizeof(BROWSEINFO));
    	TCHAR szDiplayNamex[MAX_PATH];
    	ZeroMemory(szDiplayNamex, MAX_PATH);
    	LPITEMIDLIST lpStartFolderx = NULL;
    	SHGetSpecialFolderLocation(GetSafeHwnd(), 0, &lpStartFolderx);
    
    	bix.hwndOwner = GetSafeHwnd();
    	bix.pszDisplayName = szDiplayNamex;
    	bix.lpszTitle = _T("Quellpfad wählen:");
    	bix.ulFlags = BIF_USENEWUI;
    	bix.pidlRoot = lpStartFolderx;
    
    	LPITEMIDLIST pIDListx = SHBrowseForFolder(&bix);
    	LPMALLOC pMallocx = NULL;
    	SHGetMalloc(&pMallocx);
    	if(pIDListx != NULL){
    		SHGetPathFromIDList(pIDListx, quelle.GetBuffer(MAX_PATH));
    		quelle.ReleaseBuffer();
    		pMalloc->Free(pIDListx);
    	}
    	pMallocx->Free(lpStartFolderx);
    	pMallocx->Release();
    
    	quelle+="\\*.*";
    	quelle+='\0';
    
    	}
    
    if(yesno==IDYES)
    	{
                     //quelle kommt eigentlich aus anderer variable (pfadcopy)
                     CString quelle="c:\\test\\"; //zum demonstrieren...
    		//quelle=pfadcopy;
    		quelle+="\\*.*";
    		quelle+='\0';
    
    	}
    
    if(quelle!="" && quelle!="\\*.*")
    	{
    
    SHFILEOPSTRUCT foCopy; 
    
    foCopy.hwnd                  = NULL;  
    foCopy.wFunc                 = FO_COPY; 
    foCopy.pFrom                 = quelle; 
    foCopy.pTo                   = zielpfadx; 
    foCopy.fFlags                = FOF_SIMPLEPROGRESS; 
    foCopy.fAnyOperationsAborted = FALSE;  
    foCopy.hNameMappings         = NULL; 
    foCopy.lpszProgressTitle     = "Kopiere..."; 
    
    if(SHFileOperation(&foCopy) == 0) { 
    
    	MessageBox("Kopiert!");
    
    } 
    else 
    { 
    	MessageBox("Fehler");
    
    }
    
    }
    else
    {
    	MessageBox("ungültiger Pfad");
    
    }
    

    Könnt ihr helfen? 😞



  • Ich habe inzwischen rausgefunden, dass FOF_NOCONFIRMMKDIR
    eigentlich benötigte Ordner anlegen sollte.
    Nur klappt das nicht wirklich...

    Jetzt habe ich mir mit

    ::CreateDirectory(ganzerpfadmitDOCS,NULL);
    

    beholfen.



  • Sorry, aber ich stehe vor dem nächsten Problem.
    Wie kann ich prüfen ob der Ordner nicht doch evtl. schon vorhanden ist?

    Ich dachte erst an

    CFileStatus status; 
    
    if( CFile::GetStatus( meinlangerpfadmitDOCS, status )) 
    {
    //existiert
    }
    else
    {
    
    //existiert nicht!
    }
    

    Mir wird aber immer ausgegeben, dass der Ordner nicht existiert 😕


Anmelden zum Antworten