'This program will go through a prompted folder and move the higher up revision into the obsolete folder should it find one.
'It works by pulling everthing right of the last \ and then taking the first 10 or 12 chars based upon it being a PO
'It then compares this check string to the rest of the file names
'Imports are used for external features. Some may be redundant
Imports System
Imports System.Diagnostics
Imports System.Threading
Imports System.Windows.Forms
Sub Main()
'This Calls the private function oFolder which allows you to select a folder path from a list. It simply returns a string of the folder path
oFolder = oFolderDlg
'This is just a confirmation chunk to ensure the right folder was selected.
r= MsgBox(oFolder & " was selected" &vbLf & vbLf & "Proceed?", MsgBoxStyle.YesNo,"iLogic")
If r = MsgBoxResult.No
Exit Sub
End If
'Declarations for the File Operations done using the vb.net that interacts with the system
Dim oFiles() As String
Dim oFile As String
'This text save file is simply for recording post-processing info. Change the text file to some existing text location on your computer.
'The text file is written to by the oWrite.Writeline() command. If used, it must also include an open and close feature.
Dim oTextSave As String = "C:\Users\Rob\Desktop\Test.txt"
'A boolean to check if the obsolete folder was found, so that the user wont be prompted every loop regardless of the outcome.
Dim oObsFound As Boolean = False
'Booleans to control the looping of the program
Dim oSkip As Boolean
Dim circ As Boolean = True
'Creating the text file for post processing
oWrite = System.IO.File.CreateText(oTextSave)
'The while loop to control the folder search feature
While circ = True
'Making a list of the files in the folder, as first found by using the Folder Dialog Private function
oFiles = System.IO.Directory.GetFiles(oFolder)
'Reset the oSkip
oSkip = False
'Iterating through the list of files one at a time.
For Each oFile In oFiles
'Just a variable used to shorten the file name for comparison sake; this one is grabbing our part number from the end of the name; ie; 200123-123
'from the string C:/Users/200123-123.ipt
oCheck = Left(Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1)), 10)
'Just a condition check; some titles we have contain PO, so I perform a different check for them, because of how the strings work
If oCheck Like "*PO*"
oCheck = Left(Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1)), 12)
End If
'Set the index to 0 again seeing as this is in a loop
i=0
'This string checks for as many files are in the array. (0 to n-1)
For j = 0 To oFiles.Length - 1
'This declares oFn as the String that occupies the array oFiles at the location j
oFn = oFiles(j)
'THis is a string comparison; LIKE is an operator, * are wildcards, meaning any character and any amount of them
'It is essentially checking to see if the file name in the folder matches what I am check it against.
If oFn Like "*" & oCheck & "*"
i=i+1
End If
Next
'This whole i+ and i+1 will tell me if i have more than one similiar file.
'Our file system has files designate 201231-123_R1 where R is revision number.
'From there, I remove all revisions that are lower than the highest one; which is what the i index keeps track of
'The delete only needs to occur if there is more than 1 revision in the folder, thus the i>1
'oWrite is just recording what files i delete so i have a log of it in case something goes wrong
If i >1
oWrite.WriteLine(oFile)
'This is a check; for the current folder, if it has not found the sub-folder that I want to move the files to, it will prompt the user for input
'This is unnessary to prompt the user for input if the program can find the folder on it's own; the oObsFound or "Obsolete Found" is the controller for 'this
If oObsFound = False
'This is the string to search for the obsolete folder using the program
oOBS = Left(oFile, InStrRev(oFile, "\", -1)) & "OBSOLETE\"
'This is checking if the folder path that I want to move the files to exists already.
If System.IO.Directory.Exists(oOBS) = False
MsgBox("Obsolete folder not found, please select manually")
'Again, the oFolderDlg function to allow the user to find/make the folder to move files to.
oOBS = oFolderDlg
End If
'This is telling it that it found the obsolete folder, or one was made, so that it doesn't need to look for it next time through the loop, or until i reset the control again.
oObsFound = True
End If
'This is the full file path. The move function requires that you put the whole string of what you want the file to end up as rather than just the directory you want to move it to; ie it's a copy, rename, and delete.
oNewName = oOBS & Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1))
'A try loop for moving the file in case there are any errors; such as a file of the same name already existing in the folder.
Try
System.IO.File.Move(oFile, oNewName)
Catch
MsgBox(oNewName)
End Try
'I cant recall what the oSkip is for, but it plays some function in controlling the looping
oSkip = True
Exit For
End If
Next
If oSkip = True
Else
circ = False
End If
End While
'THis closes it and ends off the function
'MsgBox(oCheck)
oWrite.Close
ThisDoc.Launch(oTextSave)
End Sub
Function oFolderDlg
Dim dialog = New FolderBrowserDialog()
' dialog.SelectedPath = Application.StartupPath
dialog.ShowNewFolderButton = True
' openFileDialog1.InitialDirectory
If DialogResult.OK = dialog.ShowDialog() Then
oPath = dialog.SelectedPath
Else
MsgBox("No File Selected. Aborting Rule")
oPath = ""
End If
Return oPath
End Function
--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.