標題:
[發問]
請教大大如果目錄存在略過不要複製,如何改,謝謝
[打印本頁]
作者:
wufonna
時間:
2019-7-5 20:47
標題:
請教大大如果目錄存在略過不要複製,如何改,謝謝
本帖最後由 wufonna 於 2019-7-5 20:48 編輯
Option Explicit
Sub Ex()
Dim myPath As String, myFile As String, myname As String, n As Integer, strPath As String
myPath = ThisWorkbook.Path & "\" 'myPath = ThisWorkbook.Path 檔案路
myFile = "*.JPG"
myname = Dir(myPath & myFile)
Do While myname <> ""
n = n + 1
' Name myPath & myname As myPath & "FS" & Format(n, "00000") & ".jpg"
strPath = myPath & Replace(myname, ".jpg", "")
MkDir strPath
'錯誤
'If Len(Dir("c:\test\aaa", vbDirectory)) = 0 Then
' MkDir "c\test\aaa"
'End If
'
myname = Dir
Loop
MsgBox n
End Sub
複製代碼
作者:
wufonna
時間:
2019-7-6 20:14
本帖最後由 wufonna 於 2019-7-6 20:18 編輯
回復
1#
wufonna
改這樣可以,請教大大
Option Explicit
'批次改目錄
Sub Ex()
Dim myPath As String, myFile As String, myname As String, n As Integer, strPath As String
Dim S() As String, i As Integer, m As Variant
myPath = ThisWorkbook.Path & "\" 'myPath = ThisWorkbook.Path 檔案路
myFile = "*.JPG"
myname = Dir(myPath & myFile)
i = 0
Do While myname <> ""
n = n + 1
' Name myPath & myname As myPath & "FS" & Format(n, "00000") & ".jpg"
strPath = myPath & Replace(myname, ".jpg", "")
ReDim Preserve S(i)
S(i) = strPath
' Debug.Print S(i)
myname = Dir
i = i + 1
Loop
For Each m In S
' Debug.Print m
If Len(Dir(m, vbDirectory)) = 0 Then
MkDir m
End If
Next
' MsgBox n
End Sub
複製代碼
作者:
GBKEE
時間:
2020-4-26 17:18
回復
2#
wufonna
'**Dir 會傳回第一個與_路徑名稱_相符的檔案名稱。 若要取得任何與_路徑名稱_相符的其他檔案名稱,請再次呼叫不含引數的 Dir
你附檔的程式碼有盲點 If Dir(strPath, vbDirectory) = "" Then MkDir strPath
'**上式程式碼再次使用 Dir(strPath, vbDirectory) 導致 myname = Dir 傳回空自字串
你這程式碼可以的
也可用FileSystemObject 物件 試試
Option Explicit
Sub Ex()
Dim myPath As String, myFile As String, myname As String, n As Variant, strPath As String
Dim AR(), Folder As String
myPath = ThisWorkbook.Path & "\" 'myPath = ThisWorkbook.Path 檔案路徑
myFile = "*.JPG"
myname = Dir(myPath & myFile, vbDirectory) ' 'Dir 尋找檔案路徑\ *.JPG"的檔案
Do While myname <> ""
strPath = myPath & Replace(myname, ".jpg", "")
With New FileSystemObject '引用項目 Microsoft Scripting Runtime
On Error Resume Next '表示當一個執行階段錯誤產生時,程式控制立刻到發生錯誤陳述式接下去的陳述式,而繼續執行下去
If .GetFolder(strPath) Is Nothing Then MkDir strPath '**加入圖片名稱的資料夾
On Error GoTo 0 '停止現在程序裏任何已啟動的錯誤處理程式
End With
myname = Dir
Loop
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)