topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 4:08 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Automatic shortcut creation for items in a folder.
« on: September 10, 2014, 11:26 AM »
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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Automatic shortcut creation for items in a folder.
« Reply #1 on: September 10, 2014, 12: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 [Select]
  1. #By BigTeddy 05 September 2011
  2.  
  3. #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
  4. #The advantage of this method over using WMI eventing is that this can monitor sub-folders.
  5. #The -Action parameter can contain any valid Powershell commands.  I have just included two for example.
  6. #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
  7. # Version 1.1
  8.  
  9. $folder = [environment]::getfolderpath("mydocuments")  # Path to users 'My Documents'
  10. $filter = '*.*'  # You can enter a wildcard filter here.
  11.  
  12. # In the following line, you can change 'IncludeSubdirectories to $true if required.                          
  13. $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'DirectoryName, LastWrite'}
  14.  
  15. # Here, the creation event is registered.
  16.  
  17. Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
  18. $name = $Event.SourceEventArgs.Name
  19. $changeType = $Event.SourceEventArgs.ChangeType
  20. $timeStamp = $Event.TimeGenerated
  21. Write-Host "The folder '$name' was $changeType at $timeStamp" -fore green
  22. Out-File -FilePath d:\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
  23.  
  24. # To stop the monitoring, run the following command:
  25. # Unregister-Event FileCreated

I'll try and add in the shortcut bit now.
« Last Edit: August 31, 2015, 09:19 PM by 4wd »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Automatic shortcut creation for items in a folder.
« Reply #2 on: September 10, 2014, 01: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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Automatic shortcut creation for items in a folder.
« Reply #3 on: September 10, 2014, 02: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 [Select]
  1. #By BigTeddy 05 September 2011
  2.  
  3. #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
  4. #The advantage of this method over using WMI eventing is that this can monitor sub-folders.
  5. #The -Action parameter can contain any valid Powershell commands.  I have just included two for example.
  6. #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
  7. # Version 1.1
  8.  
  9. $global:folder = [environment]::getfolderpath("mydocuments")  # Path to users 'My Documents'
  10. $global:desktop = [environment]::getfolderpath("desktop")  # Path to users 'Desktop'
  11. $filter = '*.*'  # You can enter a wildcard filter here.
  12.  
  13. # In the following line, you can change 'IncludeSubdirectories to $true if required.                          
  14. $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'DirectoryName, LastWrite'}
  15.  
  16. # Here, the creation event is registered.
  17.  
  18. Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
  19. $name = $Event.SourceEventArgs.Name
  20. $changeType = $Event.SourceEventArgs.ChangeType
  21. $timeStamp = $Event.TimeGenerated
  22. Write-Host "The folder '$name' was $changeType at $timeStamp" -fore green
  23. # Out-File -FilePath d:\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
  24.  
  25. $result = $name.LastIndexOf('\')
  26. if ($result -eq -1) {
  27.   $DestinationPath = $desktop + '\' + $name + '.lnk'
  28.   } elseif ($result -gt 0) {
  29.   $DestinationPath = $desktop + '\' + $name.SubString($result + 1) + '.lnk'
  30.   }
  31. $WshShell = New-Object -comObject WScript.Shell
  32. $Shortcut = $WshShell.CreateShortcut($DestinationPath)
  33. $Shortcut.TargetPath = $folder + '\' + $name
  34. $Shortcut.WorkingDirectory = $folder + '\' + $name
  35. $Shortcut.Save()
  36. }
  37.  
  38. # To stop the monitoring, run the following commands:
  39. # Unregister-Event FileCreated
« Last Edit: August 31, 2015, 09:20 PM by 4wd »