Jump to content

LNKSubstringReplacer Link LNK Path Substring Replacer


 Share

Recommended Posts

Choose a directory of link files to replace substring paths.

Also gives you a full display (text) of your LNK files.

 

LNKSubstringReplacer_screenshot1.gif

LNKSubstringReplacer_screenshot2.gif.26fc2d6ae0c1f05469c7252bc353d03b.gif

#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

Edited by robertcollier4
updated script to add text display
Link to comment
Share on other sites

  • 11 months later...

I had to change some code to make it works:

line 87, changed 'ElseIf' to 'Else'

line 17, changed '$dirToSearchDefault' to '$dirToSearch'

I am also wondering why you didn't use build in function _FileListToArrayRec() instead of _RecFileListToArray.au3

Anyway, good job! Your script helped me a lot.

Link to comment
Share on other sites

  • Moderators

maniootek,

It is hardly surprising as _FileListToArrayRec is essentially my earlier _RecFileListToArray UDF - at the time of the original thread the code was not incorporated into the standard includes.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...