We help IT Professionals succeed at work.
Get Started

Dir, SubDir and Copy file

earngreen asked
Last Modified: 2012-05-09
I have a directory with a number of files in it. I am trying to manipulate the files by customer name and move them to a folder with this customers name. I have the following steps to complete

Get the customers number from the filename
Check to see if there is an existing folder for this customer if not create a folder, if the parent folder exists then check to see if there is a child subfolder for the previous month, if not create it and then copy the file into the child subfolder.

I am most of ther way there I am just having some trouble with the parent and child subfolders.

Dim FolderDate = Format(DateSerial(Year(Now()), Month(Now()), 0), "yyyy-mm")
        Dim d As New IO.DirectoryInfo("C:\SourceFolder\")
        Dim Destinationfolder As String = "C:\Test Folder\"
        Dim f As IO.FileInfo
        For Each f In d.GetFiles("*.pdf")
            Dim FName As String = f.Name.Substring(0, f.Name.LastIndexOf("."))
            FName = FName.Substring(0, 6)


            Dim path As String
            path = Destinationfolder & FName
            Dim subdi = path & "\" & FolderDate

            ' Determine whether the directory exists.
            If Directory.Exists(path) Then
            End If

            If Directory.Exists(subdi) Then

            End If

            ' Try to create the directory.
            Dim di As DirectoryInfo = Directory.CreateDirectory(path)
            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path))

            System.IO.File.Copy(f.FullName, subdi & "\" & f.Name, True)

            'Catch e As Exception
            '    Console.WriteLine("The process failed: {0}.", e.ToString())
            'End Try


Open in new window

Watch Question
Director of Information Technology
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE