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

この問題を 2 つまたは 3 つの興味深いトリックで解決しましょう。

  • コンパイルされるコードは、それを識別する特定のラベル/行で囲まれています。たとえば:EMBEDDED_CODE
  • コマンドを使用findstr /N "^:EMBEDDED_CODE" "%~F0"して、埋め込みコードの開始行と終了行を取得します。コマンド自体^の行を見つけないように、検索文字列は で始まることに注意してください。findstr
  • コマンドを使用for /F "skip=%begin% ...して行番号と %end% を比較し、埋め込まれたコードを抽出します。
  • 置き換えたい変数をパーセントではなく感嘆符で囲み、最初に遅延展開を有効にします。

ここにあります:

編集:埋め込みコードの :LABELS にコロンを保持するためにfindstrbyを変更しました。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 に答える