我的批处理脚本无法从网络源运行,这就是为什么它要将自己的副本复制到桌面并启动新的副本,在完成时它将删除副本。
:: 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没有更新,并且仍然像原始脚本一样。显示原始脚本的位置。
我如何检查位置,自我复制到桌面并启动它,就像在窗口内双击一样?因为脚本只会失败,如果它是通过共享的原始脚本启动的。
发生了什么:
for /F "tokens=3-8 delims= " %%a in ('findstr /B /C:":: Drive:" "%~dpnx0"') do (有什么建议吗?我是新来的,希望我的英语是可以理解的。干杯
编辑:感谢您的帮助,只要我不打开“删除所有现有驱动器”函数,带路径的东西现在就修复了,脚本也能正常工作。如果我这样做,就会发生以下情况:
前面采取的步骤:脚本检查位置并将自身复制到用户桌面,并从那里开始,所有驱动器也会被删除,而不是在findstr错误上失败:
映射功能:
: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 HomesPS:如果我从c:\运行脚本,它会将自己复制到桌面上并完美地运行,就像我说的那样,如果我不删除原始执行脚本的网络驱动器。
有什么想法吗?
发布于 2018-05-07 01:03:12
@echo off
setlocal
:: Check location
if not exist "%userprofile%\Desktop\" (
>&2 echo Desktop folder not exist
exit /b 1
)
if "%~dp0" == "%userprofile%\Desktop\" goto :main
xcopy /I /Y "%~f0" "%userprofile%\Desktop\" >nul 2>&1
start "new window" "cmd /c "%userprofile%\Desktop\%~nx0""
goto :eof
:main
echo %~f0
pause
goto :eof添加了对桌面文件夹的检查,因为它是Shell文件夹,并且可能不存在于该位置。
逻辑更改为:主标签,如果位置是桌面文件夹。
修改start命令,以处理脚本名称和扩展,而无需硬编码名称和扩展名。
https://stackoverflow.com/questions/50205208
复制相似问题