web-dev-qa-db-ja.com

* .bat拡張子を使用せずにバッチスクリプトを実行する方法

* .bat拡張子なしでバッチスクリプトを実行できるWindowsのメソッドはありますか?

26
Sree

これは私にとって興味深いトピックです!私はそれについていくつかの観察をしたいと思います。

最初の重要なポイント:バッチファイルは、拡張子が.BATまたは.CMDのファイルです。限目。バッチファイルは、通常のDOSコマンドの実行に加えて、特定の特定のバッチファイル機能を実現できます。

  • %1%2 ...を介したバッチファイルパラメータへのアクセスとSHIFTコマンドの実行。
  • GOTOコマンドの実行。
  • CALL:NAMEコマンドの実行(内部サブルーチン)。
  • SETLOCAL/ENDLOCALコマンドの実行。

ここで面白い部分は、CMD.exeの入力として任意のファイルをリダイレクトできるため、それに含まれるDOSコマンドはバッチファイルと同様の方法で実行されますが、いくつかの違いがあります。最も重要なのは、以前のバッチファイル機能が機能しないことです。別の違いは、以下のNOT-Batchファイル(私はBATCH.TXTと呼びました)に示されています。

@echo off
rem Echo off just suppress echoing of the Prompt and each loop of FOR command
rem but it does NOT suppress the listing of these commands!

rem Pause command does NOT pause, because it takes the character that follows it
pause
X

rem This behavior allows to put data for a SET /P command after it
set /P var=Enter data: 
This is the data for previous command!
echo Data read: "%var%"

rem Complex FOR/IF commands may be assembled and they execute in the usual way:
for /L %i in (1,1,5) do (
   set /P line=
   if "!line:~0,6!" equ "SHOW: " echo Line read: !line:~6!
)
NOSHOW: First line read
SHOW: Second line
NOSHOW: This is third line
SHOW: The line number 4
NOSHOW: Final line, number five

rem You may suppress the tracing of the execution redirecting CMD output to NUL
rem In this case, redirect output to STDERR to display messages in the screen
echo This is a message redirected to STDERR >&2

rem GOTO command doesn't work:
goto label
goto :EOF
rem but both EXIT and EXIT /B commands works:
exit /B

:label
echo Never reach this point...

前のファイルを実行するには、次のように入力します。CMD/ V:ON <BATCH.TXT遅延拡張を有効にするには/ Vスイッチが必要です。

より専門的な違いは、NOT-Batchファイル内のコマンドがバッチファイルコンテキストではなくコマンドラインコンテキストで実行されるという事実に関連しています。おそらく、デイブやジェブはこの点について詳しく説明することができます。

編集:追加の観察(batch2.txt):

@echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.

rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2

echo Standard input/output operations> CON
echo/> CON
< CON set /P var=Enter value: > CON
echo/> CON
echo The value read is: "%var%"> CON

前のファイルを次のように実行します:CMD <BATCH2.TXT> NUL

[〜#〜]編集[〜#〜]その他の追加の観察結果(batch3.txt)

@echo off

rem Dynamic access to variables that usually requires DelayedExpansion via "call" trick

rem Read the next four lines; "next" means placed after the FOR command
rem (this may be used to simulate a Unix "here doc")

for /L %i in (1,1,4) do (
   set /P line[%i]=
)
Line one of immediate data
This is second line
The third one
And the fourth and last one...

(
echo Show the elements of the array read:
echo/
for /L %i in (1,1,4) do call echo Line %i- %line[%i]%
) > CON

このファイルを通常の方法で実行します:CMD <BATCH3.TXT> NUL

面白い!そうですね。

[〜#〜] edit [〜#〜]:これで、GOTOコマンドとCALLコマンドをNotBatch.txtファイルでシミュレートできるようになりました!!! この投稿 を参照してください。

アントニオ

17
Aacini

私の場合、Windowsで拡張子のないファイル(* .cmd、*。exeのみ)を実行するために、.cmdを含めるためのpathext変数(システム変数内)を見逃しました。追加すると、file.cmdを実行するのは単にファイルするだけです。

環境変数->。cmd; .exeを含めるためにシステム変数を追加/編集します(もちろん、ファイルはパスにある必要があります)

1
Chandu

はい、可能かもしれませんが、おそらく簡単な方法ではありません=)まず第一に..セキュリティを引き起こします。私は数年前と数ヶ月前に同じことをしようとしましたが、それについての解決策が見つかりませんでした..あなたがやろうとすることができます

execu.cmd

type toLaunch.txt >> bin.cmd
call bin.cmd
pause > nul
exit

次にtoLaunch.txtに入れます

@echo off
echo Hello!
pause > nul
exit

例と同じように、コードを「コンパイル」してから、「出力」ファイルを実行します。つまり、「解析」するだけです。

解析する代わりに、useの名前を変更し、toLaunch.txt内を使用してスクリプト内に自動名前変更を配置することもできます。

ren %0 %0.txt

お役に立てば幸いです。

0
Andrea Bori