DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Automatic shortcut creation for items in a folder.  (Read 4,779 times)

questorfla

  • Fighting Slime all the Time
  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
Automatic shortcut creation for items in a folder.
« on: September 10, 2014, 04:26 PM »
This is something I need to find to put a stop to users who can't seem to understand that their desktop is NOT made to keep all their documents piled up on.

I can make a single shortcut to My Documents on their desktop for them but that isn't good enough apparently.  They want one for every single folder and file. (Not all files within folders, just a few that are "loose files" usually word docs.).

None of them can remember HOW to make a shortcut once shown.  Besides which, it is a matter of they just don't want to do even the least bit of extra effort.  The issue is not just one of neatness, their Documents Folder is automatically backed up on the Office Servers because the files in there are important.  But I cannot automatically  backup their entire desktop due to hidden system files and others. 

I'm trying to find a utility that would automatically create a shortcut on their Desktop to any folders that are created or moved into their My Documents folder.  Basically, the same procedure as "right click, send, shortcut to desktop" but I was wondering if there might be a way to make that just "happen" automatically for any folders created or placed inside another folder.

The same procedure might be used for other Main Storage folders like pictures, music etc.  I am just unsure of how to make this an automated procedure that would occur every time. :-\

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Automatic shortcut creation for items in a folder.
« Reply #1 on: September 10, 2014, 05:04 PM »
You should be able to accomplish this with Powershell using the following information:

Powershell FileSystemWatcher
Create Shortcuts using Powershell

Seems rather straightforward.

Modified the first Powershell script to monitor Folder Creation only, atm just outputs to logfile and Powershell interface:

Code: ( powershell)
#By BigTeddy 05 September 2011 
 
#This script uses the .NET FileSystemWatcher class to monitor file events in folder(s). 
#The advantage of this method over using WMI eventing is that this can monitor sub-folders. 
#The -Action parameter can contain any valid Powershell commands.  I have just included two for example. 
#The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true. 
# Version 1.1 
 
$folder = [environment]::getfolderpath("mydocuments")  # Path to users 'My Documents'
$filter = '*.*'  # You can enter a wildcard filter here. 
 
# In the following line, you can change 'IncludeSubdirectories to $true if required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'DirectoryName, LastWrite'} 
 
# Here, the creation event is registered.
 
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The folder '$name' was $changeType at $timeStamp" -fore green 
Out-File -FilePath d:\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 
 
# To stop the monitoring, run the following command: 
# Unregister-Event FileCreated 

I'll try and add in the shortcut bit now.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: September 01, 2015, 02:19 AM by 4wd »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Automatic shortcut creation for items in a folder.
« Reply #2 on: September 10, 2014, 06:34 PM »
One thing about using the filesystemwatcher to be aware of is that it loses connection to the watched folder after having been up for a while- most especially on network or mapped folders- and that's a known limitation.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Automatic shortcut creation for items in a folder.
« Reply #3 on: September 10, 2014, 07:18 PM »
One thing about using the filesystemwatcher to be aware of is that it loses connection to the watched folder after having been up for a while- most especially on network or mapped folders- and that's a known limitation.

Oh well, it's been an exercise in Powershell :)

Completed version, probably some redundant code, requires .NET 4.5 due to one of the procedures, IIRC.

Anyway, it works here OK creating shortcuts to folders and sub-folders in 'My Documents' on the 'Desktop'.

One caveat, I'm not checking for the prior existence of a shortcut the same name.  So if someone creates a folder called 'test' followed by a sub-folder called 'test', the create shortcut command will most likely fail.

Code: ( powershell)
#By BigTeddy 05 September 2011 
 
#This script uses the .NET FileSystemWatcher class to monitor file events in folder(s). 
#The advantage of this method over using WMI eventing is that this can monitor sub-folders. 
#The -Action parameter can contain any valid Powershell commands.  I have just included two for example. 
#The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true. 
# Version 1.1 
 
$global:folder = [environment]::getfolderpath("mydocuments")  # Path to users 'My Documents'
$global:desktop = [environment]::getfolderpath("desktop")  # Path to users 'Desktop'
$filter = '*.*'  # You can enter a wildcard filter here. 
 
# In the following line, you can change 'IncludeSubdirectories to $true if required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'DirectoryName, LastWrite'} 
 
# Here, the creation event is registered.
 
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The folder '$name' was $changeType at $timeStamp" -fore green 
# Out-File -FilePath d:\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"

$result = $name.LastIndexOf('\')
if ($result -eq -1) {
  $DestinationPath = $desktop + '\' + $name + '.lnk'
  } elseif ($result -gt 0) {
  $DestinationPath = $desktop + '\' + $name.SubString($result + 1) + '.lnk'
  }
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($DestinationPath)
$Shortcut.TargetPath = $folder + '\' + $name
$Shortcut.WorkingDirectory = $folder + '\' + $name
$Shortcut.Save()
}
 
# To stop the monitoring, run the following commands: 
# Unregister-Event FileCreated 
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: September 01, 2015, 02:20 AM by 4wd »

Original address: https://www.donationcoder.com/forum/index.php?topic=38934.0