Automatic Files Sorting and Folders Creation

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 33
Reputation: leokuz is an unknown quantity at this point 
Solved Threads: 0
leokuz leokuz is offline Offline
Light Poster

Automatic Files Sorting and Folders Creation

 
0
  #1
Feb 28th, 2008
I am wondering if it is possible to accomplish following in VB.NET:

I would like to automate files move and sorting from "C:\uploaded_files" (files contain any extensions e.g. .pdf, .tif, .eml, .jpg, etc) to directory under different subdirectories as shown below.

F:\Files\Oregon\Sold\Victoria Kraus\2008\OSVK082036PI\
F:\Files\Oregon\Leased\Victoria Kraus\2008\OLVK082036PI\
F:\Files\Washington\Sold\Victoria Kraus\2008\WSVK082036PI\
F:\Files\Washington\Leased\Victoria Kraus\2008\WLVK082036PI\
F:\Files\Oregon\Sold\Dan Richardson\2008\OSDR082036PI\
F:\Files\Oregon\Leased\Dan Richardson\2008\OLDR082036PI\
F:\Files\Washington\Sold\Dan Richardson\2008\WSDR082036PI\
F:\Files\Washington\Leased\Dan Richardson\2008\WLDR082036PI\

I have following file naming: OSVK082036PI.pdf or OLVK082036PI.tif or WSVK082036PI.pdf or WLVK082036PI.doc. Many files also are more descriptive, like OSVK082036PI-newRequest.pdf, etc. Last part after the dash is not so important for file sorting.

First letter in the file "O" or "W" stands for the first letter of first subfolder’s name, then second letter "S" or "L" stands for the next subfolder's first name name, then "VK" or any other combination of two or three letters also has to do with next subfolder's name but not necessarily first letters of the subfolder (these are just initials of the employee in charge of the file), then next two letters "08" or "07" stands for next subfolder’s last two letters name (this subfolder name usually is 2008 or 2007 or 2009 and stands for the year). The last subfolder’s name includes "08" or "07" etc plus last six letters of the file (e.g. "082036PI" and can begin on "08", "07", "06", "09" etc.). That is the folder where corresponding files are moved into.

Directory 2008 for example (stands for the year) may or may not exist when file with "08" or "07" is uploaded, but I want script/program to check file for presence of "08" or “07” in it and then if subfolder 2008 does not exist create one and then create in it subfolder named after 12 first letters of the file name (e.g. OLVK082036PI) and move files into that folder. However, if folder 2008 or folder named after first 12 letters of file name already exists, then I would like file/s just to be moved into that folder. If, let's say file with same name already exists in the destination folder, then I would like new file being appended in the end with let’s say time stamp separated from original file name by dash (e.g. OLVK082036PI-newContract-0227080133.pdf).

I have following script, but it is limited in what it can do, and I don't like that it prompts for user's input. I would like completely automated without user interaction. The file, or program I plan to run by scheduling it in the Windows Task Scheduler”. I am doing this on Windows 2003 Server, but testing on Windows Vista.

To summarized: I would like program or script to be able to find appropriate “year” folder and the file folder and move files into it. If appropriate year and/or file folder does not exist, I want it to be automatically created and files moved into it.

If someone could help or direct me to the right directions I would highly appreciate. Thank you.

Here is my code to start up with (saved with extension .wsf):

' The following script will move files from one folder over to
' folders named exactly the same as the files minus their extension.
'
' You must specify the folder location of the files, the base folder
' that contains all the destination folders and the file extension.
' The script will avoid all files without the same extension, and
' if the child destination folder does not exist, the file will not
' be copied over.

<package>
<job id="move_files">
<script language="vbscript">
Option Explicit
On Error Resume Next

Dim strSourceFolder, strDestinationFolder, strFileExtension, strLogFile
strSourceFolder = InputBox("Base Folder:" & vbCRLF & "Enter fully qualified path for the base folder that contains the files that are to be moved. TRAILING SLASH IS NECESSARY!", "Move files - Step 1")
strDestinationFolder = InputBox("Destination Folder:" & vbCRLF & "Enter fully qualified path for the destination folder that contains the directories that correspond with the filenames (without extensions). TRAILING SLASH IS NECESSARY!", "Move files - Step 2")
strFileExtension = InputBox("File extension:" & vbCRLF & "Enter the file extension of the files you want to move. This script will avoid all files that do not match this extension.", "Move files - Step 3")
strLogFile = "move_files.log"

' Declare objects.
Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1, ForWriting = 2, ForAppending = 8

' Setup the logging feature.
Dim objLogFile : Set objLogFile = FSO.OpenTextFile(strLogFile, ForWriting, True)

objLogFile.WriteLine "Generated on: " & Date
objLogFile.WriteLine ""

' Check to ensure that the values entered are accurate.
If FSO.FolderExists(strSourceFolder) And FSO.FolderExists(strDestinationFolder) Then
' The folders exist, proceed with copying the files.
Dim objSourceFolder, colFiles, objFile, strChildFolderName, intCounter

Set objSourceFolder = FSO.GetFolder(strSourceFolder)
Set colFiles = objSourceFolder.Files
intCounter = 0

' Run through the files in the source folder.
For Each objFile In colFiles
If Right(objFile.Name, Len(strFileExtension)) Then
strChildFolderName = Left(objFile.Name, Len(objFile.Name) - Len(strFileExtension))
If FSO.FolderExists(strDestinationFolder & strChildFolderName) Then
objFile.Move strDestinationFolder & strChildFolderName & "\", True
If Err Then
objLogFile.WriteLine "File NOT copied (" & Err.Number & ": " & Err.Description & "): " & objFile.Name & " to " & strDestinationFolder & strChildFolderName
Else
intCounter = intCounter + 1
End If
Else
objLogFile.WriteLine "File NOT copied (destination folder does not exist): " & objFile.Name & " to " & strDestinationFolder & strChildFolderName
End If
Else
objLogFile.WriteLine "File IGNORED (incorrect file extension): " & objFile.Name
End If
Next

Set objSourceFolder = Nothing
Set colFiles = Nothing
Else
' Either the source or destination folder does not exist.
Msgbox "Either the source or destination folder does not exist. Please enter valid pathnames."
End If

objLogFile.WriteLine "Files copied: " & intCounter
objLogFile.Close
Set objLogFile = Nothing
Set FSO = Nothing
WScript.Quit
</script>
</job>
</package>
Last edited by leokuz; Feb 28th, 2008 at 1:41 am. Reason: correction
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: leokuz is an unknown quantity at this point 
Solved Threads: 0
leokuz leokuz is offline Offline
Light Poster

Re: Automatic Files Sorting and Folders Creation

 
0
  #2
Feb 28th, 2008
I am thinking this (in general)

Source folder c:\uploaded_files

if file name is OSVK08*.*

then if folder F:\Files\Oregon\Sold\Victoria Kraus\2008\OSVK08* exists
move files here

if file exist, then append

if folder F:\Files\Oregon\Sold\Victoria Kraus\2008\OSVK08* does not exists

create folder

move file here
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: leokuz is an unknown quantity at this point 
Solved Threads: 0
leokuz leokuz is offline Offline
Light Poster

Re: Automatic Files Sorting and Folders Creation

 
0
  #3
Feb 28th, 2008
And I would like automatic folder creation (if does not exist) to be on this level:

\2008\OSVK08*

that is two subfolders inside subdirectory corresponding to employee's name (e.g. "Victoria Kraus")
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: JRSofty is an unknown quantity at this point 
Solved Threads: 10
JRSofty's Avatar
JRSofty JRSofty is offline Offline
Junior Poster in Training

Re: Automatic Files Sorting and Folders Creation

 
0
  #4
Feb 29th, 2008
To do this in VB.NET is actually fairly simple. First you will need to import the System.IO namespace and then do some chopping up of the file name.

Here's some pseudo code on how to accomplish this
  1. Import System.IO
  2.  
  3. Sub MoveFiles()
  4. Filenames = Directory.GetFiles("C:\uploaded_files")
  5. SubBuild = ""
  6. For Each Filename as String in Filenames
  7. if mid(filename,0,1).tolower = "o" then
  8. SubBuild = "Oregon\"
  9. Else
  10. SubBuild = "Washington\"
  11. End If
  12. If mid(filename,1,1).tolower = "s" then
  13. SubBuild = SubBuild & "Sold\"
  14. Else
  15. SubBuild = SubBuild & "Leased\"
  16. End If
  17. If mid(filename,2,2).tolower = "vk" then
  18. SubBuild = SubBuild & "Victoria Kraus\"
  19. else
  20. SubBuild = SubBuild & "Dan Richards\"
  21. end if
  22. if mid(filename,4,2).tolower = "08" then
  23. subbuild = subbuild & "2008"
  24. end if
  25. if Directory.exists("C:\Files\" & subbuild) then
  26. file.move(filename,"C:\Files\" & subbuild & filename)
  27. else
  28. Directory.Create("C:\Files" & subbuild)
  29. file.move(filename,"C:\Files\" & subbuild & filename)
  30. End if
  31. Next
  32. End Sub

Don't use my code exactly because it is pseudo VB.net. However, it should point you in the right direction to perform the function that you want.
JRSofty Programming | .NET Dreaming | GalahTech

If your question is solved then mark the thread solved. If someone gives you good advice then give them some rep.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: leokuz is an unknown quantity at this point 
Solved Threads: 0
leokuz leokuz is offline Offline
Light Poster

Re: Automatic Files Sorting and Folders Creation

 
0
  #5
Feb 29th, 2008
Thank you JRSofty! A little direction from someone who knows and cares turns into a great help for someone in need. God bless you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: leokuz is an unknown quantity at this point 
Solved Threads: 0
leokuz leokuz is offline Offline
Light Poster

Re: Automatic Files Sorting and Folders Creation

 
0
  #6
Mar 1st, 2008
JRSofty, I'm trying to accomplish this using Visual Basic 2008 Express Edition. When I have copied your code into it I got following errors ("Declaration expected" error refers to Import System.IO):

Error 1: Declaration expected.
Error 2: Name 'Filenames' is not declared.
Error 3: Name 'Directory' is not declared.
Error 4: Name 'SubBuild' is not declared.
Error 5: Name 'Filenames' is not declared.

Sorry, I don't want you to do my homework but as new to VB and programming as a whole I am wondering why Import System.IO is not recognized here?
Last edited by leokuz; Mar 1st, 2008 at 6:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,827
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Automatic Files Sorting and Folders Creation

 
0
  #7
Mar 3rd, 2008
Did you put it before any declarations where made? (on the very top of your file)

Niek
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: leokuz is an unknown quantity at this point 
Solved Threads: 0
leokuz leokuz is offline Offline
Light Poster

Re: Automatic Files Sorting and Folders Creation

 
0
  #8
Mar 3rd, 2008
This is so far where I went iwth JRSofty's suggestion (because I was getting many more errors I have modified code replacing some lines with "My.Computer.FileSystem...":

'**********************

Sub Main()

End Sub

Dim SubBuild As String
Dim Filename As String


Sub MoveFiles()
Filename = My.Computer.FileSystem.GetFiles("C:\uploaded_files")
SubBuild = ""
For Each Filename As String In Filename
If Mid(Filename, 0, 1).ToLower = "o" Then
SubBuild = "Oregon\"
Else
SubBuild = "Washington\"
End If
If Mid(Filename, 1, 1).ToLower = "s" Then
SubBuild = SubBuild & "Sold\"
Else
SubBuild = SubBuild & "Leased\"
End If
If Mid(Filename, 2, 2).ToLower = "vk" Then
SubBuild = SubBuild & "Victoria Kraus\"
Else
SubBuild = SubBuild & "Dan Richards\"
End If
If Mid(Filename, 4, 2).ToLower = "08" Then
SubBuild = SubBuild & "2008"
End If
If My.Computer.FileSystem.DirectoryExists("C:\Files\" & SubBuild) Then
My.Computer.FileSystem.MoveFile(Filename, "C:\Files\" & SubBuild & Filename)
Else
My.Computer.FileSystem.CreateDirectory("C:\Files" & SubBuild)
My.Computer.FileSystem.MoveFile(Filename, "C:\Files\" & SubBuild & Filename)
End If
Next
End Sub

End Module

'**************************

Just to test/see, I have removed "Import System.IO" from code and I am getting right now one error, that is "Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' cannot be converted to 'String'".

However I have tried to put Import System.IO in following places with no success:

*********************
Module Module1

Import System.IO

Sub Main()

End Sub
*********************
Module Module1

Sub Main()

Import System.IO

End Sub
*******************
Import System.IO
Dim SubBuild As String
Dim Filename As String


Sub MoveFiles()
**********************
Last edited by leokuz; Mar 3rd, 2008 at 3:22 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC