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>