2016-10-29 21 views
0

我從網絡的某處獲得了此代碼,並且它對我來說工作正常。但是,我的經驗還不足以在主文件夾的所有子文件夾中運行(「C:\ Folder \」)。我希望有任何建議。在所有子文件夾中運行代碼

Sub check() 
Dim strFolder As String 
Dim strFile As String 

    strFolder = "C:\Folder\" 
    strFile = Dir(strFolder & "*.*") 
    Do While Len(strFile) > 0 
    If InStr(strFile, "xxx") > 0 Then 
     Name strFolder & strFile As strFolder & Replace(strFile, "xxx", "yyy") 
    End If 
    strFile = Dir() 
    Loop 
End Sub 
+0

「我從網上的某個地方得到了這段代碼」 - 如果它是惡意的? –

+0

這行代碼是幹什麼的:'將strFolder&strFile命名爲strFolder&Replace(strFile,「xxx」,「yyy」)'?這看起來不像有效的VB.NET語法。 –

回答

0

你好得多使用System.IO命名空間(你需要添加Imports語句吧),因爲有一個Directories.GetFiles方法允許的選項中搜索AllDirectories您指定的文件夾

Imports System.IO;

Sub Check() 
Dim strFolder As String = "C:\Folder\" 
Dim strFiles() As String = Directory.GetFiles(strFolder, "*.*", SearchOption.AllDirectories) 

    For Each strFile As String In strFiles 
     If strFile.Contains("xxx") Then 
      File.Move(strFolder & strFile, strFolder & strFile.Replace("xxx", "yyy")) 
     End If 
    Next 
End Sub 
相關問題