moving files problem

seth-gr

Member
Joined
May 21, 2007
Messages
8
Location
Greece
Programming Experience
Beginner
Hello ,
I have this code but its just not working and i cant findout why Ihaven't any errors in compiling but it doesn't do anything at all
If anyone has an idea or a link please help I have found this code in this link
http://www.homeandlearn.co.uk/NET/nets8p7.html
I noticed that if I put a specific file name such as file.txt is working but i want to take all the files with txt extension. As they have told me in another forum this function doesn't support wildcards such as (*) is there another way to move all the *.txt files without knowing their names?

Thank you in advance

VB.NET:
Imports System.IO
Imports System.Diagnostics
Imports System.IO.File (FileToMove1, MoveLocation1)
 
Dim FileToMove1 As String
Dim MoveLocation1 As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   
        If System.IO.File.Exists(FileToMove1) = True Then
            System.IO.File.Move(FileToMove1, MoveLocation1)
        End If
        FileToMove1 = "E:\800\*.txt"
        MoveLocation1 = "E:\800\temp\"

 End Sub
 
Last edited by a moderator:
this should work, it moves all files in all folders (starting at the root folder you provide, being "E:\800" in this case) to the new folder
VB.NET:
Imports System.IO
Imports System.Diagnostics
 
Private m_SourceFilesDir As String
Private m_DestFilesDir As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  m_SourceFilesDir = "E:\800\"
  m_DestFilesDir = "E:\800\temp\"
  Call MoveFiles(m_SourceFilesDir, m_DestFilesDir)
End Sub

Private Sub MoveFiles(ByVal Src As String, ByVal Dest As String)
  If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then Dest &= Path.DirectorySeparatorChar
  Dim Files() As String = Directory.GetFiles(Src)
  For Each element As String In Files
    File.Move(element, Dest & Path.GetFileName(element))
  Next element
End Sub
 
Something to note: The System.IO.Directory.GetFiles method has an onverload that accepts a searchPattern as it's second parameter. You can pass *.txt as the value of that parameter to get only text files.

seth-gr, your profile says you are using .NET 1.0. If you are indeed using 1.0 you should update to at least 1.1. The latest version of the framework is 3.
 
Back
Top