.bat 中的 .bat,不可操作——你会怎么做?

Dan*_*iel 5 windows batch command-line batch-file

我收到了一个警告,说这是一个过于“主观”的问题,但我真的找不到在像标题一样短的文本中说这个的另一种方式,所以我很抱歉......对标题的任何建议表示赞赏。

所以关于我的问题,昨天我尝试编写一些代码但我失败了,就这么简单......经过几个小时的搜索问题,我仍然找不到它:所以希望找到解决方案,我在问,你会怎么做?

目标:

F:\目录下有一个名为a.bat的文件,点击后需要移动到C:\Users目录下,移动操作会破坏文件,需要重新启动移动后的 C:\Users 目录。所以我们需要在一个批次中编码一个批次,对吗?

单击 a.bat 时会形成一个,并在移动完成时执行移动以及启动 a.bat。现在我似乎无法做到,代码就是行不通。它创建了一个 CMD 循环,迫使我重新启动计算机,即使它看起来像一个简单的任务——是的,如果 %cd% == C:\Users goto z,我就这样做了,这将跳过创建可以执行的批处理移动,如果文件已经在该目录中。

Dav*_*ard 6

如果我理解这个问题,你可以尝试这样的事情:

@ECHO OFF

:: If the script is not running under "C:\Users" it will copy
:: itself there and then execute that copy which will remove
:: this copy (ie. the copy in F:\) and then perform the normal
::script operations.

IF /I NOT "%~dp0"=="C:\Users\" (
  IF NOT EXIST "C:\Users\%~nx0" COPY "%~0" C:\Users
  IF EXIST "C:\Users\%~nx0" (
    "C:\Users\%~nx0" "%~0"
    GOTO END
  ) ELSE (
    ECHO.
    ECHO Unable to copy the script to "C:\Users".
    ECHO.
    ECHO Ensure you are running the script as an administrator and try again.
    ECHO.
    ECHO Press any key to close this window ...
    PAUSE > NUL
    GOTO END
  )
)


:: The first parameter of the script (%~1) is the path to the old
:: location of the script that needs to be removed.  The parameter
:: is used recursively (above) whenever the script is executed from
:: a location other than "C:\Users".

IF NOT "%~1"=="" IF EXIST "%~1" DEL /F "%~1"


::
:: Put your script code here
::

:END
Run Code Online (Sandbox Code Playgroud)

注意:
批处理文件必须以管理员身份运行才能在 C:\Users 中复制,除非 C:\Users 的权限已更改为允许标准用户写入文件夹。


Ste*_*ven 2

您不需要移动 .bat 文件。只需cd调用更改批处理实例的目录即可。创建一个包含以下内容的新 .bat 文件,并从任何位置运行它。

cd C:\Users
dir
pause
Run Code Online (Sandbox Code Playgroud)

该文件应输出 C:\Users 中的文件和目录