1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Script to search shortcut target box for specific text?

Discussion in 'Software Development' started by martinlest, Oct 12, 2018.

Thread Status:
Not open for further replies.
  1. martinlest

    martinlest Thread Starter

    Joined:
    Nov 11, 2010
    Messages:
    150
    Hi,

    I have been trying to adapt various scripts (I could post them, but as they did not originate with me, and I have not been able to edit them to achieve what I want, that seems a bit pointless!) to achieve a simple task:

    The problem is this: I have various shortcuts on my PC whose targets, for reasons I have not been able to work out, will change ('all by themselves'!) from, say this:

    Code:
    "E:\XP Utilities\data\"
    to this:

    Code:
    "\\Martin-fspc\e\xp utilities\data\"
    (Martin-fspc being, of course, the local PC name). Yes, the PC is on a home network, but I do not access any of the shortcuts except on that local PC . I have no idea why or when this change occurs, but corrected shortcuts tend to revert to the network form after a few weeks or so. Total mystery to me....

    OK, if anyone can advise on that and prevent it happening, that would be great. But what I wanted to do (and ask about here on the forum) , and haven't managed, is to write a batch or vbs script that will search my PC (or selected directories thereof) for any instance of \\Martin-fspc\ in a shortcut target and identify the shortcut containing it. It would be even more welcome if the script changed all instances of, for example
    "\\Martin-fspc\
    to
    "E:\
    whilst leaving the rest of the target information ( XP Utilities\data\" in this example ) intact, but there are not so many - if they can be identified at least, I can change them manually.

    Problem is, in a few weeks I will find they have gone back to the network address, but if I had a script that would at least show any shortcuts on my PC with \\ at the start of the target box, I could keep on top of the problem at least.

    Many thanks for any help,

    Martin
     
    Last edited: Oct 12, 2018
  2. martinlest

    martinlest Thread Starter

    Joined:
    Nov 11, 2010
    Messages:
    150
    OK, this is the existing script I am trying to edit, so that any instance of \\Martin-fspc\e\ in a shortcut target box (even if followed by further subfolders, such as \\Martin-fspc\e\New Folder) will be replaced by E:\.

    At the moment the script window closes as soon as it opens, so I am way off...

    Code:
    @Echo Off
    Call :_MakeFile
    ::::::::::::::::::::::::::::::::::::::::::::::::::
    Set _Search=\\Martin-fspc\e\
    Set _Replace=E:\
    Set _Path=C:\
    Set _LogFile=%~dp0Shortcut_Change_Log.txt
    ::::::::::::::::::::::::::::::::::::::::::::::::::
    For /F "Tokens=* Delims=" %%I In ('Dir "%_Path%\*.lnk" /B /S') Do (
    >>"%_LogFile%" cscript //nologo "%_VBFile%" "%%I" "%_Search%" "%_Replace%"
    >>"%_LogFile%" Echo.
    )
    If Exist "%_VBFile%" Del "%_VBFile%"
    Goto :EOF
    ::::::::::::::::::::::::::::::::::::::::::::::::::
    :: Subroutines
    ::::::::::::::::::::::::::::::::::::::::::::::::::
    :_MakeFile
    Set _VBFile=%Temp%\0.vbs
    (Echo.strShortCut = WScript.Arguments^(0^)
    Echo.strSearch = WScript.Arguments^(1^)
    Echo.strReplace = WScript.Arguments^(2^)
    Echo.Set WshShell = WScript.CreateObject^("WScript.Shell"^)
    Echo.Set objShortcut = WshShell.CreateShortcut^(strShortCut^)
    Echo.strOldPath = objShortcut.TargetPath
    Echo.strNewPath = Replace^(strOldPath, strSearch, strReplace,1,1,1^)
    Echo.If strOldPath ^<^> strNewPath Then
    Echo.  objShortcut.TargetPath = strNewPath
    Echo.  Wscript.Echo "Shortcut Name " ^& strShortCut
    Echo.  Wscript.Echo "Changed Target From " ^& strOldPath ^& " to " ^& strNewPath
    Echo.  strOldPath = objShortcut.WorkingDirectory
    Echo.  strNewPath = Replace^(strOldPath, strSearch, strReplace,1,1,1^)
    Echo.  objShortcut.WorkingDirectory = strNewPath
    Echo.  Wscript.Echo "Changed Start-in From " ^& strOldPath ^& " to " ^& strNewPath
    Echo.End If
    Echo.objShortcut.Save)>"%_VBFile%"
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Tags:
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1217763

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice