JMD*_*JMD 5 backup network-shares command-line
我正在创建一个简单的命令行备份脚本,以使用 robocopy 将“重要”内容镜像到我的 NAS 盒。我不想依赖于该 NAS 盒的硬编码映射驱动器号。在脚本中,我想映射一个临时驱动器号,复制相关文件,并处理映射的驱动器。我不想假设如果该驱动器号已经被映射,它就指向我期望的位置。我也不想在映射驱动器时“假设”某些驱动器号可用,因此是前导的“*”。
net use * \\nasbox\sharename 密码 /user:username /persistent:no
在我的 .bat 脚本中提取该驱动器号以用于后续命令的正确方法是什么?我想我可以拼凑“for”的标记解析的一些变体,但我也怀疑一定有比深奥的 for 语法更简单的方法。
C:\>net use * \\nasbox\sharename 密码 /user:username /persistent:no 驱动器 Z:现在连接到 \\nasbox\sharename。 命令成功完成。 C:\>
一旦我有了映射的驱动器号,我想按照以下方式跟进命令:
robocopy C:\path\to\important\stuff %NewDrive%\path\to\backup /MIR /Z
使用命令pushd
和popd
。
pushd \\server\share
将创建一个临时驱动器(从 Z: 开始,然后向后直到找到一个可用的字母)并进入它。完成后,popd
将删除临时驱动器并让您回到原来的位置。
C:\Users\Snark>pushd /?
Stores the current directory for use by the POPD command, then
changes to the specified directory.
PUSHD [path | ..]
path Specifies the directory to make the current directory.
If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter. Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.
C:\Users\Snark>popd /?
Changes to the directory stored by the PUSHD command.
POPD
If Command Extensions are enabled the POPD command will delete
any temporary drive letter created by PUSHD when you POPD that
drive off the pushed directory stack.
Run Code Online (Sandbox Code Playgroud)