Tech Support Guy banner
Status
Not open for further replies.

Script to search shortcut target box for specific text?

Tags
script
666 views 1 reply 1 participant last post by  martinlest 
#1 · (Edited)
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
 
See less See more
#2 ·
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%"
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top