【发布时间】:2018-10-16 17:29:29
【问题描述】:
我的批处理脚本无法从网络源运行,这就是为什么它将自己复制到桌面并启动新副本,完成后删除副本。
:: Check location
if "%~dp0" == "%userprofile%\Desktop\" goto:eof
xcopy /I /Y "%~dpnx0" "%userprofile%\Desktop\" >nul 2>&1
start "new window" cmd /c %userprofile%\Desktop\batchname.bat
exit
这很好,我的问题是,%cd% 或 %0 没有更新,并且仍然像原始脚本一样运行。显示原始脚本的位置。
如何检查位置,将自身复制到桌面并启动它,就像在 Windows 中双击一样?因为脚本只会失败,如果它是通过共享的原始脚本启动的。
会发生什么:
- 从网络共享启动脚本 XYZ.bat
- 脚本通知不在桌面上
- 脚本会自行复制到桌面
- 脚本从桌面运行副本
脚本结束
从原始脚本启动的副本
- 脚本位于桌面 >> 很好
-
脚本读取脚本头信息:
for /F "tokens=3-8 delims= " %%a in ('findstr /B /C:":: Drive:" "%~dpnx0"') do ( 脚本失败,因为 %~dpnx0 包含原始脚本的路径,由于此时删除了所有网络共享,因此该路径不可用
有什么建议吗? PS:我是新来的,希望我的英语可以理解。干杯
编辑: 感谢您的帮助,现在已经修复了路径的东西并且脚本工作正常,只要我不打开“删除所有现有驱动器”功能。如果我这样做,会发生以下情况:
之前采取的步骤: 脚本检查位置并将自身复制到用户桌面并从那里开始,所有驱动器也被删除,而不是在 findstr 上失败并出现错误:
- [Drive-Mapper:] 已删除所有地图驱动器
- [Drive-Mapper:] 现在映射驱动器:
- Das aktuelle Verzeichnis ist ungültig。 当前 文件夹不存在
- [Drive-Mapper:]成功完成
- Drücken Sie eine beliebige 味道。 . . 按任意键继续
映射函数:
:mapdrives
%say% Mapping drives now:
set errorcount=0
for /F "tokens=3-8 delims= " %%a in ('findstr /B /L /C:":: Drive:" "%~f0"') do (
REM echo Server=%%a User=%%b Letter=%%c drive=%%d nick=%%e
REM if "%%b" == "all" OR if "%%b" == "%username%" (
if "%%b" == "all" (
>nul 2>&1 net use %%c: \\%%a\%%d /persistent:yes
if errorlevel 1 (%say2% Failed %%c: \\%%a\%%d & set /a errorcount=errorcount+1) else (
if "%%e" == "" (
:: Rename without nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%d (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d
) else (
:: Rename with nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%e (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d @ %%e
)
)
)
if "%%b" == "%username%" (
>nul 2>&1 net use %%c: \\%%a\%%d /persistent:yes
if errorlevel 1 (%say2% Failed %%c: \\%%a\%%d & set /a errorcount=errorcount+1) else (
if "%%e" == "" (
:: Rename without nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%d (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d
) else (
:: Rename with nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%e (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d @ %%e
)
)
)
%sf_wait2%
)
%sf_wait%
if "%errorcount%" == "0" (%say% Successfully finished) else (%say% Warning %errorcount% Errors!!)
%say2% & pause
goto:eof
批处理头中的脚本螃蟹信息:
:: - Force - Deleting drives (0=No,1=Yes)
set force_del_drives=1
:: - Force - Kill Explorer (0=No,1=Yes)
set force_kill_explorer=1
::----------------------------------------------------------------------------------------
:: HINT SERVER USER LETER DRIVE NICKNAME (IF NOT USING DRIVENAME)
:: Drive: server all H Home My Home
:: Drive: server all V Drive1
:: Drive: server all M Drive2
::----------------------------------------------------------------------------------------
:: -Drive: server user I DisabledDrive
:: Drive: server user K Drive4
:: Drive: server user Z Homes All Homes
PS:如果我从 c:\ 运行脚本,它会将自身复制到桌面并完美运行,如果我不删除最初执行脚本的网络驱动器,它也可以像我说的那样工作。
有什么想法吗?
【问题讨论】:
-
请在您发布的代码中显示
for /f行。您提供的代码运行良好。 -
Script fails because %~dpnx0 contains path of original script这个假设是完全错误的。%~dpnx0或更短的%~f0表示当前正在运行的批处理 -
findstr 字符串是
::<space>Drive:,而您的脚本螃蟹信息 是::<space><space>Drive:,其中<space>是一个实际空间。不明白 findstr thingy 与您提到的任何错误有何关系。
标签: batch-file path copy environment-variables