Skript pro vytvoření zástupce z příkazového řádku ve windows

VB Skript níže vytvoří zástupce.

Je třeba uložit do souboru .wsf – například mymk_shortcut.wsf . Poté nakopírovat do windows\system32 (nebo jinam kam páte proměnnou PATH).

Použití

mymk_shortcut /A /R „C:\zde bude\vytvoren\zastupce“ „C:\obecne\moje.pdf“

Po spuštění bude v „C:\zde bude\vytvoren\zastupce“ vytvořen soubor „moje.lnk“ jenž je zástupce pro „C:\obecne\moje.pdf“.

Pokud bude vynechán parametr /R bude jméno zástupce „moje.pdf.lnk“.

Pokud bude vynechán parametr /A tak se skript zeptá na jméno zástupce před vytvořením (předvyplněno bude „moje.lnk“ nebo „moje.pdf.lnk“).

Ještě má skript parametr /V při jehož použití se zobrazí hláška o úspěšném vytvoření zástupce.

<job>
<script language="VBScript">

  helpTxt = vbNewLine & vbNewLine & "Usage: mymk_shortcut [/A] [/R] [/V] linkDir target " & vbNewLine 
  helpTxt = helpTxt & " linkDir: directory where new link will be created " & vbNewLine 
  helpTxt = helpTxt & " target: path to the file/folder linked " & vbNewLine 
  helpTxt = helpTxt & " /A: Auto - new link name will be automatically created from target - without it user is asked for new link name" & vbNewLine 
  helpTxt = helpTxt & " /R: Remove Extension - remove extension of original filename when creating shortcut filename" & vbNewLine 
  '// helpTxt = helpTxt & " /L: Add .lnk extension - add ".lnk" extension to original file name when creating shortcut filename" & vbNewLine 
  helpTxt = helpTxt & " /V: Verbose - prints output even on success, by default only on errors "
  If WScript.Arguments.Count < 2 Then  
    MsgBox helpTxt
    Wscript.Quit
  End If

  Set fso = CreateObject("Scripting.FileSystemObject")
  
  '// read required command line parameters 
  linkDir = Trim(WScript.Arguments.Item(WScript.Arguments.Count-2))
  target = Trim(WScript.Arguments.Item(WScript.Arguments.Count-1))
  
  '// remove trailing backslash in linkDir & target if necessary
  If(Right(linkDir,1) = "\") Then
    linkDir = Left(linkDir,Len(linkDir)-1)
  End If
  If(Right(target,1) = "\") Then
    linkDir = Left(target,Len(target)-1)
  End If
  
  '// check first param 
  If NOT fso.FolderExists(linkDir) Then 
    MsgBox "Error: linkDir """ & linkDir & """ must be existing folder" & helpTxt
    Wscript.Quit
  End If

  '// check second param and find out if it is Folder or File 
  If fso.FolderExists(target) Then
    isTargetFolder = 1
  Elseif fso.FileExists(target) Then  
    isTargetFile = 1
  Else
    MsgBox "Error: target """ & target & """ must be existing folder/file" & helpTxt
    Wscript.Quit
  End If

  '// read optional command line parameters
  'target = Trim(WScript.Arguments.Item(WScript.Arguments.Count-1))
  extraOptions = ""
  For i = 0 to WScript.Arguments.Count-3
    extraOptions = extraOptions & Trim(WScript.Arguments.Item(i))
  Next
  extraOptions = UCase(extraOptions)
  extraOptionAuto = InStr(extraOptions,"A")
  extraOptionRemoveExtension = InStr(extraOptions,"R")
  '// extraOptionAddLnkExtension = InStr(extraOptions,"L") '// without .lnk link is not working so option removed
  extraOptionVerbose = InStr(extraOptions,"V")
  'MsgBox "Options: A:"&extraOptionAuto&" R:"&extraOptionRemoveExtension&" V:"&extraOptionVerbose '// DEBUG
    
  '// default link name created from target file name 
  linkName = fso.GetFileName(target)
  
  '// replace extension if set 
  If extraOptionRemoveExtension Then
  	linkName = fso.GetBaseName(linkName)
  End If
  
  '// Add .lnk 
  linkName = linkName & ".lnk"

  '// ask user for name of new link to be created
  '// if file/folder of specified name already exists user is informed and asked to give another name  
  '// if auto parameter is specified user is not asked in first loop
  tmpErrMsg = ""
  Do   
    If extraOptionAuto = 0 Then
      linkName = InputBox(tmpErrMsg & vbNewLine & "Enter the name for new link or press Cancel to end.", "Create Link", linkName)
      If linkName = "" Then
        Wscript.Quit
      End If
    End If
    
    link = linkDir & "\" & linkName
  
    If fso.FileExists(link) OR fso.FolderExists(link) Then
      tmpErrMsg = "Error: File or folder """ & link & """ already exists, choose another."
      link = ""
      extraOptionAuto = 0 '// We have to reset otherwise we will end-up with endless 
    End If
  Loop While link = ""

  targetFolder = fso.GetParentFolderName(target)

  '// MsgBox "target: " & target & vbNewLine & "link: " & link & vbNewLine & "targetFolder: " & targetFolder '// DEBUG
  '// Wscript.Quit '// DEBUG

  Set oWS = WScript.CreateObject("WScript.Shell")
  Set oLink = oWS.CreateShortcut(link)
  oLink.TargetPath = target
  oLink.WorkingDirectory = targetFolder
  '// oLink.Arguments = ""
  '// oLink.Description = "Pole komentar"   
  '//  oLink.HotKey = ""
  '//  oLink.IconLocation = ""
  '//  oLink.WindowStyle = "1"   
  oLink.Save

  '// (if VERBOSE option is specified output is always printed, otherwise only on error)
  If fso.FileExists(link) OR fso.FolderExists(link) Then
    If extraOptionVerbose Then
      MsgBox "Link " & link & " created"
    End If
  Else
    MsgBox "Error: could not create link " & link
  End If
  
</script>
</job>

Zveřejnit odpověď

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *

*