0

我想编写一个批处理文件,它在字符串中读取自己的代码,然后以字符串为内容创建一个新的批处理文件并执行它。

4

2 回答 2

2

您可以阅读启动的批处理文件的内容

    type "%~f0"

因此,尝试类似

    echo off
    type "%~f0" > Test\file.bat
    Test\file.bat

但是,此脚本会不断重复自身(或者如果目录 Test 不存在则中止)。所以你需要考虑这种方法(即使用条件语句)。

于 2013-11-07T10:08:19.090 回答
0
@echo off
setlocal DisableDelayedExpansion

if not exist newBatchFile.bat (

   echo First execution!

   set "batchFile="
   for /F "usebackq delims=" %%a in ("%~F0") do (
      set "line=%%a"
      setlocal EnableDelayedExpansion
      if "!line:~0,1!" equ ")" set "batchFile=!batchFile:~0,-1!"
      set "batchFile=!batchFile!!line!"
      if "!line:~-1!" neq "(" set "batchFile=!batchFile!&"
      for /F "delims=" %%b in ("!batchFile!") do endlocal & set "batchFile=%%b"
)
   setlocal EnableDelayedExpansion
   echo !batchFile:~0,-1! > newBatchFile.bat
   newBatchFile.bat

) else (

   echo Second execution!

)
于 2013-11-07T16:02:31.070 回答