1

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!

MackMan
  • 129
  • 3
  • 15
  • You can't use wildcards like this; however, I think the `Username Documents` could be the Windows' localized names only, so actually they are likely called just `Documents` on the drive... – aschipfl Oct 15 '15 at 19:35
  • That's where the issue comes in as well, I wasn't the original one setting these up so some users are set like - `h:\abc1234\johndoedocuments` and others are `h:\abc1234\john doe documents`. So I'm trying to get it to catch either one of those file names in the script if possible. Sorry for the confusion on wording. I'll fix that! – MackMan Oct 15 '15 at 19:39
  • if you have a user list, perhaps in a text file, you could walk through it using `for /F` and use the `if exist` queries suggested [below](http://stackoverflow.com/a/33157531/5047996) inside of the loop; the version without spaces could be built using string replacement syntax (see `set /?`) then... – aschipfl Oct 15 '15 at 22:07

2 Answers2

2

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
    )
)
Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35
  • Can I use a wildcard in this option instead of using the person's name though? I'm wanting to implement this throughout the facility. Could I write a more elaborate script to do like a search for folders containing the word Documents then input that as a variable? – MackMan Oct 15 '15 at 20:58
  • Yes, you can use wildcards like `\path\to\*doc?`, as long as the part of the path containing such is the very _last_ one; so `\path\to\any*\items` does not work; – aschipfl Dec 17 '15 at 00:29
0

Made a couple changes, for it to work correctly you have to create the Documents folder in C first, add it to the library then create the link; otherwise it will not work because the network drive isn't indexed and you can't add a non-indexed file to a library.

Here's the completed working code, thank you so much, Dmitry! Can you explain what these wildcard are? Sorry I'm still getting into a lot of cmd commands. I understand most of it except for the %%A and %%~fA parts :)

@echo off
mkdir C:\Documents
pause
for /d %%A in (\\servername\homefolder\%username%\*documents) do (
    if exist "%%~fA" (
        rd C:\Documents
        mklink /D C:\Documents "%%~fA"
        goto :eof
    )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
MackMan
  • 129
  • 3
  • 15
  • `%%A` is the `for` loop parameter. You can of course pick any letter of the alphabet other than `%%A`. But there are [parameter extensions](http://ss64.com/nt/syntax-args.html) and letters a, d, f, n, p, s, t, x are reserved. So using a capital letter is also a good way to avoid conflicts `%%A` rather than `%%a`. – Dmitry Sokolov Oct 16 '15 at 13:01
  • BTW, `if exist "%%~fA"` is not necessary. The `for /d` loops through existed directories. I forgot to remove it. – Dmitry Sokolov Oct 16 '15 at 13:05