Jump to content
Sign in to follow this  
perwhis

Change target of many shortcuts

Recommended Posts

perwhis

The task is to automatically change the target of hundreds of shortcuts, due to changes of server names. I am pretty sure I have seen someone solve this already, but I cannot find the solution any longer, so I gave it a try myself. Unfortunately I am not an experienced AutoIt scripter, so there are some things I still need to sort out. This is what my (messy) script looks like this far:

$old_path = "dokument-2"
$new_path_NT = "pp.tr\dok"
$new_path_9x = "dokument-4"

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.lnk")

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop

    MsgBox(4096, "File:", $file)
    ; Read in the path of a shortcut
    $details = FileGetShortcut($file)
    MsgBox(0, "Path:", $details[0] & @CR & $details[1] & @CR & $details[2] & @CR & _
            $details[3] & @CR & $details[4] & @CR & $details[5] & @CR & $details[6])

    ; Check if shortcut contains old path and replace with new
    If StringInStr($details[0], $old_path) And @OSTYPE = "WIN32_NT" Then
        StringReplace($details[0], $old_path, $new_path_NT)
        If StringInStr($details[1], $old_path) And @OSTYPE = "WIN32_NT" Then
            StringReplace($details[1], $old_path, $new_path_NT)
        EndIf
        ; Send current shortcut to recycle bin if contains path to change
        MsgBox(0, "Delete", $file)
        ;   FileRecycle($file)
        
        ; Create new shortcut with new path
        ;   FileCreateShortcut($details[0], $file, $details[1])")
    EndIf

WEnd

; Close the search handle
FileClose($search)

1. At the moment the script only looks for shortcuts in the current folder. How do I make it search all shortcuts in a harddisk or in a server?

2. I'm insecure about how FileCreateShortcut works. Can I somehow give the full $details-array to preserve all contents? If so, what should it look like?

3. I need to give different paths depending on OS-type, and I feel I do it in a very clumsy way. Any suggestions on better ways to do it? (I've only put NT in code yet, but will repeat it with a different if-statement.)

Cheers!

Share this post


Link to post
Share on other sites
Authenticity

You can do something like this instead of deleting the file:

Dim $oShell = ObjCreate('WScript.Shell.1')
Dim $oShortcut
Dim $sPath = @DesktopDir
Dim $hFile = FileFindFirstFile($sPath & '\*.lnk')
Dim $sFile

If $hFile <> -1 Then
    While 1
        $sFile = FileFindNextFile($hFile)
        If @error Then ExitLoop
        $oShortcut = $oShell.CreateShortcut($sFile)
        ConsoleWrite($oShortcut.TargetPath & @LF)
    WEnd
    FileClose($hFile)
EndIfoÝ÷ ÚØ^©eʧyçb²Ú²È ©ÞÂÖ«ëijØm¢Ø^MªàzÓÚ¶k¢«·&§vë¶j÷¦zØhvíä¡¢»ºÚç-)Ó~¨J+µË­I«ÞN(ا¹æ§vÇ­Èm秶*Þv*ÞrÚ+Ë*.綬z·º»"¢{j·!¶Å©©ä®*m~îØ^­æ«zf§ÉìZ^²)©éا+Së2²×¦ÉèµÈZ§íçåÊZ%²¨º;¬¶wvÚyÉZ­ëazö«¦åz%v)ඩjØZqÊ+v)à¶az^­«b¢{2²×¦Ýý²»§)à¢{¨Ç¿ºX¤y«­¢+Ù¥´ÀÌØíÍAÑ¡Q½IÁ±()%=MQåÁôÅÕ½Ðí]%8ÌÉ}9PÅÕ½ÐìQ¡¸(ÀÌØíÍAÑ¡Q½IÁ±ôÅÕ½ÐíÁÀ¹ÑÈÀäÈí½¬ÅÕ½Ðì)±Í(ÀÌØíÍAÑ¡Q½IÁ±ôÅÕ½Ðí½­Õµ¹Ð´ÐÅÕ½Ðì)¹%

Share this post


Link to post
Share on other sites
lordicast

I use this copy multiple files ...

#include <File.au3>

Dim $File = @ScriptDir & '\find.txt'
Dim $Where

$Dir = InputBox('Original File Place','Where are the files to move?','C:\log\')
$NewLocation = InputBox('New Location','Where would you like to store new files?','C:\NewLocation\New\')
$uExt = InputBox('The Extenstion','What is the extenstion for the files we are moving?','txt')
$loc = '*.'&$uExt

RunWait(@ComSpec & ' /c ' & 'dir "' & $Dir &$loc& '" /a :h /b /s' & ' > "' & @ScriptDir & '\find.txt"', '', @SW_HIDE)

_FileReadToArray($File, $where)

For $x = 1 to $where[0]
    if StringInStr($where[$x],'.'&$uExt,0) = 0 Then
        sleep(100)
    Else
    $Ext = StringRight($where[$x],4)
    $In = StringInStr($where[$x],'\',0,-1)
    $Len = StringLen($where[$x])
    $Out = $len - $in
    $Name = StringMid($where[$x],$In+1,$Out-4)
    $Final = $NewLocation & $name&$Ext
    FileCopy($where[$x], $Final, 9)
    EndIf
Next

FileDelete(@ScriptDir & '\find.txt')
MsgBox(0,'','Complete')

[Cheeky]Comment[/Cheeky]

Share this post


Link to post
Share on other sites
mrmacadamia

The task is to automatically change the target of hundreds of shortcuts, due to changes of server names. I am pretty sure I have seen someone solve this already, but I cannot find the solution any longer, so I gave it a try myself. Unfortunately I am not an experienced AutoIt scripter, so there are some things I still need to sort out. This is what my (messy) script looks like this far:

$old_path = "dokument-2"
$new_path_NT = "pp.tr\dok"
$new_path_9x = "dokument-4"

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.lnk")

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop

    MsgBox(4096, "File:", $file)
    ; Read in the path of a shortcut
    $details = FileGetShortcut($file)
    MsgBox(0, "Path:", $details[0] & @CR & $details[1] & @CR & $details[2] & @CR & _
            $details[3] & @CR & $details[4] & @CR & $details[5] & @CR & $details[6])

    ; Check if shortcut contains old path and replace with new
    If StringInStr($details[0], $old_path) And @OSTYPE = "WIN32_NT" Then
        StringReplace($details[0], $old_path, $new_path_NT)
        If StringInStr($details[1], $old_path) And @OSTYPE = "WIN32_NT" Then
            StringReplace($details[1], $old_path, $new_path_NT)
        EndIf
        ; Send current shortcut to recycle bin if contains path to change
        MsgBox(0, "Delete", $file)
        ;   FileRecycle($file)
        
        ; Create new shortcut with new path
        ;   FileCreateShortcut($details[0], $file, $details[1])")
    EndIf

WEnd

; Close the search handle
FileClose($search)

1. At the moment the script only looks for shortcuts in the current folder. How do I make it search all shortcuts in a harddisk or in a server?

2. I'm insecure about how FileCreateShortcut works. Can I somehow give the full $details-array to preserve all contents? If so, what should it look like?

3. I need to give different paths depending on OS-type, and I feel I do it in a very clumsy way. Any suggestions on better ways to do it? (I've only put NT in code yet, but will repeat it with a different if-statement.)

Cheers!

geez, FileGetShorcut really give me an headache.

I don't understand why certain shortcut can be read and certain cannot and always give me an error "non-array..."

I tried your code to test on my computer and give me "Subscript used with non-Array variable." error.

Something wrong would happen if you want to use it with loop condition. I don't know why.

Then I tried run from help file to test and it worked but when i change to read different shortcut, it will give me "Subscript used with non-Array variable."

Share this post


Link to post
Share on other sites
Ascend4nt

@mrmacadamia

Try _FileGetShortcutEx(). Also see 'LNK (Windows Shortcut Disassembler)' - in my signature.

There's a bunch of other 'special' type of LNK shortcuts - I had further expanded the _FileGetShortcutEx code myself (not uploaded yet), to read those containing URLs (which is stupid, since they could have been put in a .URL file), UNC paths to computers, and shortcuts containing single GUIDs (shortcut to 'My Computer', for example).

You can have shortcuts to crazy things that are a bit more difficult to figure out - control panel items, printers, etc... these are generally ones I skip. (you have to parse through Shell Item ID's and whatnot - blech)

Anyway, hope it helps.

Share this post


Link to post
Share on other sites
rudi

1. At the moment the script only looks for shortcuts in the current folder. How do I make it search all shortcuts in a harddisk or in a server?

Use _FileListToArrayFaster1e.au3 (by randallc, modified from SolidSnake, big_daddy, SmoKE_N, GEOsoft)

2. I'm insecure about how FileCreateShortcut works.

Just play around with the example, that you will find in the help files.

3. I need to give different paths depending on OS-type, and I feel I do it in a very clumsy way. Any suggestions on better ways to do it? (I've only put NT in code yet, but will repeat it with a different if-statement.)

Just curious: Why do you want to point different OS' to different paths?

Select
    Case @OSVersion = "Win_XP"
        $newpath = "NewPathXP"
    Case @OSVersion = "Win_2000"
        $newpath = "NewPath2000"
    Case @OSVersion = "Win_Vista"
        $newpath = "NewPathVista"
    Case Else
        MsgBox(0, "Error", "Your OS is '" & @OSVersion & "' and is currently not covered by this script")
EndSelect

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
mrmacadamia

@mrmacadamia

Try _FileGetShortcutEx(). Also see 'LNK (Windows Shortcut Disassembler)' - in my signature.

There's a bunch of other 'special' type of LNK shortcuts - I had further expanded the _FileGetShortcutEx code myself (not uploaded yet), to read those containing URLs (which is stupid, since they could have been put in a .URL file), UNC paths to computers, and shortcuts containing single GUIDs (shortcut to 'My Computer', for example).

You can have shortcuts to crazy things that are a bit more difficult to figure out - control panel items, printers, etc... these are generally ones I skip. (you have to parse through Shell Item ID's and whatnot - blech)

Anyway, hope it helps.

Thanks for your response.

Besides, I've already search the forum and I've already test with your wonderful udf. It works with many shortcut by now but the problem is when I want use with FileListToArray, FileFindFirstFile and For...Next with so many shortcuts and still, the "non-array..." error exists.

So I think I had to use FileGetShortcut for each shortcut like this

if FileGetShortcut then
   filedelete...
endif

if FileGetShortcut then
   filedelete...
endif

if FileGetShortcut then
   filedelete...
endif

if FileGetShortcut then
   filedelete...
endif

Share this post


Link to post
Share on other sites
Ascend4nt

@'mrmacadamia: I hope you really understand all those functions, because the 'like this' code isn't at all how you test for whether FileGetShortcut worked. Also - are you verifying the shortcuts that fail by hand, to see if your code logic is working correctly? I'd do that before you go deleting files!

Share this post


Link to post
Share on other sites
mrmacadamia

@'mrmacadamia: I hope you really understand all those functions, because the 'like this' code isn't at all how you test for whether FileGetShortcut worked. Also - are you verifying the shortcuts that fail by hand, to see if your code logic is working correctly? I'd do that before you go deleting files!

I'm sorry because i don't have the code in my hand by the time i'm posting the previous post.

I'll post the code when I get back.

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
Sign in to follow this  

×

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.