首页 > 解决方案 > 批量复制并在新位置重新打开

问题描述

我的批处理脚本无法从网络源运行,这就是为什么它将自己复制到桌面并启动新副本,完成后删除副本。

:: 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 中双击一样?因为脚本只会失败,如果它是通过共享的原始脚本启动的。

怎么了:

有什么建议么?PS:我是新来的,希望我的英语可以理解。干杯


编辑: 感谢您的帮助,路径的东西现在已经修复并且脚本工作正常,只要我不打开“删除所有现有驱动器”功能。如果我这样做,会发生以下情况:

以前采取的步骤:脚本检查位置并将其自身复制到用户桌面并从那里开始,所有驱动器也被删除,而不是在 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 Homes

PS:如果我从 c:\ 运行脚本,它会将自身复制到桌面并完美运行,它也可以像我说的那样工作,如果我不删除最初执行脚本的网络驱动器。

有任何想法吗?

标签: batch-filepathcopyenvironment-variables

解决方案


@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无需硬编码名称和扩展名。


推荐阅读