programing tip

Windows 배치 파일 : .bat 대 .cmd?

itbloger 2020. 9. 30. 08:54
반응형

Windows 배치 파일 : .bat 대 .cmd?


내가 알기 .bat로는 이전 16 비트 명명 규칙이며 .cmd32 비트 Windows 용입니다. 즉, NT로 시작합니다. 그러나 나는 .bat 파일을 모든 곳에서 계속 볼 수 있으며 접미사를 사용하여 정확히 동일하게 작동하는 것 같습니다. 내 코드는, 정말 내 배치 파일의 이름을 어떤 방법으로 상관이야 NT보다 오래된 아무것도 실행 할 필요가 없습니다, 또는 일부가 않을 것이라고 가정 잡았다 잘못된 접미사를 사용하여 저를 기다리고은?


에서 이 뉴스 그룹 게시물 에 의해 마크 즈비 코우 스키 자신 :

CMD.EXE에 관한 한 .CMD와 .BAT의 차이점은 다음과 같습니다. 확장을 활성화하면 .CMD 파일의 PATH / APPEND / PROMPT / SET / ASSOC는 오류에 관계없이 ERRORLEVEL을 설정합니다. .BAT는 오류에 대해서만 ERRORLEVEL을 설정합니다.

즉, ERRORLEVEL이 0이 아닌 것으로 설정되고 해당 명령 중 하나를 실행하면 결과 ERRORLEVEL은 다음과 같습니다.

  • .bat 파일에 0이 아닌 값이 그대로 남아 있음
  • .cmd 파일에서 0으로 재설정합니다.

다음은이 스레드의 다양한 답변과 인용 된 참조에서 확인 된 정보 모음입니다.

  1. command.com MS-DOS에 도입 된 16 비트 명령 프로세서이며 Win9x 시리즈 운영 체제에서도 사용되었습니다.
  2. cmd.exeWindows NT의 32 비트 명령 프로세서입니다 (64 비트 Windows OS에도 64 비트 버전이 있음). cmd.exeWindows 9x의 일부가 아닙니다. 그것은 OS / 2 버전 1.0에서 시작되었고, OS / 2 버전 cmd은 16 비트로 시작되었습니다 (그러나 그럼에도 불구하고 같은 명령을 가진 완전한 보호 모드 프로그램이었습니다 start). Windows NT cmd는 OS / 2에서 상속 되었지만 Windows NT의 Win32 버전은 32 비트에서 시작되었습니다. OS / 2는 1992 년에 32 비트가되었지만 cmd16 비트 OS / 2 1.x 프로그램 으로 남아 있습니다.
  3. ComSpecENV 변수를 정의하는 프로그램에 의해 시작됩니다 .bat.cmd스크립트. (WinNT로 시작하면 기본값은 cmd.exe.)
  4. cmd.exe와 역 호환됩니다 command.com.
  5. Windows 9x에서 실수로 실행되는 것을 방지하기 위해 설계된 스크립트의 cmd.exe이름 .cmd지정할 수 있습니다 . 이 파일 이름 확장자는 OS / 2 버전 1.0 및 1987으로 거슬러 올라갑니다.

에서 cmd.exe지원하지 않는 기능 목록은 다음과 같습니다 command.com.

  • 긴 파일 이름 (8.3 형식 초과)
  • 명령 기록
  • 탭 완성
  • 문자를 탈출 : ^(사용을 위해 : \ & | > < ^)
  • 디렉토리 스택 : PUSHD/POPD
  • 정수 산술 : SET /A i+=1
  • 검색 / 바꾸기 / 하위 문자열 : SET %varname:expression%
  • 명령 대체 : FOR /F(이전에 존재했으며 향상되었습니다)
  • 기능 : CALL :label

실행 순서 :

스크립트의 .bat 및 .cmd 버전 (test.bat, test.cmd)이 동일한 폴더에 있고 확장자 (test)없이 스크립트를 실행하는 경우 기본적으로 스크립트의 .bat 버전이 실행됩니다. 64 비트 Windows 7에서 실행 순서는 PATHEXT 환경 변수에 의해 제어됩니다. 자세한 내용은 명령 프롬프트가 파일실행하는 순서 를 참조하십시오.

참조 :

wikipedia : 명령 셸 비교


이 답변은 너무 길고 대화 형 사용에 중점을 둡니다. 스크립팅의 중요한 차이점은 다음과 같습니다.

  • .cmd 비 NT 시스템에서 부주의 한 실행을 방지합니다.
  • .cmd 내장 명령이 성공시 Errorlevel을 0으로 변경하도록합니다.

.cmd 파일에서 사용 가능한 여러 추가 기능도있었습니다. 그러나 명령 확장은 Windows 2000 이상에서 .bat 및 .cmd 파일 모두에서 기본적으로 설정되어 있습니다.

결론 : 2012 년 이후에는 .cmd독점적으로 사용하는 것이 좋습니다 .


아니요-조금도 중요하지 않습니다. NT에서 .bat 및 .cmd 확장자는 모두 cmd.exe 프로세서가 정확히 동일한 방식으로 파일을 처리하도록합니다.

MS TechNet ( http://technet.microsoft.com/en-us/library/cc723564.aspx )의 WinNT 클래스 시스템에서 command.com과 cmd.exe에 대한 추가 흥미로운 정보 :

This behavior reveals a quite subtle feature of Windows NT that is very important. The 16-bit MS-DOS shell (COMMAND.COM) that ships with Windows NT is specially designed for Windows NT. When a command is entered for execution by this shell, it does not actually execute it. Instead, it packages the command text and sends it to a 32-bit CMD.EXE command shell for execution. Because all commands are actually executed by CMD.EXE (the Windows NT command shell), the 16-bit shell inherits all the features and facilities of the full Windows NT shell.


RE: Apparently when command.com is invoked is a bit of a complex mystery;

Several months ago, during the course of a project, we had to figure out why some programs that we wanted to run under CMD.EXE were, in fact, running under COMMAND.COM. The "program" in question was a very old .BAT file, that still runs daily.

We discovered that the reason the batch file ran under COMMAND.COM is that it was being started from a .PIF file (also ancient). Since the special memory configuration settings available only through a PIF have become irrelevant, we replaced it with a conventional desktop shortcut.

The same batch file, launched from the shortcut, runs in CMD.EXE. When you think about it, this makes sense. The reason that it took us so long to figure it out was partially due to the fact that we had forgotten that its item in the startup group was a PIF, because it had been in production since 1998.


Still, on Windows 7, BAT files have also this difference : If you ever create files TEST.BAT and TEST.CMD in the same directory, and you run TEST in that directory, it'll run the BAT file.

C:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

C:\Temp>echo echo bat > test.bat

C:\Temp>echo echo cmd > test.cmd

C:\Temp>test

C:\Temp>echo bat
bat

C:\Temp>

Since the original post was regarding the consequences of using the .bat or .cmd suffix, not necessarily the commands inside the file...

One other difference between .bat and .cmd is that if two files exist with the same file name and both those extensions, then:

  • entering filename or filename.bat at the command line will run the .bat file

  • to run the .cmd file, you have to enter filename.cmd


everything working in a batch should work in a cmd; cmd provides some extensions for controlling the environment. also, cmd is executed by in new cmd interpreter and thus should be faster (not noticeable on short files) and stabler as bat runs under the NTVDM emulated 16bit environment


.cmd and .bat file execution is different because in a .cmd errorlevel variable it can change on a command that is affected by command extensions. That's about it really.


I believe if you change the value of the ComSpec environment variable to %SystemRoot%system32\cmd.exe(CMD) then it doesn't matter if the file extension is .BAT or .CMD. I'm not sure, but this may even be the default for WinXP and above.


Slightly off topic, but have you considered Windows Scripting Host? You might find it nicer.


확장은 차이가 없습니다.

사이에 약간의 차이가 있습니다 COMMAND.COM대 파일 처리는 CMD.EXE.


차이:

.cmd 파일은 실행되기 전에 메모리에로드됩니다. .bat 파일은 한 줄을 실행하고, 다음 줄을 읽고, 해당 줄을 실행합니다.

스크립트 파일을 실행할 때 이것을 발견 한 다음 실행이 완료되기 전에 편집 할 수 있습니다. bat 파일은 이로 인해 엉망이되지만 cmd 파일은 그렇지 않습니다.

참고 URL : https://stackoverflow.com/questions/148968/windows-batch-files-bat-vs-cmd

반응형