2012-11-10 62 views
21

窗口中是否有任何方法可以通過它來執行不帶* .bat擴展名的批處理腳本?如何使用* .bat擴展名運行批處理腳本

+4

嗯,你可以將其重命名爲'.cmd'文件... –

+3

該腳本必須具有擴展名(.BAT或.CMD)才能讓Windows知道它是批處理腳本。我不認爲有可能將自己的擴展名定義爲批處理文件,也不能指示Windows僅將任何文件用作批處理腳本。 – dbenham

+0

@dbenham沒有兄弟,我還沒有測試,我通過我的電話回覆。但是我相信我早些時候看起來像.batch擴展。但我GOOGLE了一下,發現我錯了。謝謝你糾正我。 –

回答

11

這對我來說是一個有趣的話題!我想對此做一些觀察。

重要的一點是:批處理文件是擴展名爲.BAT或.CMD的文件。期。除了執行通常的DOS命令外,批處理文件還可以實現以下特定的批處理文件功能:

  • 通過%1%2 ...訪問批處理文件參數並執行SHIFT命令。
  • 執行GOTO命令。
  • 執行CALL:NAME命令(內部子程序)。
  • 執行SETLOCAL/ENDLOCAL命令。

現在有趣的部分:任何文件都可以重定向爲CMD.exe的輸入,因此包含在其中的DOS命令以類似於批處理文件的方式執行,但有一些差異。最重要的是前面的批處理文件設施將不起作用。另一個差異是在下面的NOT-批處理文件中所示(I稱之爲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

EDIT更多額外的觀察(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

有趣!不是嗎?

編輯:現在,可以在NotBatch.txt文件中模擬GOTO和CALL命令!見this post

安東尼

0

這可能是可能肯定的,但可能也沒有一個簡單的方法=)引起首先..安全的。 我在一年前嘗試做同樣的事情,並在一個月前,但我沒有找到解決方案。你可以嘗試做

execu.cmd

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

然後在toLaunch.txt把

@echo off 
echo Hello! 
pause > nul 
exit 

就像例如,它將 「編譯」 的代碼,然後它會執行「輸出「文件,這就是」解析「

而不是解析你也可以只是重命名使用,也許把腳本內部自動重命名使用內部toLaunch.txt

ren %0 %0.txt 

希望它有幫助!

0

在我的情況下,爲了使windows運行文件沒有擴展名(僅適用於* .cmd,* .exe),我錯過了pathext變量(在系統可變參數中)以包含.cmd。一旦添加,我沒有更多的運行file.cmd比簡單的文件。

環境變量 - >添加/編輯系統變量包括.CMD,.EXE(ofcourse你的文件應該在路徑)

相關問題