2012-07-06 36 views
0

我現在有這些目錄:如何複製包含與目錄相同字符串的文件?

C:\testfolder\100 
C:\testfolder\101 
C:\testfolder\102 

和我在同一個目錄中的這些文件:

C:\testfolder\file-100.txt 
C:\testfolder\file-101.txt 
C:\testfolder\file-102.txt 

我試圖在VB做的是移動的文本文件file-100.txt100目錄。對於文本文件file-101.txt也是如此,請將其移至相關文件夾101

我的問題是如何編寫一個循環,以便我的程序匹配我的文本文件名的一部分字符串並將其移動到匹配的文件夾名稱?一次移動一個文件不會有效,因爲我有數百個目錄和文件可以應用它。

編輯:

我對VB有些熟悉。我遇到了這個邏輯部分的問題,在這個部分中,我想不出一種編寫循環的方式,以便它可以爲我傳輸文件。

+0

此任務的一部分,你有麻煩?獲取給定文件路徑的目標文件夾路徑?移動文件?構建循環?獲取文件列表以移動?請編輯您的問題以更具體。 – 2012-07-06 14:47:39

+0

所以你只需要弄清楚如何從給定的文件路徑獲取目標文件夾路徑?如果是這樣,文件名是否總是以同樣的方式格式化? – 2012-07-06 14:53:57

+1

由於您將文件從父目錄移動到子目錄,因此「SearchAllSubDirectories」選項無意義。該選項會找到您移動的文件並嘗試再次移動它們。 – LarsTech 2012-07-06 15:13:13

回答

0

沒有錯誤檢查,這將是一個簡單的例程來移動這些文件。它是基於你的文件名是一致的:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
    Dim homePath As String = "c:\testfolder" 

    Dim files() As String = Directory.GetFiles(homePath, "*.txt") 
    For Each f As String In files 
    Dim fileName As String = Path.GetFileName(f) 

    Dim destPath As String = Path.GetFileNameWithoutExtension(fileName) 
    destPath = destPath.Split("-")(1) 
    destPath = Path.Combine(homePath, destPath) 

    Dim destFile As String = Path.Combine(destPath, fileName) 

    File.Move(f, destFile) 
    Next 
End Sub 

這只是幫倒忙目錄的文本文件列表中,解析文件名得到公正的數值(100,101等),然後重建新的道路。它假定目錄也存在。

0

您可以使用正則表達式來找到匹配的模式

Dim dir As String = "C:\testfolder\" 
    Dim fileList() As String = {"C:\testfolder\file-100.txt", _ 
           "C:\testfolder\file-101.txt", _ 
           "C:\testfolder\file-102.txt"} 

    Dim pattern As New Regex(Replace(dir, "\", "\\") & "file-([0-9]+)[.]txt") 

    For Each value As String In fileList 
     Dim match As Match = pattern.Match(value) 
     If match.Success Then 
      MsgBox("move from " & dir & " to " & dir & match.Groups(1).Value) 
     End If 
    Next 

請確保您有進口RegularExpressions。

Imports System.Text.RegularExpressions 
0
Private Sub organizeFiles(ByVal folderPath As String) 
    For Each filePath As String In Directory.GetFiles(folderPath, "*.txt") 
     Dim destinationFilePath As String = getDestinationFilePath(filePath) 
     If destinationFilePath IsNot Nothing Then 
      File.Move(filePath, destinationFilePath) 
     End If 
    Next 
End Sub 

Private Function getDestinationFilePath(ByVal filePath As String) As String 
    Const fileNamePrefix As String = "file-" 
    Dim fileName As String = Path.GetFileName(filePath) 
    Dim fileNameWithoutExtension As String = Path.GetFileNameWithoutExtension(filePath) 
    If Not fileNameWithoutExtension.StartsWith(fileNamePrefix) Then 
     Return Nothing 
    End If 
    Dim folderName As String = fileNameWithoutExtension.Substring(fileNamePrefix.Length) 
    Dim fileFolderPath As String = Path.GetDirectoryName(filePath) 
    Dim destinationFolderPath As String = Path.Combine(fileFolderPath, folderName) 
    Dim destinationFilePath As String = Path.Combine(destinationFolderPath, fileName) 
    Return destinationFilePath 
End Function 
相關問題