2

Basically, we have a lot of .mht files (generated periodically with MicroStrategy) and we need to serve these files on different folders to apply security. To avoid space consumption, we thought of creating shortcuts on several folders.

The files all start with groupings of 4 digits and an underscore (e.g. 0001_0041, 0001_0043, etc.).

This is what I have now:

Get-Childitem -Path "C:\Users\Max\Google Drive\Portal\Mermaid" -Recurse -Include "0002*.mht"
Foreach-Object {
  $ruta = Get-Childitem -Path "C:\Users\Max\Google Drive\Portal\Mermaid" -Recurse -Include "0002*.mht"
  $nombre = Get-Childitem -Name "C:\Users\Max\Google Drive\Portal\Mermaid" -Recurse -Include "0002*.mht"
  $ncorto = $nombre | ForEach-Object {$nombre.Substring(0,9)}
  $container = "C:\Users\Max\Google Drive\Portal\Mermaid\MHT_Shortcuts\$ncorto"
  if (!(Test-Path $container)) {
    New-Item -ItemType directory -Path $container | Out-Null
  }
  $TargetFile = $ruta
  $ShortcutFile = "$container\$nombre.lnk"
  $WScriptShell = New-Object -ComObject WScript.Shell
  $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
  $Shortcut.TargetPath = $TargetFile
  $Shortcut.Save()
}

This works as long as there is only one file starting with 0002. However, they keep historic files that have the same name with timestamp.

If there are multiple files that start with 0002 (there will be as they keep historic versions of these files), I get the error:

Exception setting "TargetPath": "The parameter is incorrect. (Exception from
HRESULT: 0x80070057 (E_INVALIDARG))"
En C:\Users\Max\Desktop\MK_LNK_V01.ps1: 25 Character: 7
+                         $Shortcut.TargetPath = $TargetFile
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterSetValueTI

Sorry if this is a duplicate, I couldn't find a post about this specific issue.

5
  • I don't understand what the question is here. Are you trying to create a shortcut to a file, but rather than finding one file with a given name you're getting multiple files with the same name? Commented Jan 12, 2016 at 20:02
  • Sorry if I wasn't clear enough. If there are multiple files that meet the condition, it fails. If there is only one file starting with 0002 it works perfectly. Creates a folder 0002_xxxx taking the short name from the file. But if there are more than one files that start with 0002 it will fail. There will be many files starting with 0002 and I need to create shortcuts for all of them. Thanks! Commented Jan 12, 2016 at 20:15
  • 1
    You've cut off the most important part of the error. There should be another line or two above the 4 you've posted that explains the error encountered. Commented Jan 12, 2016 at 20:19
  • It's not clear what you want to do with the old files by the same name. Delete them? Keep them? Rename them to something arbitrary but reversible? Move them into an archive folder? Commented Jan 13, 2016 at 2:02
  • Hello Nathan, thanks for the reply. The historic files will be kept up to 5 previous versions on the same folder. We only need to update the shortcuts on those files. The idea is to create shortcuts to all files starting with a string (e.g. 0001_0042_xxxxxx_2015_12_01, 0001_0042_xxxxxx_2016_01_01, etc.) into a folder taking the first 9 characters of the file name. Commented Jan 13, 2016 at 13:10

3 Answers 3

2

Here's a short example of how to create .lnk files for all files in a directory:

$shortcutPath = "C:\TextFiles"
$wshShell = New-Object -ComObject "WScript.Shell"
Get-ChildItem (Join-Path $shortcutPath "*.txt") | ForEach-Object {
  $lnkFilename = Join-Path $shortcutPath ("{0}.lnk" -f [IO.Path]::GetFilenameWithoutExtension($_.FullName))
  $shortcut = $wshShell.CreateShortcut($lnkFilename)
  $shortcut.TargetPath = $_.FullName
  $shortcut.Save()
}

Of course you will need to modify this example to suit your needs.

Sign up to request clarification or add additional context in comments.

2 Comments

This work fine and almost addresses the whole request. I am trying now to change the path where the shortcut is generated so it places the shortcut into a folder taking the 1st 9 characters of the filename. So, all files starting with 0001_0041 would go into one folder named 0001_0041, and so on for 0002_0042, 0001_0041, etc. Thanks for the reply.
Nailed it with this answer and adding some lines. Thanks a lot @Bill_Stewart!!! I am posting the script.
0

Based on the script that @Bill_Stewart provided, I added some lines to first delete the links, then create the containing folder based on the 1st 9 characters of the filename and create the shortcuts in it:

$shortcutPath = "C:\Origin"
$contenedor = "C:Destination"
$wshShell = New-Object -ComObject "WScript.Shell"
Get-ChildItem -Path $contenedor -Include *.lnk -File -Recurse | foreach { $_.Delete()}
Get-ChildItem (Join-Path $shortcutPath "*.mht") | ForEach-Object {
  $nombre = $_.BaseName
  $ncorto = $nombre.SubString(0, 9)
  $destino = Join-Path $contenedor $ncorto
  if (!(test-Path $destino)) {
                        New-Item -ItemType directory -Path $destino | Out-Null}     
  $lnkFilename = Join-Path $destino("{0}.lnk" -f [IO.Path]::GetFilenameWithoutExtension($_.FullName))
  $shortcut = $wshShell.CreateShortcut($lnkFilename)
  $shortcut.TargetPath = $_.FullName
  $shortcut.Save()

This worked as a charm. Thanks all for your help.

Comments

0

The TargetPath property of a WshShortcut object expects a single path, not a list of paths. Also, I'd recommend moving everything that doesn't require repeated exectution outside the loop.

$app = New-Object -ComObject 'WScript.Shell'

$container = 'C:\shortcut\folder'
$path = 'C:\Users\Max\Google Drive\Portal\Mermaid'

if (!(Test-Path $container)) {
  New-Item -Type Directory -Path $container | Out-Null
}

Get-Childitem -Path $path -Recurse -Include "0002*.mht" | Foreach-Object {
  $ShortcutFile = "$container\$nombre.lnk" -f $_.BaseName
  $Shortcut = $app.CreateShortcut($ShortcutFile)
  $Shortcut.TargetPath = $_.FullName
  $Shortcut.Save()
}

1 Comment

Thanks for the reply. I couln't make it work properly but I am stil testing this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.