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>

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

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")

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

Import System.IO

Sub MoveFiles()
     Filenames = Directory.GetFiles("C:\uploaded_files")
     SubBuild = ""
     For Each Filename as String in Filenames
          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 Directory.exists("C:\Files\" & subbuild) then
               file.move(filename,"C:\Files\" & subbuild & filename)
          else
               Directory.Create("C:\Files" & subbuild)
               file.move(filename,"C:\Files\" & subbuild & filename)
          End if
     Next
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.

Thank you JRSofty! A little direction from someone who knows and cares turns into a great help for someone in need. God bless you!

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?

Did you put it before any declarations where made? (on the very top of your file)

Niek

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()
**********************

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.