符号链接所有名称包含单词的目录(Windows批处理)

时间:2018-07-25 04:09:01

标签: windows batch-file windows-10 symlink

我有一个包含一堆子目录的目录A和一个空的目录B。我想创建一个.bat文件,该文件将目录A中名称包含“ april”的所有子目录符号链接(仅顶层)个)到目录B中。这可能吗?

到目前为止,我有:

echo off
setlocal EnableDelayedExpansion
title Updating Directories...
if exist "C:\path\to\directory\a" (
    echo Updating Directories...
    set "dira=C:\path\to\directory\a"
    set "dirb=C:\path\to\directory\b"
    for /f "usebackq delims=|" %%f in (`dir /b "!dira!"`) do (mklink /d "!dirb!\%%f" "!dira!\%%f")
    title Symlinks Created^^!
    echo Done^^!
    pause
) else (
    echo Cannot find Parent Directory.
    pause
)

这可行,但是它会将目录A中的所有子目录符号链接到目录B。例如,有什么方法可以搜索名称包含“ april”的顶级目录,返回列表,然后仅符号链接那些?

2 个答案:

答案 0 :(得分:1)

Not sure why you want a batch file. Just type the command.

for /f "delims=" %A in ('dir /b /ad "%userprofile%\desktop\*u*.*"') Do mklink /d "%userprofile%\desktop\b\%~nxA" "%A%

This create a link in a folder called B on the desktop (so create it first) for any folder on the desktop that contains the letter u in the name. See dir /?.

Note in Windows (NT family) the wildcard operators are regular expressions unlike Dos.

?*net*.* shows all files that contain net, but don't begin with net.

答案 1 :(得分:0)

Thanks to @CatCat I was able to figure it out. Here's what's now working, for posterity's sake:

echo off
setlocal EnableDelayedExpansion
title Updating Directories...
if exist "!userprofile!\path\to\directory\a" (
    echo Updating Directories...
    set "dira=!userprofile!\path\to\directory\a"
    set "dirb=!userprofile!\path\to\directory\b"
    for /f "usebackq delims=|" %%f in (`dir /b "!dira!\*april*.*"`) do (mklink /d "!dirb!\%%f" "!dira!\%%f")
    title Symlinks Created^^!
    echo Done^^!
    pause
) else (
    echo Cannot find Parent Directory.
    pause
)
相关问题