Skip to content

Instantly share code, notes, and snippets.

@takumaw
Last active April 25, 2018 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takumaw/73b5f7ae6588b8c0fe047f9ab8c3d119 to your computer and use it in GitHub Desktop.
Save takumaw/73b5f7ae6588b8c0fe047f9ab8c3d119 to your computer and use it in GitHub Desktop.
Modify Windows shortcut files
#
# Modify Windows shortcut files
#
# Path to find shortcuts from
# e.g.
# [string]$findPath = "\\myserver1\myfolder"
# -> Find shortcuts from "\\myserver1\myfolder".
[string]$findPath = "."
# Old base pat shortcut files are pointing to
[string]$oldTargetBasePath = "C:\"
# New base path shortcut files will be pointing to
[string]$newTargetBasePath = "D:\"
# e.g.
# [string]$oldTargetBasePath = "\\myserver1"
# [string]$newTargetBasePath = "\\myserver2"
# -> All shortcut files pointing to \\myserver1\xxx\yyy\zzz will be redirected to \\myserver2\xxx\yyy\zzz.
# Main
#
$wScriptShell = New-Object -ComObject WScript.Shell
$shortcutFilePaths = Get-ChildItem -Path $findPath -Recurse -Include *.lnk | select -ExpandProperty FullName
Echo "Replacing shortcuts..."
ForEach($shortcutFilePath in $shortcutFilePaths)
{
Echo "$($shortcutFilePath)"
$shortcutFile = $wScriptShell.CreateShortcut($shortcutFilePath)
[string]$targetPath = $shortcutFile.TargetPath
[string]$targetPath = [string]$targetPath.Replace($oldTargetBasePath.tostring(), $newTargetBasePath.ToString())
$shortcutFile.TargetPath = [string]$targetPath
[string]$workingDirectory = $shortcutFile.WorkingDirectory
[string]$workingDirectory = [string]$workingDirectory.Replace($oldTargetBasePath.tostring(), $newTargetBasePath.ToString())
$shortcutFile.WorkingDirectory = [string]$workingDirectory
[string]$iconLocation = $shortcutFile.IconLocation
[string]$iconLocation = [string]$iconLocation.Replace($oldTargetBasePath.tostring(), $newTargetBasePath.ToString())
$shortcutFile.IconLocation = [string]$iconLocation
$shortcutFile.Save()
}
#
# Modify Windows shortcut files
#
import sys
import os
import pywintypes
import win32com.client
# Path to find shortcuts from
FIND_PATH = r"."
# Old base pat shortcut files are pointing to
OLD_TARGET_BASE_PATH = "C:\\"
# New base path shortcut files will be pointing to
NEW_TARGET_BASE_PATH = "D:\\"
def main():
for dirpath, dirnames, filenames in os.walk(FIND_PATH):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
filename_wo_ext, filename_ext = os.path.splitext(filename)
if filename_ext == ".lnk":
print(filepath)
try:
wscript_shell = win32com.client.Dispatch('WScript.Shell')
shortcut = wscript_shell.CreateShortCut(filepath)
shortcut.Targetpath = shortcut.Targetpath.replace(OLD_TARGET_BASE_PATH, NEW_TARGET_BASE_PATH)
shortcut.WorkingDirectory = shortcut.WorkingDirectory.replace(OLD_TARGET_BASE_PATH, NEW_TARGET_BASE_PATH)
shortcut.IconLocation = shortcut.IconLocation.replace(OLD_TARGET_BASE_PATH, NEW_TARGET_BASE_PATH)
shortcut.save()
except pywintypes.com_error as e:
print(e, file = sys.stderr)
sys.stdout.flush()
sys.stderr.flush()
if __name__ == "__main__":
main()
'
' Modify Windows shortcut files
'
' Path to find shortcuts from
strTargetPath = "."
' Old base pat shortcut files are pointing to
strOldPath = "C:\" ' Old path
' New base path shortcut files will be pointing to
strNewPath = "D:\" ' New path
' Main
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTargetFolder = objFSO.GetFolder(strTargetPath)
Wscript.Echo "Replacing shortcuts..."
WalkSubFolders objTargetFolder
Sub WalkSubFolders(objFolder)
For Each objFile In objFolder.Files
if right(objFile.Name, 4)=".lnk" then
' Wscript.Echo objFile.Name
ReplaceShortcut(objFile)
end if
Next
For Each objSubFolder in objFolder.SubFolders
WalkSubFolders objSubFolder
Next
End Sub
Sub ReplaceShortcut(objFile)
Set objWsh = WScript.CreateObject("WScript.Shell")
Set objShortcutFile = objWsh.CreateShortcut(objFile.Path)
objShortcutFile.Arguments = Replace(objShortcutFile.Arguments,strOldPath,strNewPath,1,-1,1)
objShortcutFile.TargetPath = Replace(objShortcutFile.TargetPath,strOldPath,strNewPath,1,-1,1)
objShortcutFile.WorkingDirectory = Replace(objShortcutFile.WorkingDirectory,strOldPath,strNewPath,1,-1,1)
objShortcutFile.Save
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment