使用VB

时间:2017-05-16 15:26:48

标签: vb.net

我有一个日志文件,每次创建时都会保留* .docx扩展名的完整路径。问题是我不知道如何从完整路径中分割文件的名称。在移动它之前,我可以选择使用CheckedListBox创建的路径并将其移动到目标文件夹。

例如,在我的日志文件I商店(文件已创建:C:\Users\AsrahLim\Desktop\New Microsoft Word Document.docx)中,我只需要文件的名称"新的Microsoft Word Document.docx"并将其移至新文件夹。

这是我的目标文件夹:C:\Users\AsrahLim\Google Drive。以下是我的代码。

Imports System.IO

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckedListBox1.Items.Add("Select/UnSelect All")
        CheckedListBox1.CheckOnClick = True

        Dim FILE_NAME As String = "C:\Users\AsrahLim\Desktop\LogFile.txt"

        If System.IO.File.Exists(FILE_NAME) Then
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            Do While objReader.Peek() <> -1
                CheckedListBox1.Items.Add(objReader.ReadLine())
                btnSave.Enabled = True
            Loop
        Else
            MessageBox.Show("File Does Not Exist")
            Close()
        End If
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        If CheckedListBox1.CheckedItems.Count <> 0 Then
            For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
                Dim SourcePath As String = CheckedListBox1.SelectedItem
                Dim MoveLocation As String = "C:\Users\AsrahLim\Google Drive"
                SourcePath = SourcePath.Substring(SourcePath.LastIndexOf("- ") + 1)

                If File.Exists(SourcePath) = True Then
                    File.Move(SourcePath, MoveLocation)
                    MsgBox("File Moved")
                Else
                    MsgBox("File Not move")
                End If
            Next
        End If
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        Close()
    End Sub
End Class

2 个答案:

答案 0 :(得分:2)

不要尝试为路径操作实现自己的逻辑。请改用System.IO中的共享Path class

Dim filename As String = Path.GetFileName(SourcePath)

然后您可以使用

构建新路径名
Dim destinationPath As String = Path.Combine(MoveLocation, filename)

另外,测试文件是否也存在于目标位置,如果存在则将其删除。

If File.Exists(SourcePath) Then
    Dim filename As String = Path.GetFileName(SourcePath)
    Dim destinationPath As String = Path.Combine(MoveLocation, filename)
    If File.Exists(destinationPath) Then
        File.Delete(destinationPath)
    End If
    File.Move(SourcePath, destinationPath)
    MsgBox("File Moved")
Else
    MsgBox("File Not move")
End If

附注:我不喜欢像If File.Exists(SourcePath) = True Then这样的陈述。通常人们认为If语句需要进行比较。这不是真的。它需要的只是一个布尔表达式,即返回TrueFalse的表达式。 File.Exists(SourcePath)是一个完全符合这一要求的表达式。附加= True不会改变任何内容并且是多余的,因为如果File.Exists(SourcePath)返回True,则True = TrueTrue,如果File.Exists(SourcePath)返回{ {1}}然后FalseFalse = True。对于数字,False= True的中性操作。您没有说* 1,只是说Foo(1 * x)

答案 1 :(得分:0)

在您的日志中非常简单,您可以使用分隔符存储,例如:

New File Created*C:\test.docx

星号是文件名中的禁止字符,因此您可以确定它不会在路径中。在此之后你可以做到

Dim data() As String
data = Split(File.ReadAllText(LogFile.txt),  StringSplitOptions.RemoveEmptyEntries)
File.Move(data(1), Path.Combine(MoveLocation , Path.GetFileName(data(1)))

理想情况下,您不应该在计算机上的任何位置存储文件并使用不同的文件夹。

相关问题