Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active July 26, 2018 23:50
Show Gist options
  • Save relyky/6df496152cfe987ab6fda0c513f12f78 to your computer and use it in GitHub Desktop.
Save relyky/6df496152cfe987ab6fda0c513f12f78 to your computer and use it in GitHub Desktop.
VB6, dir, directory, 檢查目錄, 建立目錄, 檢查檔案是否存在
'根目錄
Dim output_dir As String
output_dir = "\\127.0.0.1\MySharedFolder"
'目錄中段名稱
dir_middle_name = Format(Now, "yyyymmdd") ' 以當天日期為中間目錄
'完整目錄路徑
Dim output_dir2 As String
output_dir2 = output_dir & "\" & dir_middle_name
'若目的地目錄不存在,則建立它
If Dir(output_dir2, vbDirectory) = "" Then ' 若目的地目錄不存在
MkDir (output_dir2) ' 建立新的目錄
End If
Public Function MakeDirectory(ByVal base_dir As String, ByVal dir_name As String) As String
Rem 建立目錄,若已建立就不再建立
Rem 但一次只能建立1層目錄
Dim full_dir_path As String
full_dir_path = base_dir & "\" & dir_name
If Dir(full_dir_path, vbDirectory) = "" Then ' 輸出目錄不存在,自動建立新的
MkDir (full_dir_path)
End If
MakeDirectory = full_dir_path ' 回傳目標目錄
End Function
Public Function MakeDirectory2(ByVal base_dir As String, ByVal middle_dir As String, ByVal dir_name As String) As String
Rem 建立目錄,若已建立就不再建立
Rem 檢查並建立2層目錄:[ base_dir / middle_dir / dir_name ]
MakeDirectory2 = MakeDirectory(MakeDirectory(base_dir, middle_dir), dir_name)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment