PDA

Vollständige Version anzeigen : Ein Ordner kopieren .......


Verena Grieshaber
04.04.2001, 12:15
Hallo Ihr!

Ich würde gerne einen Ordner kopieren. Und zwar ziehe ich den Ziel und Quellpath aus einem DirListBox.Irgendwie bin ich in der hilfe auf FileSystemObject gestoßen, leider wird das aber nicht erkannt. Ich müßte es wohl erst implizieren. Kann mir jemand helfen, ich glaub ich stell mich im moment einfach nur blöd an.
Gruß
Verena

Günther Kramer
04.04.2001, 13:26
Hallo Verena,

vielleicht kommst du damit weiter:

<div><link href="http://www.ms-office-forum.net/forum/externals/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Option</span> <span class="TOKEN">Explicit</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Const</span> MAXDWORD <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = &amp;HFFFF
<span class="TOKEN">Public</span> <span class="TOKEN">Const</span> MAX_PATH <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 260
<span class="TOKEN">Public</span> <span class="TOKEN">Const</span> INVALID_HANDLE_VALUE <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = -1
<span class="TOKEN">Public</span> <span class="TOKEN">Const</span> FILE_ATTRIBUTE_DIRECTORY <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = &amp;H10
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Type</span> FILETIME
dwLowDateTime <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwHighDateTime <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Type</span> WIN32_FIND_DATA
dwFileAttributes <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
ftCreationTime <span class="TOKEN">As</span> FILETIME
ftLastAccessTime <span class="TOKEN">As</span> FILETIME
ftLastWriteTime <span class="TOKEN">As</span> FILETIME
nFileSizeHigh <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
nFileSizeLow <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwReserved0 <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwReserved1 <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
cFileName <span class="TOKEN">As</span> <span class="TOKEN">String</span> * MAX_PATH
cAlternate <span class="TOKEN">As</span> <span class="TOKEN">String</span> * 14
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Type</span> SECURITY_ATTRIBUTES
nLength <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
lpSecurityDescriptor <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
bInheritHandle <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> FindFirstFile <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
<span class="TOKEN">Alias</span> &quot;FindFirstFileA&quot; _
(<span class="TOKEN">ByVal</span> lpFileName <span class="TOKEN">As</span> String, _
lpFindFileData <span class="TOKEN">As</span> WIN32_FIND_DATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> FindNextFile <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
<span class="TOKEN">Alias</span> &quot;FindNextFileA&quot; _
(<span class="TOKEN">ByVal</span> hFindFile <span class="TOKEN">As</span> Long, _
lpFindFileData <span class="TOKEN">As</span> WIN32_FIND_DATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> FindClose <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
(<span class="TOKEN">ByVal</span> hFindFile <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> CompareFileTime <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
(lpFileTime1 <span class="TOKEN">As</span> FILETIME, _
lpFileTime2 <span class="TOKEN">As</span> FILETIME) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> CopyFile <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
<span class="TOKEN">Alias</span> &quot;CopyFileA&quot; _
(<span class="TOKEN">ByVal</span> lpExistingFileName <span class="TOKEN">As</span> String, _
<span class="TOKEN">ByVal</span> lpNewFileName <span class="TOKEN">As</span> String, _
<span class="TOKEN">ByVal</span> bFailIfExists <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Public</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> CreateDirectory <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
<span class="TOKEN">Alias</span> &quot;CreateDirectoryA&quot; _
(<span class="TOKEN">ByVal</span> lpPathName <span class="TOKEN">As</span> String, _
lpSecurityAttributes <span class="TOKEN">As</span> SECURITY_ATTRIBUTES) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="REM">'--end block--'</span>
&nbsp;
&nbsp;
<span class="REM"> 'Form Code</span>
&nbsp;
<span class="REM">'Create a new project, and add to the form: two text boxes (Text1 &amp; Text2),</span>
<span class="REM">' two lists (List1 &amp; List2), and a command button, (Command1). Labels are optional.</span>
<span class="REM">' Text1 and List1 correspond to source file settings, while Text2 and List2</span>
<span class="REM">' handle the target. Once constructed, add the following code:</span>
&nbsp;
&nbsp;
<span class="TOKEN">Private Sub</span> Command1_Click()
List1.AddItem &quot;--- new backup ---&quot;
List2.AddItem &quot;--- new backup ---&quot;
BackupBegin
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span><hr>&nbsp;
&nbsp;
<span class="TOKEN">Private Sub</span> BackupBegin()
<span class="TOKEN">Dim</span> WFDSource <span class="TOKEN">As</span> WIN32_FIND_DATA
<span class="TOKEN">Dim</span> WFDTarget <span class="TOKEN">As</span> WIN32_FIND_DATA
<span class="TOKEN">Dim</span> sSourceFolder <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sTargetFolder <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> hFileSource <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> hFileTarget <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="REM">'Pre-backup check (this sub)</span>
<span class="REM">'------------------------------------------------</span>
<span class="REM">'1. Important! Assure both source and target</span>
<span class="REM">' paths are fully qualified</span>
<span class="REM">'2. Check for existence of source folder</span>
<span class="REM">' by obtaining a handle to the source</span>
<span class="REM">'3. If source folder not available, perform</span>
<span class="REM">' action (ie abort, map a drive etc)</span>
<span class="REM">'4. Check for existence of target folder</span>
<span class="REM">'5. If target folder not available, perform</span>
<span class="REM">' action (ie abort, create folder(s) etc)</span>
&nbsp;
<span class="REM">'Backup steps (BackupSourceFolder function)</span>
<span class="REM">'------------------------------------------------</span>
<span class="REM">'1. Begin enumerating source folder contents</span>
<span class="REM">'2. If source item is a file, attempt to</span>
<span class="REM">' locate the same file in the target folder</span>
<span class="REM">'3. If file is missing, copy it</span>
<span class="REM">'4. If file is present, obtain the file details</span>
<span class="REM">' of the source and target files</span>
<span class="REM">'5. If they are the same, move to the next file in the source folder</span>
<span class="REM">'6. If they are different, copy source file into target folder</span>
<span class="REM">'7. Repeat for all files in source.</span>
&nbsp;
<span class="REM"> 'Assure both source and target</span>
<span class="REM"> 'paths are fully qualified</span>
sSourceFolder = QualifyPath(Text1.Text)
sTargetFolder = QualifyPath(Text2.Text)
&nbsp;
<span class="REM"> 'Check for existence of source folder</span>
<span class="REM"> 'by obtaining a handle to the source</span>
hFileSource = FileGetFileHandle(sSourceFolder, WFDSource)
&nbsp;
<span class="REM"> 'If source folder not available, perform</span>
<span class="REM"> 'action (ie abort, map a drive etc)</span>
<span class="TOKEN">If</span> hFileSource = INVALID_HANDLE_VALUE <span class="TOKEN">Then</span>
MsgBox &quot;Backup source folder &quot; &amp; sSourceFolder &amp; &quot; not found.&quot;
<span class="TOKEN">Exit Sub</span>
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
&nbsp;
<span class="REM"> 'Check for existence of target folder</span>
<span class="REM"> 'by obtaining a handle to the target</span>
hFileTarget = FileGetFileHandle(sTargetFolder, WFDTarget)
&nbsp;
<span class="TOKEN">If</span> hFileTarget = INVALID_HANDLE_VALUE <span class="TOKEN">Then</span>
<span class="REM"> 'If target folder not available, perform</span>
<span class="REM"> 'action (ie abort, create folder(s) etc).</span>
<span class="REM"> 'Here, we'll create the folder(s)</span>
MsgBox &quot;Backup target folder &quot; &amp; sTargetFolder &amp; &quot; not found. Creating the target.&quot;
<span class="REM"> 'remember ... although the CreateNestedFolders call</span>
<span class="REM"> 'returns the *value* of the handle used in creating the</span>
<span class="REM"> 'folders, the handle was actually closed in the function.</span>
<span class="REM"> 'The value is returned for comparison in the If..Then</span>
<span class="REM"> 'below is not and is NOT valid, so don't attempt to</span>
<span class="REM"> 'use it to access files!</span>
hFileTarget = CreateNestedFolders(sTargetFolder)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
&nbsp;
<span class="REM"> 'If source and target handles are valid</span>
<span class="TOKEN">If</span> (hFileSource &lt;&gt; INVALID_HANDLE_VALUE) <span class="TOKEN">And</span> _
(hFileTarget &lt;&gt; INVALID_HANDLE_VALUE) <span class="TOKEN">Then</span>
<span class="REM"> 'perform the backup</span>
<span class="TOKEN">Call</span> BackupSourceFolder(hFileSource, sSourceFolder, WFDSource, sTargetFolder)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
&nbsp;
<span class="REM"> 'clean up by closing the source handle. The target</span>
<span class="REM"> 'handle is closed in the BackupSourceFolder sub.</span>
<span class="TOKEN">Call</span> FindClose(hFileSource)
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> FileCompareFileDates(WFDSource <span class="TOKEN">As</span> WIN32_FIND_DATA, _
WFDTarget <span class="TOKEN">As</span> WIN32_FIND_DATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> CTSource <span class="TOKEN">As</span> FILETIME
<span class="TOKEN">Dim</span> CTTarget <span class="TOKEN">As</span> FILETIME
&nbsp;
<span class="REM"> 'assign the source and target file write</span>
<span class="REM"> 'times to a FILETIME structure, and compare.</span>
CTSource.dwHighDateTime = WFDSource.ftLastWriteTime.dwHighDateTime
CTSource.dwLowDateTime = WFDSource.ftLastWriteTime.dwLowDateTime
CTTarget.dwHighDateTime = WFDTarget.ftLastWriteTime.dwHighDateTime
CTTarget.dwLowDateTime = WFDTarget.ftLastWriteTime.dwLowDateTime
FileCompareFileDates = CompareFileTime(CTSource, CTTarget)
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> UnQualifyPath(<span class="TOKEN">ByVal</span> sFolder <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM"> 'remove any trailing slash</span>
sFolder = Trim$(sFolder)
<span class="TOKEN">If</span> Right$(sFolder, 1) = &quot;\&quot; <span class="TOKEN">Then</span>
UnQualifyPath = Left$(sFolder, Len(sFolder) - 1)
Else: UnQualifyPath = sFolder
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> BackupSourceFolder(<span class="TOKEN">ByVal</span> hFileSource <span class="TOKEN">As</span> Long, _
<span class="TOKEN">ByVal</span> sSourceFolder <span class="TOKEN">As</span> String, _
WFDSource <span class="TOKEN">As</span> WIN32_FIND_DATA, _
<span class="TOKEN">ByVal</span> sTargetFolder <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="REM"> 'common local working variables</span>
<span class="TOKEN">Dim</span> sPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sRootSource <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sTmp <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sTargetMsg <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sSourceMsg <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> diff <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="REM"> 'variables used for the source files and folders</span>
<span class="TOKEN">Dim</span> dwSourceFileSize <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="REM"> 'variables used for the target files and folders</span>
<span class="TOKEN">Dim</span> WFDTarget <span class="TOKEN">As</span> WIN32_FIND_DATA
<span class="TOKEN">Dim</span> hTargetFile <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> dwTargetFileSize <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
sRootSource = QualifyPath(sSourceFolder)
sPath = sRootSource &amp; &quot;*.*&quot;
&nbsp;
<span class="REM"> 'last check!</span>
<span class="TOKEN">If</span> hFileSource &lt;&gt; INVALID_HANDLE_VALUE <span class="TOKEN">Then</span>
<span class="TOKEN">Do</span>
<span class="REM"> 'remove trailing nulls from the first retrieved object</span>
sTmp = TrimNull(WFDSource.cFileName)
<span class="REM"> 'if the object is not a folder..</span>
<span class="TOKEN">If</span> (WFDSource.dwFileAttributes <span class="TOKEN">And</span> FILE_ATTRIBUTE_DIRECTORY) &lt;&gt; _
FILE_ATTRIBUTE_DIRECTORY <span class="TOKEN">Then</span>
<span class="REM"> 'check for the corresponding file</span>
<span class="REM"> 'in the target folder by using the API</span>
<span class="REM"> 'to locate that specific file</span>
hTargetFile = FindFirstFile(sTargetFolder &amp; sTmp, WFDTarget)
<span class="REM"> 'if the file is located in the target folder..</span>
<span class="TOKEN">If</span> hTargetFile &lt;&gt; INVALID_HANDLE_VALUE <span class="TOKEN">Then</span>
<span class="REM"> 'get the file size for the source and target files</span>
dwSourceFileSize = FileGetFileSize(WFDSource)
dwTargetFileSize = FileGetFileSize(WFDTarget)
<span class="REM"> 'compare the dates.</span>
<span class="REM"> 'If diff = 0 source and target are the same</span>
<span class="REM"> 'If diff = 1 source is newer than target</span>
<span class="REM"> 'If diff = -1 source is older than target</span>
diff = FileCompareFileDates(WFDSource, WFDTarget)
<span class="REM"> 'if the dates, attributes and file times</span>
<span class="REM"> 'are the same...</span>
<span class="TOKEN">If</span> (dwSourceFileSize = dwTargetFileSize) <span class="TOKEN">And</span> _
WFDSource.dwFileAttributes = WFDTarget.dwFileAttributes <span class="TOKEN">And</span> _
diff = 0 <span class="TOKEN">Then</span>
<span class="REM"> '...the files are the same, so take</span>
<span class="REM"> 'appropriate action (here, this is</span>
<span class="REM"> 'to simply list the files for info)</span>
List1.AddItem sTmp &amp; vbTab &amp; _
dwSourceFileSize &amp; vbTab &amp; _
WFDSource.dwFileAttributes &amp; vbTab &amp; _
&quot;files the same&quot;
List2.AddItem sTmp &amp; vbTab &amp; _
dwTargetFileSize &amp; vbTab &amp; _
WFDTarget.dwFileAttributes &amp; vbTab &amp; _
&quot;No&quot;
Else:
<span class="REM"> 'files are not the same</span>
<span class="TOKEN">If</span> diff = 1 <span class="TOKEN">Then</span>
<span class="REM"> 'perform the preferred copy method ONLY if</span>
<span class="REM"> 'diff indicated that the source was newer!</span>
<span class="TOKEN">Call</span> CopyFile(sSourceFolder &amp; sTmp, sTargetFolder &amp; sTmp, <span class="TOKEN">False</span>)
sTargetMsg = &quot;Yes&quot;
sSourceMsg = &quot;source newer&quot;
<span class="TOKEN">ElseIf</span> diff = -1 <span class="TOKEN">Then</span>
<span class="REM"> 'source is older</span>
sTargetMsg = &quot;No&quot;
sSourceMsg = &quot;source older&quot;
<span class="TOKEN">ElseIf</span> diff = 0 <span class="TOKEN">Then</span>
<span class="REM"> 'the dates are the same but the file attributes</span>
<span class="REM"> 'are different. Since the date didn't change,</span>
<span class="REM"> 'replacing the file is a judgement call for</span>
<span class="REM"> 'the developer.</span>
sTargetMsg = &quot;No&quot;
sSourceMsg = &quot;attr different&quot;
<span class="REM"> 'Call CopyFile(sSourceFolder &amp; sTmp, sTargetFolder &amp; sTmp, False)</span>
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="REM"> 'debug only: add the files to the</span>
<span class="REM"> 'lists with the appropriate message</span>
List1.AddItem sTmp &amp; vbTab &amp; _
dwSourceFileSize &amp; vbTab &amp; _
WFDSource.dwFileAttributes &amp; vbTab &amp; _
sSourceMsg
List2.AddItem sTmp &amp; vbTab &amp; _
dwTargetFileSize &amp; vbTab &amp; _
WFDTarget.dwFileAttributes &amp; vbTab &amp; _
sTargetMsg
<span class="TOKEN">End</span> <span class="TOKEN">If</span> <span class="REM">'If dwSourceFileSize</span>
<span class="REM"> 'since the target file was found,</span>
<span class="REM"> 'close the handle</span>
<span class="TOKEN">Call</span> FindClose(hTargetFile)
Else:
<span class="REM"> 'the target file was not found so</span>
<span class="REM"> 'copy the file to the target directory</span>
<span class="TOKEN">Call</span> CopyFile(sSourceFolder &amp; sTmp, sTargetFolder &amp; sTmp, <span class="TOKEN">False</span>)
<span class="REM"> 'info only: add the files to the lists</span>
List1.AddItem sTmp &amp; vbTab &amp; _
&quot;target file did not exist&quot;
List2.AddItem sTmp &amp; vbTab &amp; _
dwTargetFileSize &amp; vbTab &amp; _
WFDTarget.dwFileAttributes &amp; vbTab &amp; _
&quot;Yes&quot;
<span class="TOKEN">End</span> <span class="TOKEN">If</span> <span class="REM">'If hTargetFile</span>
<span class="TOKEN">End</span> <span class="TOKEN">If</span> <span class="REM">'If WFDSource.dwFileAttributes</span>
<span class="REM"> 'clear the local variables</span>
dwSourceFileSize = 0
dwTargetFileSize = 0
<span class="TOKEN">Loop</span> <span class="TOKEN">While</span> FindNextFile(hFileSource, WFDSource)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> FileGetFileSize(WFD <span class="TOKEN">As</span> WIN32_FIND_DATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
FileGetFileSize = (WFD.nFileSizeHigh * (MAXDWORD + 1)) + WFD.nFileSizeLow
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> FileGetFileHandle(sPathToFiles <span class="TOKEN">As</span> String, WFD <span class="TOKEN">As</span> WIN32_FIND_DATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> sPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sRoot <span class="TOKEN">As</span> <span class="TOKEN">String</span>
&nbsp;
sRoot = QualifyPath(sPathToFiles)
sPath = sRoot &amp; &quot;*.*&quot;
<span class="REM"> 'obtain handle to the first match</span>
<span class="REM"> 'in the target folder</span>
FileGetFileHandle = FindFirstFile(sPath, WFD)
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> QualifyPath(sPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM"> 'assures that a passed path ends in a slash</span>
<span class="TOKEN">If</span> Right$(sPath, 1) &lt;&gt; &quot;\&quot; <span class="TOKEN">Then</span>
QualifyPath = sPath &amp; &quot;\&quot;
Else: QualifyPath = sPath
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Public Function</span> TrimNull(startstr <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM"> 'returns the string up to the first</span>
<span class="REM"> 'null, if present, or the passed string</span>
<span class="TOKEN">Dim</span> pos <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
pos = InStr(startstr, Chr$(0))
<span class="TOKEN">If</span> pos <span class="TOKEN">Then</span>
TrimNull = Left$(startstr, pos - 1)
<span class="TOKEN">Exit Function</span>
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
TrimNull = startstr
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> CreateNestedFolders(<span class="TOKEN">ByVal</span> sCompletePath <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="REM"> 'creates nested directories on the drive</span>
<span class="REM"> 'included in the path by parsing the passed</span>
<span class="REM"> 'directory string and looping through each</span>
<span class="REM"> 'folder listed to create the final path.</span>
<span class="TOKEN">Dim</span> SA <span class="TOKEN">As</span> SECURITY_ATTRIBUTES
<span class="TOKEN">Dim</span> WFD <span class="TOKEN">As</span> WIN32_FIND_DATA
<span class="TOKEN">Dim</span> drivePart <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> newDirectory <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> item <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> pos <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> cnt <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> hPath <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="REM"> 'Procedures in this function</span>
<span class="REM"> '--------------------------------------------------</span>
<span class="REM"> '1. Make sure the path is fully qualified: required!</span>
<span class="REM"> '2. Check for a drive in the string; if</span>
<span class="REM"> ' so get it otherwise assume current drive</span>
<span class="REM"> '3. Enter loop ...</span>
<span class="REM"> '4. Extract each folder that makes up the total path</span>
<span class="REM"> '5. If the first time through, create the</span>
<span class="REM"> ' folder using the drive spec, otherwise</span>
<span class="REM"> ' append successive levels to the nested folders</span>
<span class="REM"> '7. Call CreateDirectory until the total path created</span>
<span class="REM"> '8. As a sign of success, call FileGetFileHandle</span>
<span class="REM"> ' passing the directory that should now exist.</span>
<span class="REM"> ' If the returned value is not INVALID_HANDLE_VALUE,</span>
<span class="REM"> ' the CreateDirectory call was successful.</span>
<span class="REM"> '9. Close the handle on exiting.</span>
sCompletePath = QualifyPath(sCompletePath)
pos = InStr(sCompletePath, &quot;:\&quot;)
<span class="TOKEN">If</span> pos <span class="TOKEN">Then</span>
drivePart = StripDelimitedItem(sCompletePath, &quot;\&quot;)
Else: drivePart = StripDelimitedItem(CurDir(), &quot;\&quot;)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">Do</span>
cnt = cnt + 1
item = StripDelimitedItem(sCompletePath, &quot;\&quot;)
<span class="TOKEN">If</span> cnt = 1 <span class="TOKEN">Then</span>
newDirectory = drivePart &amp; item
Else: newDirectory = newDirectory &amp; item
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
SA.nLength = LenB(SA)
<span class="TOKEN">Call</span> CreateDirectory(newDirectory, SA)
<span class="TOKEN">Loop</span> <span class="TOKEN">Until</span> sCompletePath = &quot;&quot;
hPath = FileGetFileHandle(sCompletePath, WFD)
CreateNestedFolders = hPath
<span class="TOKEN">Call</span> FindClose(hPath)
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr>&nbsp;
<span class="TOKEN">Private Function</span> StripDelimitedItem(startStrg <span class="TOKEN">As</span> String, delimiter <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM"> 'take a string separated by delimiter,</span>
<span class="REM"> 'split off 1 item, and shorten the string</span>
<span class="REM"> 'so the next item is ready for removal.</span>
<span class="TOKEN">Dim</span> pos <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> item <span class="TOKEN">As</span> <span class="TOKEN">String</span>
&nbsp;
pos = InStr(1, startStrg, delimiter)
<span class="TOKEN">If</span> pos <span class="TOKEN">Then</span>
StripDelimitedItem = Mid$(startStrg, 1, pos)
startStrg = Mid$(startStrg, pos + 1, Len(startStrg))
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">Function</span><hr></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

<font size="1" face="Century Gothic">Moderatorenanmerkung: Überarbeitung, durch den Wechsel der Forensoftware zum 01.01.2003 verursacht: in diesem Beitrag Link(s) korrigiert.
jinx – 09.07.2003</font>

Stefan Kulpa
04.04.2001, 17:50
Hallo,

neben der sehr ausführlichen Antwort von Günther vielleicht noch ein Hinweis zum FSO.

Folgendes Beispiel funktioniert einwandfrei:

Dim objFso As New FileSystemObject
objFso.CopyFolder "I:\WINNT\Temp", "I:\Temp", True

Dabei wird der Inhalt von "I:\WINNT\Temp" in den Ordner "I:\Temp" kopiert - der Ordner selbst jedoch nicht. Allerdings werden Unterordner in "I:\WINNT\Temp" ebenfalls beim Kopieren berücksichtigt - wenn es das ist, was du wolltest ...

Was das Objekt angeht - du benötigst eine Referenz auf die "Microsoft Scripting Runtime" bzw. auf die Datei SCRRUN.DLL. Alternativ geht's auch mittels CreateObject(...).

Gruß