batch-filecmdmklink

Using Wildcard for folder using a SymbolicLink


Previously in Windows 7 I was able to change the file path for the My Documents folder to a network map (ex H:\John Doe Documents). Since we've switched to Windows 7 I have had to use a workaround by making a linked file from a folder on the C drive to the mapped location and including it in the My Documents library before the file is actually linked.

Our current file structure goes as - \\servername\homefolder\%username%\John Doe Documents or \\servername\homefolder\%username%\johndoedocuments. I need to cover both folders when creating the symbolic link.

Here's the script I am using currently

@echo off

mkdir c:\Documents
echo.
echo.

echo Right click My Documents and add C:\Documents to the Library Locations.
echo.
echo.

pause
rd C:\Documents
mklink /D C:\Documents \\servername\homefolder\%username%\*documents\

Currently this isn't working. If I remove the *documents\ it does work though. The reason I am trying to do this is because we also house the users pst file for outlook in the \%username% folder and we would rather not have the user see that folder and potentially delete it. Would rather them just go directly into the documents folder instead.

Any help? Hoping it's something simple I'm missing. Thanks in advance!


Solution

  • You can use if exist ... to detect which path exists.

    if exist "\\servername\homefolder\%username%\John Doe Documents" (
        mklink /D C:\Documents "\\servername\homefolder\%username%\John Doe Documents\"
        goto :eof
    )
    if exist "\\servername\homefolder\%username%\johndoedocuments" (
        mklink /D C:\Documents "\\servername\homefolder\%username%\johndoedocuments\"
        goto :eof
    )
    

    Update.

    I think you could use wildcards in this manner

    for /d %%A in (\\servername\homefolder\%username%\*documents) do (
        if exist "%%~fA" (
            mklink /D C:\Documents "%%~fA"
            goto :eof
        )
    )