createWindowsShortcut: Creates a Microsoft Windows Shortcut (.lnk file)

Description Usage Arguments Value Required privileges on Windows Author(s) References See Also Examples

Description

Creates a Microsoft Windows Shortcut (.lnk file).

Usage

1
2
## Default S3 method:
createWindowsShortcut(pathname, target, overwrite=FALSE, mustWork=FALSE, ...)

Arguments

pathname

The pathname (with file extension *.lnk) of the link file to be created.

target

The target file or directory to which the shortcut should point to.

overwrite

If TRUE, an existing link file is overwritten, otherwise not.

mustWork

If TRUE, an error is produced if the Windows Shortcut link is not created, otherwise not.

...

Not used.

Value

Returns (invisibly) the pathname.

Required privileges on Windows

In order for this method, which utilizes Windows Script Host a VBScript, to succeed on Windows, the client/R session must run with sufficient privileges (it has been reported that Administrative rights are necessary).

Author(s)

Henrik Bengtsson

References

[1] Create a windows shortcut (.LNK file), SS64.com, https://ss64.com/nt/shortcut.html

See Also

readWindowsShortcut()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Create Windows Shortcut links to a directory and a file
targets <- list(
  system.file(package="R.utils"),
  system.file("DESCRIPTION", package="R.utils")
)

for (kk in seq_along(targets)) {
  cat("Link #", kk, "\n", sep="")

  target <- targets[[kk]]
  cat("Target: ", target, "\n", sep="")

  # Name of *.lnk file
  pathname <- sprintf("%s.LNK", tempfile())

  tryCatch({
    # Will only work on Windows systems with support for VB scripting
    createWindowsShortcut(pathname, target=target)
  }, error = function(ex) {
    print(ex)
  })

  # Was it created?
  if (isFile(pathname)) {
    cat("Created link file: ", pathname, "\n", sep="")

    # Validate that it points to the correct target
    dest <- filePath(pathname, expandLinks="any")
    cat("Available target: ", dest, "\n", sep="")

    res <- all.equal(tolower(dest), tolower(target))
    if (!isTRUE(res)) {
      msg <- sprintf("Link target does not match expected target: %s != %s", dest, target)
      cat(msg, "\n")
      warning(msg)
    }

    # Cleanup
    file.remove(pathname)
  }
}

Example output

Loading required package: R.oo
Loading required package: R.methodsS3
R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.22.0 (2018-04-21) successfully loaded. See ?R.oo for help.

Attaching package: 'R.oo'

The following objects are masked from 'package:methods':

    getClasses, getMethods

The following objects are masked from 'package:base':

    attach, detach, gc, load, save

R.utils v2.7.0 successfully loaded. See ?R.utils for help.

Attaching package: 'R.utils'

The following object is masked from 'package:utils':

    timestamp

The following objects are masked from 'package:base':

    cat, commandArgs, getOption, inherits, isOpen, parse, warnings

Link #1
Target: /usr/lib/R/site-library/R.utils
<simpleError in open.connection(con, open = "rb"): cannot open the connection>
Link #2
Target: /usr/lib/R/site-library/R.utils/DESCRIPTION
<simpleError in open.connection(con, open = "rb"): cannot open the connection>
Warning messages:
1: In open.connection(con, open = "rb") :
  cannot open file '/work/tmp/tmp/Rtmpy448s1/fileb2e5376edd6.LNK': No such file or directory
2: In open.connection(con, open = "rb") :
  cannot open file '/work/tmp/tmp/Rtmpy448s1/fileb2e5376edd6.LNK': No such file or directory
3: closing unused connection 4 (/work/tmp/tmp/Rtmpy448s1/fileb2e5376edd6.LNK) 
4: closing unused connection 3 (/work/tmp/tmp/Rtmpy448s1/fileb2e5376edd6.LNK) 
5: In open.connection(con, open = "rb") :
  cannot open file '/work/tmp/tmp/Rtmpy448s1/fileb2e3bbc7a04.LNK': No such file or directory
6: In open.connection(con, open = "rb") :
  cannot open file '/work/tmp/tmp/Rtmpy448s1/fileb2e3bbc7a04.LNK': No such file or directory

R.utils documentation built on Sept. 26, 2021, 9:07 a.m.