Main Area and Open Discussion > General Software Discussion
Automatic shortcut creation for items in a folder.
(1/1)
questorfla:
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:
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.
wraith808:
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:
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.-wraith808 (September 10, 2014, 01:34 PM)
--- End quote ---
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
Navigation
[0] Message Index
Go to full version