China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 07:02
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Completed] Help me correct the VBS code to open the location of the shortcut View 2,660 Replies 25
Original Poster Posted 2009-07-29 05:32 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline

set args = WScript.Arguments
linkname = args(0)
set wshshell = CreateObject("WScript.Shell")
set scut = wshshell.CreateShortcut(linkname)
set fs = CreateObject("Scripting.FileSystemObject")
folder = """" & fs.GetParentFolderName(scut.TargetPath) & """"
wshshell.Run(folder)

This is a VBScript code. There is a BUG in it. The attached file is the installation and uninstallation BAT, which includes the VBScript code.

This is a program to add the right-click "Open containing folder" to a shortcut. The function is to open the corresponding directory of the shortcut when right-clicking.

It is similar to Right-click Properties - Find Target, but the difference is that you don't need to view the properties, and Find Target selects the target, while Open containing folder directly opens the corresponding folder of the shortcut without selecting the target.



The problem I raised is that there is a BUG when this VBS runs. In fact, this VBS is widely spread on the Internet. I accidentally discovered it and hope brothers can solve the problem and fix the BUG to improve the program.

For example: I have a Maxthon shortcut on the desktop, and the corresponding program is E:\Maxthon\Maxthon.exe
If there is also the program E:\Maxthon.exe (that is, the file maxthon.exe exists in the E drive)

Then when right-clicking the maxthon to open the containing folder, it is running E:\Maxthon.exe. Of course, if there is no E:\Maxthon.exe, the function can be realized. This is the BUG.


Hope everyone can fix it, so that even if the file E:\Maxthon.exe exists, it can still enter the corresponding folder of the shortcut normally.

[ Last edited by kidzgy on 2009-8-7 at 08:56 ]
Attachments
打开所在位置插件.rar (1.25 KiB, Downloads: 11)
Floor 2 Posted 2009-07-29 12:29 ·  中国 江西 赣州 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
Another code:
(Source: http://bbs.360safe.com/viewthread.php?tid=99058&page=1#pid352070)
dim ws,objArgs,strPath
Set objArgs = WScript.Arguments
if objArgs.Count>0 then
strPath=mid(objArgs(0),instr(objArgs(0),"/")+1,len(objArgs(0))-instr(objArgs(0),"/"))
end if
set ws=CreateObject("Wscript.Shell")
ws.run "Explorer /select, " & GetTargetPath(strPath)

Function GetTargetPath(LinkName)
On Error Resume Next
Dim Shortcut
Set Shortcut = CreateObject("Wscript.Shell").CreateShortcut(LinkName)
GetTargetPath = Shortcut.TargetPath
Shortcut.Save
Set Shortcut = Nothing
End Function
Floor 3 Posted 2009-07-29 20:36 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
Thank you very much for your code. Your function is exactly equivalent to finding the target.

And what I need is to open the path where the shortcut is located without selecting the target, just like clicking the mouse once. There is the "Find Target", what I want is not, and it is best to maximize the window.
Floor 4 Posted 2009-07-31 07:11 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Sweat~

Combine below...

wshshell.Run(folder)
changed to
wshshell.Run("explorer " & folder)
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 5 Posted 2009-07-31 08:55 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
Wow, no way, it's that simple.

Thanks a lot. Then the new code is

set args = WScript.Arguments
linkname = args(0)
set wshshell = CreateObject("WScript.Shell")
set scut = wshshell.CreateShortcut(linkname)
set fs = CreateObject("Scripting.FileSystemObject")
folder = """" & fs.GetParentFolderName(scut.TargetPath) & """"
wshshell.Run("explorer " & folder)
Floor 6 Posted 2009-07-31 09:09 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
I don't know what benefits there are if it's not selected...
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 7 Posted 2009-08-01 07:58 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
It's like this. If there are a lot of files in a folder and you select one, it will go to that file, but the scroll bar is not at the top. If you don't select, it's like double-clicking the folder and just entering, which is a faster way to browse.
Floor 8 Posted 2009-08-04 08:39 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
By the way, a new problem has emerged. When there are double quotes at the beginning and end of the "Target" and "Start in" properties of the shortcut, the location cannot be opened. Then, how should this be solved?
Floor 9 Posted 2009-08-04 09:37 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
folder = """" & fs.GetParentFolderName(scut.TargetPath) & """"

Because you added it again...

First, check if there are quotes already. If not, add them
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 10 Posted 2009-08-04 21:15 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
The code modified by floor 5 pops up such a prompt. Then how to modify it?



[ Last edited by kidzgy on 2009-8-6 at 08:18 ]
Floor 11 Posted 2009-08-04 21:16 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
I tried to subtract one double quote or two from both the beginning and the end as you said, but it didn't work, and there's still such a prompt.
Floor 12 Posted 2009-08-05 01:39 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
wshshell.Run(folder)
Before
msgbox folder you'll know your modification is wrong
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 13 Posted 2009-08-05 05:46 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
After I added the msgbox folder, running without double quotes pops up with double quotes at the beginning and end.

But running with double quotes at the beginning and end is still the prompt from post 10. The reason might be ending with.lnk or.url.

Thus, the problem is how to make VBS support shortcuts with double quotes.



[ Last edited by kidzgy on 2009-8-6 at 08:20 ]
Floor 14 Posted 2009-08-05 07:45 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Don't add quotation marks to the ones that already have quotation marks...
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 15 Posted 2009-08-05 08:19 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
19-year member
UID 93679
Gender Male
Status Offline
Please provide the content that needs to be translated, including the Chinese text and any relevant code blocks if applicable. Currently, the input only has the Chinese request without the specific code or content from the 5th floor that needs to be translated. Please provide the full relevant content for translation.
Forum Jump: