0

好的,我今天的问题显然非常困难,按标题我的任务似乎并不困难,但相信我,这不是你通常的任务。

我想要做的是运行一个批处理文件来编译另一个批处理文件,其中设置了一个正在运行的批处理文件中的变量。不过,这就是问题所在。我不想从编码中编写批处理文件。这就是我的意思。

这是完成的批处理文件的示例。

@echo off
color a
title my compiled batch
echo.
echo.
echo  This is the batch file that was compiled from the one I was using to create it.
echo.
echo.
pause
exit 

这是您最有可能想到的编译器。

[new.bat]
@echo off
color a
cls
echo.
echo type your name.
echo.
echo.
set input=
set /p input=Name:  ( I typed "my compiled batch" in this input area)
echo @echo off >>new.bat
echo color a >>new.bat
echo title %input% >>new.bat
echo echo. >>new.bat
echo echo. >>new.bat
echo This is the batch file that was compiled from the one I was using to create it. >>new.bat
echo echo. >>new.bat
echo echo. >>new.bat
echo pause >>new.bat
echo exit >>new.bat

这是我希望能够看到它的 784 行有多长。

echo "@echo off
color a
title my compiled batch
echo.
echo.
echo This is the batch file that was compiled from the one I was using to create it.
echo.
echo.
pause
exit" >> new.bat

所以基本上我想将批处理文件编译为单个字符串,但它仍然进入“new.bat”,就好像我自己编写了它一样。

反正有这样做吗?

4

2 回答 2

3

让我们通过两三个有趣的技巧来解决这个问题:

  • 要编译的代码包含在标识它的某个标签/行之间;例如:EMBEDDED_CODE
  • 使用findstr /N "^:EMBEDDED_CODE" "%~F0"命令获取嵌入代码的开始行和结束行。请注意,搜索字符串以开头,^以避免找到findstr命令本身的行。
  • 使用for /F "skip=%begin% ...命令并将行号与 %end% 进行比较以提取嵌入代码。
  • 将要替换的变量括在感叹号而不是百分比之间,并在开始时启用延迟扩展。

这里是:

编辑:为了在嵌入代码中保留 :LABELS 中的冒号,我更改findstr了 by 。find

@echo off
setlocal EnableDelayedExpansion
color a
cls
echo.
echo type your name.
echo.
echo.
set input=
set /p input=Name:  ( I typed "my compiled batch" in this input area)

REM Get begin and end lines of the embedded code
set begin=
for /F "delims=:" %%a in ('findstr /N "^:EMBEDDED_CODE" "%~F0"') do (
   if not defined begin (
      set begin=%%a
   ) else (
      set end=%%a
   )
)

REM Extract the embedded lines into new.bat file,
REM the replacement of variables enclosed in exclamation marks is automatic:
(for /F "skip=%begin% tokens=1* delims=[]" %%a in ('find /N /V "" "%~F0"') do (
   if %%a equ %end% goto :EOF
   echo(%%b
)) > new.bat
goto :EOF


:EMBEDDED_CODE Begin
@echo off
color a
title !input!
echo.
echo.
echo This is the batch file that was compiled from the one I was using to create it. 
echo Created on !date! @ !time!
echo.
echo.
pause
exit
:EMBEDDED_CODE End

安东尼奥

于 2013-03-24T12:04:57.277 回答
1

给定 BATCHTEMPLATE.BAT (这是一个text文件 - 但EDITPLUS可以语法高亮.bat文件...

@echo off
color a
$name
echo.
echo.
echo  This is the batch file that was compiled from the one I was using to create it.
$date
echo.
echo.
pause
exit 

然后运行这个批处理:

@ECHO OFF
SETLOCAL
::
SET name=
SET /p name="Name: "
::
(
FOR /f "delims=" %%i IN (batchtemplate.bat) DO (
 ECHO %%i|FINDSTR /b /c:"$" >NUL
 IF ERRORLEVEL 1 (ECHO.%%i) ELSE (
 IF /i %%i==$name ECHO.title  %name%
 IF /i %%i==$date ECHO.ECHO Created %date%
 )
)
)>new.bat

TYPE new.bat

使用

echo MY BATCH|thisbatch

产量

Name: @echo off
color a
title  MY BATCH
echo.
echo.
echo  This is the batch file that was compiled from the one I was using to create it.
ECHO Created 24/03/2013
echo.
echo.
pause
exit 

我使用类似的系统来编写程序...

于 2013-03-24T07:39:46.840 回答