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?

Recommended Answers

All 5 Replies

You code is not for the version VB6.

You need to use FileSystemObject to achieve the same in VB 6.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.