I want to create a program that I can create a Text file folder on my desktop and move all the text files into the folder.

Here is what I have so far:

Below is where I create the Text folder:

Dim myPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Text")
IO.Directory.CreateDirectory(myPath)

Here is where I am trying to move all of the .txt files to the new Text folder:

For Each foundFile As String In My.Computer.FileSystem.GetFiles _
        (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
        FileIO.SearchOption.SearchTopLevelOnly, "*.txt")

Here is where the error is: It won't do it...

My.Computer.FileSystem.MoveFile(foundFile, myPath & "/" &   foundFile)

Next

Do you have any tips for moving the .txt files to the Text folder?

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

See if this helps.

Dim myPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Text"
        If Not IO.Directory.Exists(myPath) Then IO.Directory.CreateDirectory(myPath) '// create if it does not exist.
        For Each foundFile As String In My.Computer.FileSystem.GetFiles _
       (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
       FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
            My.Computer.FileSystem.MoveFile(foundFile, myPath & "/" & IO.Path.GetFileName(foundFile))
        Next

The issue was that you are getting the full file path when you use "foundFile" and you only need to get the FileName and Extension of the "foundFile" to have it moved.

Thanks, I think I figured it out! I used your code and a try/catch with a Savefiledialog and it worked... Here it is:

If Not IO.Directory.Exists(myPath) Then IO.Directory.CreateDirectory(myPath) '// create if it does not exist.
        For Each foundFile As String In My.Computer.FileSystem.GetFiles _
         (My.Computer.FileSystem.SpecialDirectories.Desktop, _
         FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
            count = ++count
            Try
                My.Computer.FileSystem.MoveFile(foundFile, myPath & "/" & IO.Path.GetFileName(foundFile))
            Catch
                If My.Computer.FileSystem.FileExists(foundFile) Then
                    MsgBox("File: " & foundFile & " already belongs to the: " & myPath & " directory. Please, choose a directory to move it to")
                    SaveFileDialog1.InitialDirectory = myPath
                    SaveFileDialog1.FileName = foundFile.Replace(".txt", ++count & ".txt")
                    SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

                    SaveFileDialog1.ShowDialog()
                    My.Computer.FileSystem.DeleteFile(foundFile)
                End If
            End Try
        Next

Thanks again,
Mike

Edited 5 Years Ago by __avd: Added [code] tags.

Thanks, I think I figured it out! I used your code and a try/catch with a Savefiledialog and it worked... Here it is:

If Not IO.Directory.Exists(myPath) Then IO.Directory.CreateDirectory(myPath) '// create if it does not exist.
For Each foundFile As String In My.Computer.FileSystem.GetFiles _
(My.Computer.FileSystem.SpecialDirectories.Desktop, _
FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
count = ++count
Try
My.Computer.FileSystem.MoveFile(foundFile, myPath & "/" & IO.Path.GetFileName(foundFile))
Catch
If My.Computer.FileSystem.FileExists(foundFile) Then
MsgBox("File: " & foundFile & " already belongs to the: " & myPath & " directory. Please, choose a directory to move it to")
SaveFileDialog1.InitialDirectory = myPath
SaveFileDialog1.FileName = foundFile.Replace(".txt", ++count & ".txt")
SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

SaveFileDialog1.ShowDialog()
My.Computer.FileSystem.DeleteFile(foundFile)
End If
End Try
Next

Thanks again,
Mike

########################################################################################

One more question: How do you specify shortcuts: i.e. Firefox shortcut on desktop?
I also want to make a folder for shortcuts.

Thanks in advance,
Mike

About your recent code posted, check if "Ok" is clicked, otherwise it will delete the file even if you cancel.

If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            My.Computer.FileSystem.DeleteFile(foundFile)
        End If

Specify shortcuts, start a new thread and supply more information.

This article has been dead for over six months. Start a new discussion instead.