perwhis Posted May 4, 2009 Share Posted May 4, 2009 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: expandcollapse popup$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! Link to comment Share on other sites More sharing options...
Authenticity Posted May 4, 2009 Share Posted May 4, 2009 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Á±ôÅÕ½Ðí½Õµ¹Ð´ÐÅÕ½Ðì)¹% Link to comment Share on other sites More sharing options...
lordicast Posted May 4, 2009 Share Posted May 4, 2009 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] Link to comment Share on other sites More sharing options...
mrmacadamia Posted July 30, 2009 Share Posted July 30, 2009 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: expandcollapse popup$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." Link to comment Share on other sites More sharing options...
Ascend4nt Posted July 30, 2009 Share Posted July 30, 2009 @mrmacadamiaTry _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. My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
rudi Posted July 30, 2009 Share Posted July 30, 2009 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! Link to comment Share on other sites More sharing options...
mrmacadamia Posted July 31, 2009 Share Posted July 31, 2009 @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 Link to comment Share on other sites More sharing options...
Ascend4nt Posted July 31, 2009 Share Posted July 31, 2009 @'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! My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
mrmacadamia Posted August 4, 2009 Share Posted August 4, 2009 @'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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now