ShortenFolderNames
This code takes folders dragged and dropped win the format '99.99.99.(ADMIN|ADMN|PMOD)' and creates a shortcut by that name, renames the folder to '99.99.99-ADMN' shortening the file path.
Option ExplicitConst ThisScriptVersion = "2016-01-13.16.31"''drag and drop or send toDim WSH 'As New WshShell''take folder name(s)''for each fn in names''look for numeric portion''split after end of numeric portion''$fp= filepath to folder''$1= folder number id''$2= descriptive for human name''Rename folder $1''Create shortcut in $fp''Point to $1''Name of shortcut "shortcut - " Optional + $1 + $2''end for''''Debug in excel''Dim wscript As New objWSCRIPT_EmulatorDim FSO ''As New FileSystemObjectmain'''''''''''''''''''Sub test()Set FSO = CreateObject("Scripting.FileSystemObject")CreateShortcut "P:\60439114_Repair_Cadet_Chapel\7.0 - _CAD_GIS"End SubSub main()If MsgBox("This will take folders dragged/dropped to script named '99.99.99-longfilename'" & vbCr _& "Rename it '99.99.99'" & vbCr _& "and create a shortcut" & vbCr _& "'99.99.99.longnamehere' that points back the file." & vbCr _& vbCr _& "Continue?", vbYesNo + vbExclamation, "Warning- this will rename folders and break filepaths.") <> vbYes ThenMsgBox "Exiting. Nothing changed."wscript.quitEnd If''BEGIN BODY''cHECK ARGS- IF USER DOUBLE CLICKED OR NO ARGS RETURN INSTRUCTIONSSet FSO = CreateObject("Scripting.FileSystemObject")If wscript.Arguments.count = 0 ThenMsgBox "This Script version:" & ThisScriptVersion & vbCr & "Drag and drop a file or folder, or SendTo " & wscript.ScriptFullName & " to strip non-numeric folder titles and create 'human' shortcuts to erenamed folder.", vbOK, "Error"wscript.quitEnd IfIf wscript.Arguments.count > 0 ThenDim ArgItem ''Arg Item in wscriptDim DirCt ''Count Directories in drag and dropDim FileCt ''Count Files in drag and dropDim Folders() ''list of foldersDim xFor Each ArgItem In wscript.Arguments''If FileExists(ArgItem) Then FileCt = FileCt + 1If FSO.FolderExists(ArgItem) ThenDirCt = DirCt + 1ReDim Preserve Folders(DirCt)Folders(DirCt) = ArgItemEnd IfNextIf DirCt + FileCt <> wscript.Arguments.count ThenMsgBox "File Or Directory Does Not Exist", vbOK, "Source File or Folder error"EXIT_CLEANUPEnd IfEnd IfDim i, strsFor i = 1 To DirCtstrs = strs & vbCr & Folders(i)CreateShortcut Folders(i)Next ''iMsgBox (strs)End SubSub EXIT_CLEANUP()End SubSub CreateShortcut(fpFolder) ''As String)''msgbox fpFolderDim LNK ''''As linkDim PFolder ''As FolderDim oUrlLinkDim fn1 ''As StringDim fn2 ''As StringDim strLnkFPDim xSet PFolder = FSO.GetFolder(fpFolder)x = RegexpSplit(PFolder.Name)''Check for enough returns to create shortcutDim xixi = -1xi = UBound(x)If xi < 1 Then Exit Sub ''couldn't latch onto a common name format''If no change in pfolder name no need to shortcutIf FSO.FolderExists(PFolder.ParentFolder.Path & "\" & x(0)) Then Exit SubSet WSH = wscript.CreateObject("WScript.Shell")''strDesktop = WSH.SpecialFolders("Desktop")strLnkFP = PFolder.ParentFolder & "\" & PFolder.Name & ".lnk"If FSO.FileExists(strLnkFP) Then FSO.DeleteFile (strLnkFP)MsgBox x(0)Set LNK = WSH.CreateShortcut(strLnkFP) ''original name for link''rename folderPFolder.Name = x(0)LNK.TargetPath = PFolder.Path ''wscript.ScriptFullNameLNK.WindowStyle = 1''LNK.Hotkey = "CTRL+SHIFT+F"''LNK.IconLocation = "notepad.exe, 0"LNK.Description = x(0) & x(1)LNK.WorkingDirectory = PFolder.PathLNK.Save''Set oUrlLink = WSH.CreateShortcut(fpFolder & "\Microsoft Web Site.url")''oUrlLink.TargetPath = "http://www.microsoft.com"''oUrlLink.SaveEnd SubSub RegexpSplitTest()Debug.Print RegexpSplit("1.0_Admin")(0)End SubFunction RegexpSplit(strFN) ''As String) ''returns array of 2 items matching '###.###...' and the remainderDim rgq ''As String''rgq = "((?:\d+\.{0,1}){1,})(.*)"rgq = "((?:\d+\.{0,1}){1,}[^a-z,0-9]{0,}(?:ADMIN|ADMN|PMOD|PMGT){0,1})(.*)"Dim RegEx ''As New RegExpSet RegEx = CreateObject("vbscript.regexp")RegEx.Pattern = rgqRegEx.IgnoreCase = TrueDim m ''As MatchCollectionSet m = RegEx.Execute(strFN)If m.Item(0).SubMatches.count > 0 Then ''values weere found to match 2 subexpressionsReDim x(1)x(0) = UCase(m.Item(0).SubMatches.Item(0))x(1) = m.Item(0).SubMatches.Item(1)ElseReDim x(0)x(0) = strFN ''error return 1 value onlyEnd IfRegexpSplit = x ''return valueSet RegEx = NothingEnd Function
Comments
Post a Comment