Question Moving Multiple Files

Arctic Midnight

New member
Joined
May 5, 2014
Messages
4
Programming Experience
1-3
Hello everyone, I hope I posted this in the right area.
I'm currently working on a simple project, but I've hit a wall.
I'm trying to move files from one location to another. It worked with my.computer.filesystem.copyfile so i thought I could swap that for It worked with my.computer.filesystem.movefile. Turns out I couldn't.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CheckBox1.Checked = True Then
            My.Computer.FileSystem.MoveFile(destinationbox.Text, modBox.Text)
        End If
        If CheckBox1.Checked = False Then
            My.Computer.FileSystem.MoveFile(modBox.Text, destinationbox.Text, True)
            
        End If
    End Sub

    Private Sub selectmod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles selectmod.Click
        If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
            modBox.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub selectdestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles selectdestination.Click
        If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
            destinationbox.Text = FolderBrowserDialog1.SelectedPath

        End If
    End Sub

It works flawlessly when i'm copying, but it refuses to do so when I try to move it. It usually throws an exception at me and claims the file already exists in the new directory to move to, which it never is. Any help is much appreciated.


Now, it doesn't work; period. I decided to try and go back to copyfile, and now it refuses to work. It will tell me the directory already exists, as if it's trying to create the directory. I truly have no idea what's going on.
 
Last edited:
To be more specific, the error it gives me is "Could not complete operation since a directory already exists in this path" then the value of destinationbox.
Destinationbox: "C:\Users\choward\Desktop\DUMP" (The directory of which to move the files to)
modbox: "C:\Users\choward\Desktop\Test\snipermod.txt" (The file to move, which snipermod.txt is a placeholder)
 
Here's a fine example of why you should not accept the default names for controls. You're asking us to diagnose an issue with some code that depends on the Checked property of CheckBox1. What exactly is CheckBox1 for? If it had a meaningful name then we wouldn't have to ask. If that CheckBox1 is checked then your telling the system to move from a folder path to a file path. Could that possibly be the issue?
 
I never planned on posting the code anywhere, so my bad on that. Checkbox1 is to reverse the move. I have to rewrite the code now due to an error on my backups.

Though, I would like to state that I rewrote that entire section to merely
VB.NET:
 My.Computer.FileSystem.CopyFile(modBox.Text, destinationbox.Text, True)
Which is what it was before; and even that didn't work. I don't understand why none of it wants to work anymore.


My main objective is the user to select a file, then a destination, and have it move one or more files to the selected destination. I'll admit it, I'm not very seasoned. I've made a few programs but nothing too awful fancy.
 
I never planned on posting the code anywhere, so my bad on that. Checkbox1 is to reverse the move. I have to rewrite the code now due to an error on my backups.

Though, I would like to state that I rewrote that entire section to merely
VB.NET:
 My.Computer.FileSystem.CopyFile(modBox.Text, destinationbox.Text, True)
Which is what it was before; and even that didn't work. I don't understand why none of it wants to work anymore.


My main objective is the user to select a file, then a destination, and have it move one or more files to the selected destination. I'll admit it, I'm not very seasoned. I've made a few programs but nothing too awful fancy.

When things don't work, we don't throw our hands up in despair. We test our theories. According to that code, 'modBox.Text' contains the path of the file that you want to copy, 'destinationbox.Text' contains the path of the folder that you want to copy it to and you want to overwrite the destination file if it exists. Let's then test that the source file exists and the destination directory exists:
If IO.File.Exists(modBox.Text) Then
    MessageBox.Show("Source file exists.")
Else
    MessageBox.Show("Source file DOES NOT exist.")
End If

If IO.Directory.Exists(destinationbox.Text) Then
    MessageBox.Show("Destination folder exists.")
Else
    MessageBox.Show("Destination folder DOES NOT exist.")
End If
 
You're 100% correct on what goes where. In a past program I made, you selected a file which was then moved, but the destination was static / hardcoded.
After using the code above, it did show that the source file exists and the directory of which I was trying to use do both exist, but it then gave me the same error "Could not complete operation since a directory already exists in this path 'C:\Users\choward\Desktop\DUMP'."

It's almost like something got corrupted.



Edit 1: Well, i tried using "System.IO.Directory.Move" and now it tells me the file exists, even though it clearly does not. I'm at an utter loss as to why this code hates me all of a sudden; when i used it before. Truth be told, the only real thing that is different is the destination is not static as it was on my last program; aside from not having any fancy timers or progress-bars for flashy stuff. (I really like progress-bars :drunk:)


Here's the code from that program.
(From the Transfer Button)
VB.NET:
If OnlineBox.Text = "" Then
            MessageBox.Show("Please select a file for online sound.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End If

        If MessageBox.Text = "" Then
            MessageBox.Show("Please select a file for message sound.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End If
        Timer1.Start()
        My.Computer.FileSystem.CopyFile(OnlineBox.Text, "C:\Program Files (x86)\Steam\friends\friend_online.wav", True)
        My.Computer.FileSystem.CopyFile(MessageBox.Text, "C:\Program Files (x86)\Steam\friends\message.wav", True)

(Under Timer1.tick)
VB.NET:
ProgressBar1.Increment(1)

        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Timer1.Stop()
            Beep()
            MessageBox.Show("Sounds Replaced", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
            If DialogResult.OK Then
                ProgressBar1.Value = ProgressBar1.Minimum
            End If

        End If

EDIT 2: After some experimenting, it breaks when I attempt to have it move to a directory, rather than a file instead. I'll keep experimenting.
 
Last edited:
Back
Top