2012-04-10 10 views
6

私はfirst.batsecond.batを持っています。 echo %~n0(実行中のバッチの表示名)バッチ(.bat):現在のスクリプトではなく、最初のスクリプトの名前を取得します

出力がSecond.batですが、私はありません、それは自分だ、それは呼び出し元のファイル名を表示したい:call second.bat

第二には、次のとおりです。

first.batです。

これは可能ですか?

答えて

0
あなたは親プロセスIDを取得し、またはKurzedMetalとドラコニックのように、引数や環境変数として何かを渡すための複雑なシステムコールせずにそれを行うことはできません

投稿。関係するシステムコールはthis postで詳述されているので、DLLコールをバッチにインポートする必要があります。

+0

私は、引数を使用していないが、バリCがやったヘルパーバッチファイルを使用して独自のスクリプトで使用してください。 – KurzedMetal

+0

申し訳ありませんが、あなたの答えを誤解しました! – CharlesB

3

ストア呼び出す前に、環境変数に%~n0の値second.bat

3

これを行う最も簡単な方法は、最初のバッチのファイル名を2番目のパラメータにパラメータとして渡すことです。

REM First.bat 
call Second.bat %~n0 

REM Second.bat 
echo %1 

2

このバッチは、呼び出し元スクリプトの名前を検出したり、それは(goto)文がスタックから1つのレベルを削除することを、事実を使用しています

@echo off 
setlocal DisableDelayedExpansion 
set "func=%~0" 
for /F "delims=\" %%X in ("%func:*\=%") do set "func=%%X" 
if ":" == "%func:~0,1%" (
    goto %func% 
) 
REM *** Get the name of the caller 
(
    (goto) 2>nul 
    setlocal DisableDelayedExpansion 
    call set "caller=%%~f0" 
    call set _caller=%%caller:*%%~f0=%% 
    if defined _caller (
     set "callType=batch" 
     call "%~d0\:mainFunc\..%~pnx0" %* 
    ) ELSE (
     set "callType=cmd-line" 
     cmd /c "call "%~d0\:mainFunc\..%~pnx0" %*" 
    ) 
    endlocal 
) 
echo NEVER REACHED 
exit /b 

:mainFunc 
echo :mainFunc of %~nx0 arg1=%1 is called from '%caller%'/%callType% 
exit /b 

コマンドラインから直接呼ばれていたとしても。
この結果、現在のバッチファイルが残ってしまい、%~f0は呼び出し元スクリプトの名前(およびそのバッチの現在の機能である%~0)またはコマンドラインから呼び出された場合は%~f0のテキストになります。

その後、独自のスクリプトでは、ヘルパーバッチファイルを追加することができます簡単に使用するために"%~d0\:mainFunc\..%~pnx0"

外部スクリプト

で再び呼び出されます。
このライン

@echo off 
<:GetCaller <nul call GetCaller.bat myCallerVar 
echo This batch was called from "%myCallerVar%" 

名前GetCaller.bat

@echo off 
setlocal DisableDelayedExpansion 
set "func=%~0" 
for /F "delims=\" %%X in ("%func:*\=%") do set "func=%%X" 
if ":" == "%func:~0,1%" (
    goto %func% 
) 

REM *** STEP1 
REM *** Get the filename of the caller of this script, needed for later restart that 
(
    (goto) 2>nul 
    setlocal DisableDelayedExpansion %= it could be reenabled by the GOTO =% 
    set "_returnVar=%~1" 
    call set "_lastCaller=%%~f0" 
    call set "_argToLastCaller=%%*" 
    call "%~d0\:Step2\..%~pnx0" %* 
) 
exit /b %= This is never reached =% 

:Step2 
REM *** STEP2 
REM *** Get the filename/cmd-line of the caller of the script 
(
    (goto) 2>nul 
    (goto) 2>nul 
    setlocal DisableDelayedExpansion %= it could be reenabled by the GOTO =%  
    set "_returnVar=%_returnVar%" 
    set "_lastCaller=%_lastCaller%" 
    set "_argToLastCaller=%_argToLastCaller%" 
    call set "caller=%%~f0" 
    call set _caller=%%caller:*%%~f0=%% 
    if defined _caller (
     set "callType=batch" 
     call "%~d0\:Step3batch\..%~pnx0" 
    ) ELSE (
     set "callType=cmd-line" 
     cmd /c "call "%~d0\:Step3batch\..%~pnx0" " 
    ) 
    endlocal 
) 
exit /b %= This is never reached =% 

:Step3batch 
REM *** STEP3 Restart the requester batch, but jump to the label :GetCaller 
call :GetCaller 
exit /b %= This is never reached =% 

:GetCaller 
REM *** This uses the trick, that starting a batch without CALL will jump to the last used label 
if "%_returnVar%" NEQ "" set "%_returnVar%=%_caller%" 
%_lastCaller% %_argToLastCaller% 

echo #6 never reached 
+0

他のスクリプトに埋め込む2行または3行だけを単純化する...目的だけでスクリプト名を呼び出すことができます....あなたは例のオンスを呼び出し元のスクリプトの名前にすることができますか? – ZEE

+0

@ZEE呼び出し元のスクリプト名を取得するために現在のバッチを終了してから現在のスクリプトを再起動する必要がありますが、無限ループを回避する必要があるため、2行または3行ではできません。しかし、完全なコードをヘルパーバッチファイルに移動することは可能です。 – jeb

+0

@ZEEヘルパースクリプトを追加しました – jeb

関連する問題