R.utils (version 2.0.2)

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

Description

Creates a Microsoft Windows Shortcut (.lnk file).

Usage

## S3 method for class 'default':
createWindowsShortcut(pathname, target, overwrite=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.
...
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).

References

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

See Also

readWindowsShortcut()

Examples

Run this code
# 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, "", sep="")

  target <- targets[[kk]]
  cat("Target: ", target, "", 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, "", sep="")

    # Validate that it points to the correct target
    dest <- filePath(pathname, expandLinks="any")
    cat("Available target: ", dest, "", 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, "")
      warning(msg)
    }

    # Cleanup
    file.remove(pathname)
  }
}

Run the code above in your browser using DataCamp Workspace