By
robertcollier4
Choose a directory of link files to replace substring paths.
Also gives you a full display (text) of your LNK files.
#include <Array.au3>
#include <_RecFileListToArray.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#Include <GUIEdit.au3>
Local $dirToSearchDefault = @ProgramsCommonDir
; Local $dirToSearchDefault = "D:\testdir"
Local $dirToSearch = InputBox("Select directory", "In which directory are your LNK files?", $dirToSearchDefault, "", 520, 320)
If $dirToSearch = "" Then
Exit
EndIf
Local $listFiles = _RecFileListToArray($dirToSearchDefault, "*", 1, 1, 0, 2)
If (Not IsArray($listFiles)) and (@Error=1) Then
MsgBox (0,"","No files found in search path, exiting")
Exit
EndIf
;---------------------------------------------------------------
;Displaying list of Link LNK in GUI Editbox
Local $ThisTodisplayList = "LNK Files Found In: " & $dirToSearch & @CRLF & @CRLF
$ThisTodisplayList &= "========================" & @CRLF
Local $ThisTodisplayDrive = "", $ThisTodisplayDir = "", $ThisTodisplayFilename = "", $ThisTodisplayExtension = ""
For $i = 1 To $listFiles[0]
Local $listFilesPathSplit = _PathSplit($listFiles[$i], $ThisTodisplayDrive, $ThisTodisplayDir, $ThisTodisplayFilename, $ThisTodisplayExtension)
If(StringCompare(".lnk", $ThisTodisplayExtension, 2) == 0) Then
;Local $ThisTodisplayLNK = f$listFiles[$i]
Local $thisToDisplayLNKArray = FileGetShortcut($listFiles[$i])
If(NOT @error) Then
If($thisToDisplayLNKArray[0] <> "") Then
;_ArrayDisplay($thisToDisplayLNKArray)
;$ThisTodisplayLNKtarget = $thisToDisplayLNKArray[0]
;$ThisTodisplayLNKargs = $thisToDisplayLNKArray[2]
;$ThisTodisplayLNKworkingdir = $thisToDisplayLNKArray[1]
$ThisTodisplayList &= "LNK: " & $ThisTodisplayDrive & $ThisTodisplayDir & $ThisTodisplayFilename & $ThisTodisplayExtension & @CRLF
$ThisTodisplayList &= "Target: " & $thisToDisplayLNKArray[0] & " " & $thisToDisplayLNKArray[2] & @CRLF
$ThisTodisplayList &= "Workingdir:" & $thisToDisplayLNKArray[1] & @CRLF
$ThisTodisplayList &= @CRLF
$ThisTodisplayList &= "========================" & @CRLF
EndIf
EndIf
EndIf
Next
; _ArrayDisplay($listFiles, "$aFileList")
$GuiTodisplay = GUICreate("Summary of LNK Files Found In: " & $dirToSearch, 1152, 864)
; Local $idTreeview = GUICtrlCreateTreeView(6, 6, 1152, 1046, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$hEdit = GUICtrlCreateEdit($ThisTodisplayList, 4, 4, 1144, 772, BitOr($GUI_SS_DEFAULT_EDIT, $ES_READONLY))
$hButtonContinue = GUICtrlCreateButton("Continue", 4, 776, 1144, 80)
GUICtrlSetState($hButtonContinue ,$GUI_DEFBUTTON)
GUISetState(@SW_SHOW)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
GuiDelete($GuiTodisplay)
ExitLoop
Case $hButtonContinue
GuiDelete($GuiTodisplay)
ExitLoop
EndSwitch
WEnd
;Displaying list of Link LNK in GUI Editbox
;---------------------------------------------------------------
$modalSimulMode = MsgBox (3, "Simulated mode?" ,"Would you like to perform this substring replacement of link files in simulated mode? In simulated mode you will see what would have happened in a file named" & @CRLF & "LNKSubstringReplacer Results.txt" & @CRLF & @CRLF & "but no changes will be made" & @CRLF & @CRLF & "Yes - simulated mode, no changes will be made" & @CRLF & "No - resaving mode, changes will be made")
If $modalSimulMode = 2 Then
Exit
ElseIf $modalSimulMode = 6 Then
$saveAgain = False
ElseIf $modalSimulMode = 7 Then
$saveAgain = True
ElseIf
MsgBox(0, "ERROR", "Error with asking simulated mode, exiting")
EndIf
Local $StrToSearch = InputBox("Specify substring to search for", "What substring would you like to be replaced for link target and link working directory?", "", "", 520, 320)
; If $StrToSearch = "" Then ; allow empty input in case just want to resave and fix all links
If @error = 1 Then
Exit
EndIf
Local $StrToReplace = InputBox("Specify substring to replace with", "All occurrences of" & @CRLF & $StrToSearch & @CRLF & @CRLF & "Contained in links in the directory " & @CRLF & $dirToSearch & @CRLF & @CRLF & "Should be replaced with what?", $StrToSearch, "", 520, 320)
; If $StrToSearch = "" Then ; allow empty input in case just want to resave and fix all links
If @error = 1 Then
Exit
EndIf
; Processing
; Write simulated results to a text file
$dateNow = @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & @SEC
$fileLnkPath = @ScriptDir & "\" & $dateNow & " LNKSubstringReplacer Results.txt"
FileDelete($fileLnkPath)
Local $fileTextList = FileOpen($fileLnkPath, $FO_APPEND)
If $fileTextList = -1 Then
MsgBox(0, $fileLnkPath, "FileOpen Error, exiting")
Exit
EndIf
FileWrite($fileTextList, "---------------------------------------" & @CRLF)
FileWrite($fileTextList, "Link file to scan" & @CRLF)
FileWrite($fileTextList, "Link target" & @CRLF)
FileWrite($fileTextList, "Link working directory" & @CRLF)
Local $thisExtension = ""
Local $sDrive = "", $sDir = "", $ThisTodisplayLNKFilename = ""
For $i = 1 To $listFiles[0]
; Only do LNK extension
Local $listFilesPathSplit = _PathSplit($listFiles[$i], $sDrive, $sDir, $ThisTodisplayLNKFilename, $thisExtension)
; MsgBox(0, $i, $listFiles[$i] & " , " & $thisExtension)
If(StringCompare(".lnk", $thisExtension, 2) == 0) Then
; If(NOT StringCompare(".lnk", $thisExtension, 2)) Then
Local $thislnkArray = FileGetShortcut($listFiles[$i])
If(NOT @error) Then
If($thislnkArray[0] <> "") Then
; _ArrayDisplay($lnkArray)
If( StringInStr($thislnkArray[0], $StrToSearch) Or StringInStr($thislnkArray[2], $StrToSearch) Or StringInStr($thislnkArray[1], $StrToSearch) ) Then
FileWrite($fileTextList, @CRLF & "---------------------------------------" & @CRLF)
FileWrite($fileTextList, $listFiles[$i] & @CRLF)
; FileWrite($fileTextList, $thislnkArray[0] & @CRLF)
; FileWrite($fileTextList, $thislnkArray[2] & @CRLF)
FileWrite($fileTextList, $thislnkArray[0] & $thislnkArray[2] & @CRLF)
FileWrite($fileTextList, $thislnkArray[1] & @CRLF)
; Allow empty $StrToSearch or empty $strToReplace just to fix and resave all links
If( ($StrToSearch = "") OR ($StrToReplace = "") ) Then
$newLinkTarget = RemoveSurrQuotes($thislnkArray[0])
$newLinkTargetArgs = $thislnkArray[2]
$newLinkWorkingDir = RemoveSurrQuotes($thislnkArray[1])
Else
$newLinkTarget = RemoveSurrQuotes(StringReplace($thislnkArray[0], $StrToSearch, $StrToReplace))
$newLinkTargetArgs = StringReplace($thislnkArray[2], $StrToSearch, $StrToReplace)
$newLinkWorkingDir = RemoveSurrQuotes(StringReplace($thislnkArray[1], $StrToSearch, $StrToReplace))
EndIf
; Remove ending backslash from working dir
While (StringRight($newLinkWorkingDir, 1) = "\")
$newLinkWorkingDir = StringTrimRight($newLinkWorkingDir, 1)
WEnd
If($saveAgain) Then
FileWrite($fileTextList, @CRLF & "found " & @CRLF & "Resaved:" & @CRLF)
FileWrite($fileTextList, $listFiles[$i] & @CRLF)
FileWrite($fileTextList, $newLinkTarget & " " & $newLinkTargetArgs & @CRLF)
FileWrite($fileTextList, $newLinkWorkingDir & @CRLF)
$saveResult = FileCreateShortcut( _
$newLinkTarget, _
$listFiles[$i], _
$newLinkWorkingDir, _
$newLinkTargetArgs, _
$thislnkArray[3], _
$thislnkArray[4], _
"", _
$thislnkArray[5], _
$thislnkArray[6] _
)
If(NOT $saveResult) Then
FileWrite($fileTextList, "ERROR Unable to resave: " & $listFiles[$i] & @CRLF)
EndIf
Else
FileWrite($fileTextList, @CRLF & "found " & @CRLF & "Simulated Resave:" & @CRLF)
FileWrite($fileTextList, $listFiles[$i] & @CRLF)
FileWrite($fileTextList, $newLinkTarget & " " & $newLinkTargetArgs & @CRLF)
FileWrite($fileTextList, $newLinkWorkingDir & @CRLF)
EndIf
EndIf
Else
FileWrite($fileTextList, @CRLF & "---------------------------------------" & @CRLF)
FileWrite($fileTextList, $listFiles[$i] & @CRLF)
FileWrite($fileTextList, "Found empty link target, skipping " & @CRLF)
EndIf
Else
FileWrite($fileTextList, @CRLF & "---------------------------------------" & @CRLF)
FileWrite($fileTextList, $listFiles[$i] & @CRLF)
FileWrite($fileTextList, "FileGetShortcut Error, skipping " & @CRLF)
Exit
EndIf
;Else
; FileWrite($fileTextList, @CRLF & "---------------------------------------" & @CRLF)
; FileWrite($fileTextList, $listFiles[$i] & @CRLF)
; FileWrite($fileTextList, "Skipping not a link file" & @CRLF)
EndIf
Next
FileClose($fileTextList)
MsgBox (64,"Done - Check Log for details","Done - Check Log for details of what was done:" & @CRLF & @CRLF & $fileLnkPath)
;---------------------------------------------------------------
Func RemoveSurrQuotes($myString)
Return StringReplace($myString, '"', '')
EndFunc
;---------------------------------------
LNKSubstringReplacer.au3
_RecFileListToArray.au3