【问题标题】:Using Wildcard for folder using a SymbolicLink使用 SymbolicLink 对文件夹使用通配符
【发布时间】:2015-10-15 19:17:59
【问题描述】:

以前在 Windows 7 中,我能够将 My Documents 文件夹的文件路径更改为网络映射(例如 H:\John Doe Documents)。由于我们已经切换到 Windows 7,因此我不得不使用一种解决方法,将 C 驱动器上的文件夹中的链接文件创建到映射位置,并在文件实际链接之前将其包含在“我的文档”库中。

我们当前的文件结构为 - \\servername\homefolder\%username%\John Doe Documents\\servername\homefolder\%username%\johndoedocuments。创建符号链接时,我需要覆盖这两个文件夹。

这是我目前正在使用的脚本

@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\

目前这不起作用。如果我删除 *documents\ 它确实有效。我尝试这样做的原因是因为我们还将 Outlook 的用户 pst 文件存放在 \%username% 文件夹中,我们不希望用户看到该文件夹​​并可能将其删除。宁愿他们直接进入文档文件夹。

有什么帮助吗?希望这是我想念的简单的东西。提前致谢!

【问题讨论】:

  • 不能像这样使用通配符;但是,我认为 Username Documents 可能只是 Windows 的本地化名称,所以实际上它们很可能在驱动器上被称为 Documents...
  • 这也是问题所在,我不是最初设置这些的人,所以一些用户设置为 - h:\abc1234\johndoedocuments,其他用户设置为 h:\abc1234\john doe documents。因此,如果可能,我试图让它在脚本中捕获其中一个文件名。很抱歉措辞上的混乱。我会解决的!
  • 如果你有一个用户列表,也许在一个文本文件中,你可以使用for /F 遍历它,并在循环内使用if exist 建议的查询below;没有空格的版本可以使用字符串替换语法构建(参见set /?)然后...

标签: batch-file cmd mklink


【解决方案1】:

您可以使用if exist ... 来检测存在哪个路径。

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
)

更新。

我认为您可以以这种方式使用通配符

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

【讨论】:

  • 我可以在这个选项中使用通配符而不是使用人名吗?我想在整个设施中实施这一点。我可以编写一个更复杂的脚本来搜索包含单词 Documents 的文件夹,然后将其作为变量输入吗?
  • 是的,你可以使用像\path\to\*doc?这样的通配符,只要包含这样的路径部分是最最后一个的;所以\path\to\any*\items 不起作用;
【解决方案2】:

进行了一些更改,要使其正常工作,您必须首先在 C 中创建 Documents 文件夹,将其添加到库中,然后创建链接;否则它将无法工作,因为网络驱动器没有被索引并且您无法将非索引文件添加到库中。

这是完整的工作代码,非常感谢,Dmitry!你能解释一下这些通配符是什么吗?抱歉,我还在处理很多 cmd 命令。除了%%A%%~fA 部分之外,我了解大部分内容:)

@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
    )
)

【讨论】:

  • %%Afor 循环参数。您当然可以选择%%A 以外的任何字母。但是有parameter extensions 和字母a、d、f、n、p、s、t、x 是保留的。所以使用大写字母也是避免冲突%%A而不是%%a的好方法。
  • 顺便说一句,if exist "%%~fA" 不是必需的。 for /d 循环遍历现有目录。我忘了删除它。
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 2011-02-19
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多