Jump to content

LNKSubstringReplacer Link LNK Path Substring Replacer

Recommended Posts


Choose a directory of link files to replace substring paths.





#include <Array.au3>
#include <_RecFileListToArray.au3>
#include <File.au3>

$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
ElseIf $modalSimulMode = 6 Then
    $saveAgain = False
ElseIf $modalSimulMode = 7 Then
    $saveAgain = True
    MsgBox(0, "ERROR", "Error with asking simulated mode, exiting")

Local $dirToSearchDefault = @ProgramsCommonDir
; Local $dirToSearchDefault = "T:\testdir"

Local $dirToSearch = InputBox("Select directory", "In which directory would you like to search for files to perform substring link replacement for?", $dirToSearchDefault, "", 520, 320)
If $dirToSearch = "" Then

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

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

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")

; _ArrayDisplay($listFiles, "$aFileList")

; Write simulated results to a text file
$dateNow = @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & @SEC
$fileLnkPath = @ScriptDir & "\" & $dateNow & " LNKSubstringReplacer Results.txt"
Local $fileTextList = FileOpen($fileLnkPath, $FO_APPEND)
If $fileTextList = -1 Then
    MsgBox(0, $fileLnkPath, "FileOpen Error, exiting")

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 = "", $sFileName = ""

For $i = 1 To $listFiles[0]
    ; Only do LNK extension
    Local $listFilesPathSplit = _PathSplit($listFiles[$i], $sDrive, $sDir, $sFileName, $thisExtension)
    ; MsgBox(0, $i, $listFiles[$i] & " , " & $thisExtension)

    If(StringCompare(".lnk", $thisExtension, 2) == 0) Then
    ; If(NOT StringCompare(".lnk", $thisExtension, 2)) Then

        FileWrite($fileTextList, @CRLF & "---------------------------------------" & @CRLF)
        FileWrite($fileTextList, $listFiles[$i] & @CRLF)

        Local $thislnkArray = FileGetShortcut($listFiles[$i])
        If(NOT @error) Then
            If($thislnkArray[0] <> "") Then
                ; _ArrayDisplay($lnkArray)
                ; 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])
                    $newLinkTarget = RemoveSurrQuotes(StringReplace($thislnkArray[0], $StrToSearch, $StrToReplace)) 
                    $newLinkTargetArgs = StringReplace($thislnkArray[2], $StrToSearch, $StrToReplace) 
                    $newLinkWorkingDir = RemoveSurrQuotes(StringReplace($thislnkArray[1], $StrToSearch, $StrToReplace))

                ; Remove ending backslash from working dir
                While (StringRight($newLinkWorkingDir, 1) = "\")
                    $newLinkWorkingDir = StringTrimRight($newLinkWorkingDir, 1)

                If($saveAgain) Then
                    FileWrite($fileTextList, @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)
                    FileWrite($fileTextList, @CRLF & "Simulated Resave:" & @CRLF)
                    FileWrite($fileTextList, $listFiles[$i] & @CRLF)
                    FileWrite($fileTextList, $newLinkTarget & " " & $newLinkTargetArgs & @CRLF)
                    FileWrite($fileTextList, $newLinkWorkingDir & @CRLF)
                FileWrite($fileTextList, "Found empty link target, skipping " & @CRLF)
            FileWrite($fileTextList, "FileGetShortcut Error, skipping " & @CRLF)
        FileWrite($fileTextList, @CRLF & "---------------------------------------" & @CRLF)
        FileWrite($fileTextList, $listFiles[$i] & @CRLF)
        FileWrite($fileTextList, "Skipping not a link file" & @CRLF)


Func RemoveSurrQuotes($myString)
    Return StringReplace($myString, '"', '')



Edited by robertcollier4

Share this post

Link to post
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

  • Similar Content

    • 31290
      By 31290
      Hi everyone, hope you are doing fine
      Well, I'm currently writing a small script that goes to a certain web page, finds the first link of a specified section and download the file associated to this link.
      Depending on the computer that the tool is launched, the script gets the computer model and search in the (provided here) ini file which link to follow.
      At first, Dell was kind enough to provide only one link but now, they provide two of them. The first one is now a .txt file (  ) whereas my script has been designed to download only the fist and latest link released for the BIOS Update.

      Here's the current code which is working with only the first and latest link of the BIOS category:
      So the question is: 
      In the case of double links like shown in the picture above, how it is possible to tell the script to download only the link containing an the .exe file?
      Of course, I could have changed the array result to [1] instead of [0] [which is working] but it seems that Dell does that randomly and that I deal with a lot of computer models.
      Thanks for the help you can provide, 
    • nacerbaaziz
      By nacerbaaziz
      please i need to link a progress bar with a time can you help me?
      i want to set a progress bar for 10 sec
      am waiting for your answers
      thank you.
    • odaylton
      By odaylton
      Translated by Google. Sorry for any mistakes.
      Sorry for creating this post based on other great creations but after 15 days searching for a solution to a problem beast I solved them.
      Maybe someone new like me can take advantage of this solution for their programs without wasting so much time
      I want to leave here my thanks to the 2 post that saved my life
      First the Yashied for the excellent work in the article:

      And commenting on improvements to the _FileGetShortcutEX function Commented on by

      Here is my list linking the 2 articles
      #Include <GUIConstantsEx.au3> #Include <GUIImageList.au3> #Include <GUITreeView.au3> #Include <TreeViewConstants.au3> #Include <WindowsConstants.au3> #Include <WinAPIEx.au3> #include "_FileGetShortcutEX.au3" Opt('MustDeclareVars', 1) Global $hForm, $hTreeView, $hImageList, $hItem = 0, $hSelect = 0, $hInput, $Input, $Dummy1, $Dummy2 ;novas linhas <<<<< Global $hInput2, $Input2 Global $X, $Y, $sPath, $sRoot = @ProgramsCommonDir; mudado para o projeto @HomeDrive $hForm = GUICreate('MyGUI', 600, 600) $Input = GUICtrlCreateInput('', 20, 20, 560, 19) $hInput = GUICtrlGetHandle(-1) GUICtrlSetState(-1, $GUI_DISABLE) ;novas linhas <<<<< $Input2 = GUICtrlCreateInput('', 20, 40, 560, 19) $hInput2 = GUICtrlGetHandle(-1) ;GUICtrlSetState(-1, $GUI_DISABLE) ;$hTreeView = _GUICtrlTreeView_Create($hForm, 20, 50, 560, 530, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE) ;_GUICtrlTreeView_SetHeight($hTreeView, 18) GUICtrlCreateTreeView(20, 90, 560, 530, -1, $WS_EX_CLIENTEDGE) $hTreeView = GUICtrlGetHandle(-1) $Dummy1 = GUICtrlCreateDummy() $Dummy2 = GUICtrlCreateDummy() If _WinAPI_GetVersion() >= '6.0' Then _WinAPI_SetWindowTheme($hTreeView, 'Explorer') EndIf $hImageList = _GUIImageList_Create(16, 16, 5, 1) _GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 3) _GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 4) _GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 51) _GUICtrlTreeView_SetNormalImageList($hTreeView, $hImageList) $sRoot = StringRegExpReplace(FileGetLongName($sRoot), '\\+\Z', '') $sPath = StringRegExpReplace($sRoot, '^.*\\', '') If StringInStr($sPath, ':') Then $sRoot &= '\' $sPath &= '\' EndIf If _WinAPI_PathIsDirectory($sRoot) Then $hItem = _GUICtrlTreeView_AddChild($hTreeView, 0, $sPath, 0, 0) If FileClose(FileFindFirstFile($sRoot & '\*')) Then _GUICtrlTreeView_AddChild($hTreeView, $hItem, '', 2, 2) EndIf EndIf GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') GUISetState() If $hItem Then _GUICtrlTreeView_Expand($hTreeView, $hItem) EndIf While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $Dummy1 ; Update GUISetCursor(1, 1) _TVUpdate($hTreeView, GUICtrlRead($Dummy1)) GUISetCursor(2, 0) Case $Dummy2 ; Menu $hItem = GUICtrlRead($Dummy2) $sPath = _TVGetPath($hTreeView, $hItem, $sRoot) ConsoleWrite('-------------------------------' & @CR) ConsoleWrite('Handle: ' & $hItem & @CR) ConsoleWrite('Path: ' & $sPath & @CR) If _WinAPI_PathIsDirectory($sPath) Then ConsoleWrite('Type: ' & 'Directory' & @CR) Else ConsoleWrite('Type: ' & 'File' & @CR) EndIf ConsoleWrite('X: ' & MouseGetPos(0) & @CR) ConsoleWrite('Y: ' & MouseGetPos(1) & @CR) ConsoleWrite('-------------------------------' & @CR) EndSwitch WEnd Func _TVGetPath($hTV, $hItem, $sRoot) Local $Path = StringRegExpReplace(_GUICtrlTreeView_GetTree($hTV, $hItem), '([|]+)|(\\[|])', '\\') If Not $Path Then Return '' EndIf If Not StringInStr($Path, ':') Then Return StringRegExpReplace($sRoot, '(\\[^\\]*(\\|)+)\Z', '\\') & $Path EndIf Return $Path EndFunc ;==>_TVGetPath Func _TVSetPath($hTV, $hItem, $sRoot) ;16/11/24 incerindo variaveis pra detalhamento Local $Arq = _TVGetPath($hTV, $hItem, $sRoot) Local $sDir_Name = StringRegExpReplace($Arq, "(^.*\\)(.*)", "\1") Local $NomeArq = StringRegExpReplace($Arq, "^.*\\", "") Local $NomeArqPos = StringInStr($NomeArq, ".", 0, -1);localiza o ponto da extenso if $NomeArqPos Then Local $NomeArq2 = StringLeft($NomeArq, $NomeArqPos - 1) Local $ArqTipo = StringTrimLeft($NomeArq, $NomeArqPos ) if $ArqTipo="lnk" Then Local $t=_FileGetShortcutEX($Arq);FileGetShortcut($Arq) if $t[2]<> "" Then $t[0] &= "' '"&$t[2] EndIf GUICtrlSetData($Input2,"'"&$t[0] &"'") EndIf Else Local $NomeArq2=$NomeArq Local $ArqTipo='' EndIf Local $sDOS_Dir = FileGetShortName($sDir_Name, 1) GUICtrlSetData($Input, _WinAPI_PathCompactPath($hInput,$Arq, 554)) $hSelect = $hItem EndFunc ;==>_TVSetPath Func _TVUpdate($hTV, $hItem) Local $hImageList = _SendMessage($hTV, $TVM_GETIMAGELIST) Local $Path = StringRegExpReplace(_TVGetPath($hTV, $hItem, $sRoot), '\\+\Z', '') Local $hIcon, $hSearch, $hSubitem Local $Index, $File _WinAPI_LockWindowUpdate($hTV) _GUICtrlTreeView_Delete($hTV, _GUICtrlTreeView_GetFirstChild($hTV, $hItem)) $hSearch = FileFindFirstFile($Path & '\*') If $hSearch = -1 Then If Not @error Then If FileExists($Path) Then ; If _WinAPI_PathIsDirectory($Path) Then ; ; Access denied ; EndIf Else _GUICtrlTreeView_Delete($hTV, $hItem) _WinAPI_LockWindowUpdate(0) Return 0 EndIf EndIf Else While 1 $File = FileFindNextFile($hSearch) If @error Then ExitLoop EndIf If @extended Then $hSubItem = _GUICtrlTreeView_AddChild($hTV, $hItem, $File, 0, 0) If FileClose(FileFindFirstFile($Path & '\' & $File & '\*')) Then _GUICtrlTreeView_AddChild($hTV, $hSubItem, '', 2, 2) EndIf EndIf WEnd FileClose($hSearch) EndIf $hSearch = FileFindFirstFile($Path & '\*') If $hSearch = -1 Then Else While 1 $File = FileFindNextFile($hSearch) If @error Then ExitLoop EndIf If Not @extended Then $hIcon = _WinAPI_ShellExtractAssociatedIcon($Path & '\' & $File, 1) $Index = _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon) _GUICtrlTreeView_AddChild($hTV, $hItem, $File, $Index, $Index) _WinAPI_DestroyIcon($hIcon) EndIf WEnd FileClose($hSearch) EndIf _GUICtrlTreeView_SetItemParam($hTV, $hItem, 0x7FFFFFFF) _WinAPI_LockWindowUpdate(0) Return 1 EndFunc ;==>_TVUpdate Func _WinAPI_LockWindowUpdate2($hWnd) Local $Ret = DllCall('user32.dll', 'int', 'LockWindowUpdate', 'hwnd', $hWnd) If (@error) Or (Not $Ret[0]) Then Return SetError(1, 0, 0) EndIf Return 1 EndFunc ;==>_WinAPI_LockWindowUpdate ;AutoIt_Debugger_Command:Disable_Debug Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) Local $tNMTREEVIEW = DllStructCreate($tagNMTREEVIEW, $lParam) Local $hItem = DllStructGetData($tNMTREEVIEW, 'NewhItem') Local $iState = DllStructGetData($tNMTREEVIEW, 'NewState') Local $hTV = DllStructGetData($tNMTREEVIEW, 'hWndFrom') Local $ID = DllStructGetData($tNMTREEVIEW, 'Code') Local $tTVHTI, $tPoint Switch $hTV Case $hTreeView Switch $ID ;AutoIt_Debugger_Command:Enable_Debug Case $TVN_ITEMEXPANDEDW If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then _GUICtrlTreeView_Delete($hTV, $hItem) If BitAND($iState, $TVIS_SELECTED) Then _TVSetPath($hTV, _GUICtrlTreeView_GetSelection($hTV), $sRoot) EndIf Else If Not BitAND($iState, $TVIS_EXPANDED) Then _GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 0) _GUICtrlTreeView_SetImageIndex($hTV, $hItem, 0) Else _GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 1) _GUICtrlTreeView_SetImageIndex($hTV, $hItem, 1) If Not _GUICtrlTreeView_GetItemParam($hTV, $hItem) Then GUICtrlSendToDummy($Dummy1, $hItem) EndIf EndIf EndIf Case $TVN_SELCHANGEDW If BitAND($iState, $TVIS_SELECTED) Then If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then _GUICtrlTreeView_Delete($hTV, $hItem) $hItem = _GUICtrlTreeView_GetSelection($hTV) EndIf If $hItem <> $hSelect Then _TVSetPath($hTV, $hItem, $sRoot) EndIf EndIf Case $NM_RCLICK $tPoint = _WinAPI_GetMousePos(1, $hTV) $tTVHTI = _GUICtrlTreeView_HitTestEx($hTV, DllStructGetData($tPoint, 1), DllStructGetData($tPoint, 2)) $hItem = DllStructGetData($tTVHTI, 'Item') If BitAND(DllStructGetData($tTVHTI, 'Flags'), $TVHT_ONITEM) Then _GUICtrlTreeView_SelectItem($hTreeView, $hItem) If Not FileExists(_TVGetPath($hTV, $hItem, $sRoot)) Then _GUICtrlTreeView_Delete($hTV, $hItem) $hItem = _GUICtrlTreeView_GetSelection($hTV) Else GUICtrlSendToDummy($Dummy2, $hItem) EndIf If $hItem <> $hSelect Then _TVSetPath($hTV, $hItem, $sRoot) EndIf EndIf ;AutoIt_Debugger_Command:Disable_Debug EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY ;AutoIt_Debugger_Command:Enable_Debug  
    • 31290
      By 31290
      Hi everyone, 
      Following this page:
      I have to download the BIOS for the E5440 model. As I don't know in which order Dell will place the description and the link to download the file (also because the 5440 can be put in first) I'd like to make  the following piece of code to search only first the link that deals with the E5440:
      (The only thing I'm sure is that the string E5440 will be either in the first or the second position and I need to download the corresponding link)
      Local $sRead = InetRead('http://downloads.dell.com/published/pages/latitude-e5450-laptop.html', 1) ConsoleWrite(BinaryToString($sRead) & @CRLF) Local $aData = _StringBetween(BinaryToString($sRead), 'Type: BIOS', 'Category:') TrayTip("", "Downloading latest BIOS Version, please wait...", 10, 3) Local $aLinks = _StringBetween($aData[0], '<A HREF="/', '"><IMG') For $i = 0 To UBound($aLinks) - 1 $aLinks[$i] = 'http://downloads.dell.com/' & $aLinks[$i] Next Local $hDownload = InetGet($aLinks[0], @DesktopDir & "\BIOSE5450.exe", $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND) Do Sleep(250) Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE) Local $iBytesSize = InetGetInfo($hDownload, $INET_DOWNLOADREAD) Local $iFileSize = Round(FileGetSize(@DesktopDir & "\BIOSE5450.exe") / 1048576) InetClose($hDownload) MsgBox(0, "", "The latest E5450 BIOS (" & $iFileSize & " MB) have been downloaded")
      I was thinking of an string search in the array but all my attempts failed for now.
      Can someone could give me a hand over here.
    • ur
      By ur
      I wrote below code to delete a file.
      But when I am trying to delete a shortcut, it it deleting target file instead of shortcut when I created a context menu entry for this executable.
      The executable is working fine if we use it normally instead of context menu.But with COntext menu it is taking the target instead of shortcut.
      By using the below registries.
      Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Delete Permanently\command] @="\"C:\\Program Files\\Permanent Delete\\Delete Permanently.exe\" \"%1\"" And compiled the below code to executable.
      DeleteFile($CmdLine[$i]) ; Adding the path through CMD line argument. Func DeleteFile($sFilePath) Local $iDelete = FileDelete($sFilePath) ; Display a message of whether the file was deleted. If $iDelete Then MsgBox($MB_ICONINFORMATION, "Deleting ...", "The file was successfuly deleted: " & $sFilePath,10) Else MsgBox($MB_ICONERROR, "Deleting ...", "An error occurred while deleting the file: " & $sFilePath & @CRLF & "Please check whether it is in use by any process",10) EndIf EndFunc  

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.