3 Replies - 7921 Views - Last Post: 06 February 2012 - 12:48 PM Rate Topic: -----

#1 anirelles  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 01-January 09

Create folder shortcut in vbscript

Posted 04 February 2012 - 07:08 AM

Hi,

I would like to be able to create a folder shortcut programmatically but I can't achieve it.
I'm asking for help to VB experts. I would like to do it by vbscript. For precision a folder
shortcut is a shortcut that opens to the directory it points to when we double-click on it.
I need to make it works on windows vista and windows seven.

With my researches I was able to create a shortcut to an .exe with this code :

Dim Shell, DesktopPath, lnk
Set Shell = CreateObject("WScript.Shell")
DesktopPath = Shell.SpecialFolders("Desktop")
Set lnk = Shell.CreateShortcut(DesktopPath & "\Vlc.lnk")
lnk.TargetPath = "C:\Program Files\VideoLAN\VLC\vlc.exe"
lnk.Save


It works fine without any problem but it's a shortcut to a file. If I change the instruction

lnk.TargetPath = "C:\Program Files\VideoLAN\VLC\vlc.exe"



with

lnk.TargetPath = "C:\Program Files\VideoLAN\VLC"



it doesn't create a folder shortcut as I was expecting


On the web I found someone explaining that to do that I need a folder with the file desktop.ini in it:

[.ShellClassInfo]
CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
Flags=2



And also a shortcut file target.lnk created with the command

Dim Shell, lnk
Set Shell = CreateObject("WScript.Shell")
Set lnk = Shell.CreateShortcut("c:\shortcut_folder\target.lnk")
lnk.TargetPath = "C:\Program Files\VideoLAN\VLC"
lnk.Save


and then apply this command to the folder
attrib +s c:\shortcut_folder

It didn't work and it seems complex to me just to create a folder shortcut.

It would be great help a script working for what I want

Good weekend and thank you for reading.

This post has been edited by anirelles: 04 February 2012 - 07:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Create folder shortcut in vbscript

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,776
  • Joined: 26-March 09

Re: Create folder shortcut in vbscript

Posted 04 February 2012 - 02:29 PM

Have a look at this link to see if it helps.
Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Create folder shortcut in vbscript

Posted 05 February 2012 - 10:05 AM

googling "vbscript create shortcut" reveals any number of examples, the first of which is this one:
Set WshShell = CreateObject("WScript.Shell")
strDesktopPath = WshShell.SpecialFolders("Desktop")

Set objShortcutLnk = WshShell.CreateShortcut(strDesktopPath & "\notepad.lnk")
objShortcutLnk.TargetPath = "notepad"
objShortcutLnk.Save
This one creates a shortcut to notepad.
Was This Post Helpful? 0
  • +
  • -

#4 anirelles  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 01-January 09

Re: Create folder shortcut in vbscript

Posted 06 February 2012 - 12:48 PM

Thanks for you help. I figured out what was the problem.
I was calling this vbscript code in java code with :

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

  public static void main(String[] args) throws IOException {

    String script = "Set sh = CreateObject(\"WScript.Shell\")"
        + "\nSet shortcut = sh.CreateShortcut(\"C:/Users/toto828/Desktop/Shortcut.lnk\")"
        + "\nshortcut.TargetPath = \"C:/Program Files/VideoLAN/VLC\""
        + "\nshortcut.Save";
    
    File file = new File("C:/Users/toto828/Desktop/temp.vbs");
    FileOutputStream fo = new FileOutputStream(file);
    fo.write(script.getBytes());
    fo.close();
    Runtime.getRuntime().exec("wscript.exe " + file.getAbsolutePath() );
    
  }
  
}



and changing to

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

  public static void main(String[] args) throws IOException {

    String script = "Set sh = CreateObject(\"WScript.Shell\")"
        + "\nSet shortcut = sh.CreateShortcut(\"C:\\Users\\toto828\\Desktop\\Shortcut.lnk\")"
        + "\nshortcut.TargetPath = \"C:/Program Files/VideoLAN/VLC\""
        + "\nshortcut.Save";
    
    File file = new File("C:/Users/toto828/Desktop/temp.vbs");
    FileOutputStream fo = new FileOutputStream(file);
    fo.write(script.getBytes());
    fo.close();
    Runtime.getRuntime().exec("wscript.exe " + file.getAbsolutePath() );
    
  }
  
}



made it work.

This post has been edited by anirelles: 06 February 2012 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1