Batch file 当批处理程序关闭时,执行一系列命令

Batch file 当批处理程序关闭时,执行一系列命令,batch-file,command,exit,Batch File,Command,Exit,因此,我将继续修改我在上一个问题中描述的程序 在我前进的过程中,我试图创建一个部分,该部分仅在程序运行时授予对文本文档的访问权。为此,我在使用net use启动时授予访问权限。然而,我的困境是,我不知道批处理中有任何事件处理程序可以使我的程序运行net use/delete(添加第二批。这对用户单击“关闭”按钮没有帮助(您需要为此编写一个真正的程序) 批次1 Net use etc Call Batch2 Net Use/delete etc 批次2 Your commands 当第二批终止

因此,我将继续修改我在上一个问题中描述的程序


在我前进的过程中,我试图创建一个部分,该部分仅在程序运行时授予对文本文档的访问权。为此,我在使用
net use
启动时授予访问权限。然而,我的困境是,我不知道批处理中有任何事件处理程序可以使我的程序运行
net use/delete
(添加第二批。这对用户单击“关闭”按钮没有帮助(您需要为此编写一个真正的程序)

批次1

Net use etc
Call Batch2
Net Use/delete etc
批次2

Your commands

当第二批终止时,批处理1中的其余命令将运行。您需要阅读帮助
call/?

而不是
net use
,您可以使用带有UNC路径的
pushd
来创建临时网络驱动器映射。例如:

pushd \\localhost\c$\Users\%username%\Documents
然后,无论您是
popd
endlocal
exit/b
,还是用户用红色X终止脚本,在任何情况下,临时映射都将在最后被删除

只需确保
setlocal
位于脚本顶部,或者至少在
pushd
之前的某个位置。通常最好在
@echo off
之后立即将
setlocal
置于每个脚本的顶部,除非您有特定的理由不这样做


如果需要进行身份验证,请结合使用
net use
pushd
。使用
net use
而不带驱动器号,然后使用
pushd\\UNC\path&&net use\\computername/delete
。下面是一个更完整的示例:

@echo off
setlocal

set "remotePC=minastirith"
set "user=%remotePC%\adminUser"
set "pass=Password"

:: establish an authenticated session
net use \\%remotePC% /user:%user% %pass%

:: pushd and immediately terminate the net use
pushd \\%remotePC%\share && net use \\%remotePC% /delete

:: The next two commands demonstrate that even though
:: the session has been disconnected, pushd still has
:: temporary access.
cd
dir

pause

:: When you popd, endlocal, exit /b, or close the window,
:: there's no longer an authenticated session.  Another
:: attempt to pushd should result in an error.
popd
pushd \\%remotePC%\share

pause

我应该说得更具体一点。我需要访问\\localhost,那么有没有办法通过pushd命令临时授予访问权限?例如,pushd的网络使用中的等效/USER:username密码?我不明白我在留下评论时\\localhost做了什么。我的具体意思是\(另一个计算机名)@最近的WebDev2000我添加了一个额外的示例,演示了如何将
net use
pushd
相结合以提供临时身份验证。请参见上面的编辑。谢谢,这正是我所需要的!我仔细研究了一下,得出结论,这是最好的解决方案。