Asrah
Asrah

Reputation: 21

Moving files into new folder using VB

I have one Log file that keep a full path for *.docx extension every time it's created. The problem is I don't know how to split the file's name from the full path. Before move it, I can select which Path that have been created using CheckedListBox and move it to target folder.

For example in my Log File I store (file has been created: C:\Users\AsrahLim\Desktop\New Microsoft Word Document.docx), all I need is the file's name "New Microsoft Word Document.docx" and move it to new folder .

This is my target folder: C:\Users\AsrahLim\Google Drive. Below is my code.

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

Upvotes: 0

Views: 11865

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112772

Don't try to implement your own logic for path manipulations. Use the shared Path class in System.IO instead.

Dim filename As String = Path.GetFileName(SourcePath)

Then you can construct the new path name with

Dim destinationPath As String = Path.Combine(MoveLocation, filename)

Also, test if the file exists in the destination location as well and delete it if it exists.

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

A side note: I don't like statements like If File.Exists(SourcePath) = True Then. Often people think that an If-statement requires a comparison. This is not true. All it needs is a Boolean expression, i.e. an expression returning either True or False. File.Exists(SourcePath) is an expression which does exactly this. The additional = True doesn't change anything and is superfluous, because if File.Exists(SourcePath) returns True then True = True is True and if File.Exists(SourcePath) returns False then False = True is False. = True is a neutral operation as is * 1 for numbers. You don't say Foo(1 * x), you just say Foo(x).

Upvotes: 2

Mederic
Mederic

Reputation: 2019

Very simple in your log you could store with a delimiter for instance:

New File Created*C:\test.docx

The star symbol is a banned character in file name so you can be sure it wont be in the path. After this you can just do

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

Ideally you just shouldn't store files a bit everywhere on your computer and use distinct folders.

Upvotes: 0

Related Questions