We help IT Professionals succeed at work.

Dir, SubDir and Copy file

earngreen
earngreen asked
on
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)

            MessageBox.Show(FName)
            MessageBox.Show(f.FullName)

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

            'Try
            ' Determine whether the directory exists.
            If Directory.Exists(path) Then
                'Return
            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

        Next

Open in new window

Comment
Watch Question

Senior .Net Developer
Commented:
your create needs to be inside of the if exists.

'or use if not
   If Directory.Exists(subdi) Then

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


            End If



     
Assume CustomerName$ contains the customer name, and C:\Myfolder is the folder where you want to put the subfolders.
If Dir$("C:\MyFolder\" & CustomerName$, 16) = "" then MkDir("C:\MyFolder\" & CustomerName$)
This creates teh folder witht he custer name, if it does not already exist.

If Dir$("C:\MyFolder\" & CustomerName$ & "\" & Format$(Now,"mmm") & Format$(Now,"yyyy"), 16) = "" then MkDir("C:\MyFolder\" & CustomerName$ & "\" & Format$(Now,"mmm") & Format$(Now,"yyyy")

This creates a folder with the current month and year for a name within hte custoemr's name folder if it does not exist.
 
Regarding your statement, "I am most of ther way there I am just having some trouble with the parent and child subfolders. "
It would help if you could be mroe specific. I gave you code above for detecting and creating the subfolders, but you never really told us what point was giving you the trouble. If the problem was copying, for example, I suggest that you use
FileCopy StartFolder$ & "\" & MyFileName$, EndFolder$ & "\" & MyFileName$
Please excuse my lack for spell checking in my first answer. I really have a lot of trouble with the Experts-Exhange text editor. :-(
You may notice that I use the more basic VB statements like FileCopy, MkDir, and Dir$, rather than more complicated functions like System.IO.File.Copy. I find that this saves a lot of trouble.

I am also assuming that you are using something like VB6, since you did not specify and this is listed in the Visual Basic Classic area.

Author

Commented:
Basically, I have a directory of files in which I need to move. The the first nine digits of the filename is the customer number. I need to read the filename and move it to a subfolder within the customer number folder. The problem is that before I copy/move the file. I am not sure if there is a folder that has been created with the customer number nor the subfolder that needs to be created either. The subfolder is always going to be the previous months date in the format of yyyy-mm.  I guess that the problem that I am having is the logic of creating the parent folder and subfolder on the fly. The application is in vb.net but I can translate vb6 code.
In tha case, I think the
If Dir$(FolderName$,16) = "" Then
code should be fine. A similar code is

If Len(Dir$(FolderName$,16)) = 0 Then
This executes the code if the folder does not exist.
I prefer a simple MkDir command to create the folder. For example, to create
C:\Program Files\MyFolder\JohnSmith\June2010 just use
MkDir "C:\Program Files\MyFolder\JohnSmith\June2010"
Does this work for you?