Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
sexy_andrea2006 pe Simpatie.ro
Femeie
22 ani
Sibiu
cauta Barbat
22 - 48 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [WINDOWS] Batch Change Paths in Shortcut (LNK) Files Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
TRaP
Moderator

Inregistrat: acum 7 ani
Postari: 822
I switched laptops and on the new laptop I simplified some folders to get shorter paths. Now I have a bunch of LNK files (200 maybe) requiring a change.

Most of them are located in 1 folder on my desktop, but the rest are scattered across various folders. For these, there are 2 methods of updating the paths without having to Alt+Enter each LNK file to get to properties, then manually modifying the "Target" and "Start in" fields.

Batch - PowerShell script that can be run by normal user (not admin)

All shortcut files must be located inside 1 folder (e.g., C:\Users\USERNAME\Desktop\Shortcuts)

1. In the following script, replace the paths to your folders as required

# === CONFIGURE THESE ===
$folderPath = "C:\Users\USERNAME\Desktop\Shortcuts"    # Folder containing your .lnk files
$oldPart = "D:\SOME OLDER FOLDER\SOME OTHER OLDER FOLDER"    # Part of the old path
$newPart = "D:\NEW FOLDER"    # Replacement path

# Load the COM object to handle shortcuts
$shell = New-Object -ComObject WScript.Shell

# Get all .lnk files in the folder (non-recursive, or use -Recurse if needed)
Get-ChildItem -Path $folderPath -Filter *.lnk | ForEach-Object {
    $shortcut = $shell.CreateShortcut($_.FullName)

    $originalTarget = $shortcut.TargetPath
    $originalStartIn = $shortcut.WorkingDirectory

    # Update Target and StartIn if they contain the old path
    if ($originalTarget -like "*$oldPart*") {
        $shortcut.TargetPath = $originalTarget -replace [regex]::Escape($oldPart), $newPart
    }

    if ($originalStartIn -like "*$oldPart*") {
        $shortcut.WorkingDirectory = $originalStartIn -replace [regex]::Escape($oldPart), $newPart
    }

    $shortcut.Save()
}


2. Open a PowerShell window (I opened it directly into the path from variable $folderPath, but anywhere should work

3. Copy the edited script from 1

4. Paste into the PowerShell window

5. Press enter

On-demand - AutoHotkey script, that requires AutoHotkey to be installed

This works on demand for one or several shortcuts (LNK files) that must be selected and then a shortcut key must be pressed.

1. Install AutoHotkey

2. Open Notepad and paste the following code, edited with your desired folder paths

^!+s:: ; Ctrl+Alt+Shift+S triggers this for the currently selected .LNK files

; === CONFIGURE YOUR PATH CHANGES HERE ===
oldPart := "D:\SOME OLDER FOLDER\SOME OTHER OLDER FOLDER"
newPart := "D:\NEW FOLDER"

; log := ""

selectedFiles := GetSelectedFiles()

if (selectedFiles.MaxIndex() = 0) {
    MsgBox, No shortcut files selected!
    return
}

for index, filePath in selectedFiles {
    if (SubStr(filePath, -3) != ".lnk") {
        ; log .= "`nSkipping non-shortcut: " filePath
        continue
    }

    shortcut := ComObjCreate("WScript.Shell").CreateShortcut(filePath)
    target := shortcut.TargetPath
    working := shortcut.WorkingDirectory
    changed := false

    ; log .= "`n`nShortcut: " filePath
    ; log .= "`n  Original Target: " target
    ; log .= "`n  Original Start In: " working

    if InStr(target, oldPart) {
        newTarget := StrReplace(target, oldPart, newPart)
        shortcut.TargetPath := newTarget
        changed := true
        ; log .= "`n  New Target: " newTarget
    }

    if InStr(working, oldPart) {
        newWorking := StrReplace(working, oldPart, newPart)
        shortcut.WorkingDirectory := newWorking
        changed := true
        ; log .= "`n  New Start In: " newWorking
    }

    if (changed) {
        shortcut.Save()
        ; log .= "`n  -> Shortcut updated."
    } else {
        ; log .= "`n  -> No changes made."
    }
}

; Clipboard := log
; MsgBox, Update complete.`nLog has been copied to clipboard.
MsgBox, Selected shortcut(s) updated successfully!
return


; === Helper: Get selected files in Explorer window ===
GetSelectedFiles() {
    files := []
    shellApp := ComObjCreate("Shell.Application")
    hwnd := WinActive("ahk_class CabinetWClass") ; Explorer window
    if !hwnd
        return files

    for window in shellApp.Windows {
        try {
            if (window.hwnd == hwnd) {
                for item in window.Document.SelectedItems {
                    files.Push(item.Path)
                }
                break
            }
        }
    }
    return files
}

If something is not working correctly, you can remove the semicolon (;) from the beginning of the colored lines in the code above and a log text will be copied to clipboard upon running the above code. That should provide troubleshooting information.

3. Save as .ahk extension

4. Double click the ahk file to start it

5. Select a troublesome shortcut and press the assigned shortcut key

Source for both codes:
ChatGPT


pus acum 2 zile
   
Pagini: 1  

Mergi la