batch-file - 批量复制并在新位置重新打开
问题描述
我的批处理脚本无法从网络源运行,这就是为什么它将自己复制到桌面并启动新副本,完成后删除副本。
:: 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:\ 运行脚本,它会将自身复制到桌面并完美运行,它也可以像我说的那样工作,如果我不删除最初执行脚本的网络驱动器。
有任何想法吗?
解决方案
@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
添加了对Desktop文件夹的检查,因为它是Shell 文件夹,并且可能不存在于该位置。
如果位置是桌面文件夹,则逻辑更改为转到 :main 标签。
修改为处理脚本名称和扩展名的命令,start
无需硬编码名称和扩展名。
推荐阅读
- firebase - firebase deploy --only 函数是否会刷新函数外部的包?
- asp.net - 动态 og 元内容 asp.net webforms 母版和内容页
- spring-boot - 多对多单向关系JPA中的孩子获取父母
- java - 为什么 String 不受浅拷贝的影响?
- linux - 为什么docker日志使用容器ID存储在系统中?为什么不使用容器的名称?
- docker - 处理 Jenkins 在 docker 容器内运行的失败测试
- css - 如何让网格的孩子在 Safari 中占据 100% 的高度?
- javascript - 如何删除评估(javascript)
- c# - 使用 ef 代码迁移 dotnetcore 或 packge manager 控制台创建数据库
- javascript - 在 asp.net 页面回发后加载当前工作的 bootstrap 手风琴部分