0

I am trying to copy the folder structure of SOURCE Folder to TARGET Folder including sub-folders (empty and non-empty), without copying the Files themselves, instead I want to create file shortcuts of all files in their respective folders in the TARGET folder Structure.

I have tried this, and successfully created folder Structure:

C:\>XCOPY SOURCE TARGET /T /E

But I am unable to find an option to create file Shortcuts (.lnk files)for respective files in TARGET folder source

Pictorial representation of SOURCE and TARGET Folders

  • Why do they have to be lnk files and not just links? Creating lnk files can be quite difficult and your problem is likely pretty unique. Have a look at How to make a shortcut from CMD? to find some information about how to create lnk files using a script. You will likely have to write a script your own to do this. With PowerShell you could use something like Get-ChildItem -Recurse -File. – Seth Aug 14 '18 at 11:26
0

This PowerShell Function would do the trick.

Save it anywhere as Create-ShortcutForEachFile.ps1

load it into a PowerShell session like this: . C:\somewhere\Create-ShortcutForEachFile.ps1

Then use it like this: Create-ShortcutForEachFile -Source C:\foo -Destination C:\bar -Recurse

function Create-ShortcutForEachFile {

    Param(
        [ValidateNotNullOrEmpty()][string]$Source,
        [ValidateNotNullOrEmpty()][string]$Destination,
        [switch]$Recurse
    )

    # set recurse if present
    if ($Recurse.IsPresent) { $splat = @{ Recurse = $true } }

    # Getting all the source files and source folder
    $gci = gci $Source @splat
    $Files = $gci | ? { !$_.PSisContainer }
    $Folders = $gci | ? { $_.PsisContainer }

    # Creating all the folders
    if (!(Test-Path $Destination)) { mkdir $Destination -ea SilentlyContinue > $null }
    $Folders | % {
        $Target = $_.FullName -replace [regex]::escape($Source), $Destination
        mkdir $Target -ea SilentlyContinue > $null
    }

    # Creating Wscript object
    $WshShell = New-Object -comObject WScript.Shell

    # Creating all the Links
    $Files | % {
        $InkName = "{0}.lnk" -f $_.BaseName
        $Target = ($_.DirectoryName -replace [regex]::escape($Source), $Destination) + "\" + $InkName
        $Shortcut = $WshShell.CreateShortcut($Target)
        $Shortcut.TargetPath = $_.FullName
        $Shortcut.Save()
    }
}
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.