2014-03-25 1 views
5
for /f "delims=" %%a in ('"%systemRoot%\system32\find.exe" /?') do @echo %%a 

예, 이전 행이 작동합니다. 그리 유용하지는 않지만 작동합니다. 그러나 또 다른 질문에 대답하는 배치 파일을 작성하려고, 난 이전 라인의 모두windows cmd : 인용 부호가있는 명령에 인용 된 매개 변수가있는/f와 관련된 문제

나는했습니다 The system cannot find the file specified을 반환 이전 라인의 모두

for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" ^< c:\something.txt' ) do @echo %%a 
for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" c:\something.txt' ) do @echo %%a 

The filename, directory name, or volume label syntax is incorrect을 반환

for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" ^< "c:\something.txt"') do @echo %%a 
for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" "c:\something.txt"') do @echo %%a 

같은 것을 직면 따옴표를 없애거나, 두 배로하거나, 백 슬래시를 사용하거나, 역 따옴표로 작은 따옴표로 바꾸고, for 명령에서 해당 옵션을 설정하는 등의 작업을 수행 할 수 없었습니다. 내가 시도한 것들은 실패했다.

실행할 명령이 인용되어 있고 인용 인수가 필요한 경우 실패합니다.

그리고 네, find가 필요하지 않은 surounding 따옴표를 알고, 제거 된 경우, 지난 4 개 라인의은 (출력, delims, 토큰을 무시)

을 작동하지만 따옴표 어디에 경우 명령을 surounding 정말 필요합니다 (그리고 난 8dot3name 동작이 비활성화 된 시스템을 알고), 거기에 어떤 방식으로 작동하게 할 수 있습니까? 나는 무엇을 여기에서 놓치고 있냐?

답변

7

다음은 두 가지 해결책입니다.

1) 큰 따옴표가 있고 제거 된^이스케이프 문자가 있습니다.
2) 경로에서 그대로 찾기를 사용합니다.

for /f %%a in ('""%systemRoot%\system32\find.exe" /c /v "" < "c:\something.txt""') do @echo %%a 

for /f %%a in (' find.exe /c /v "" ^< "c:\something.txt"') do @echo %%a 

for 명령 내에서 명령 줄을 실행하려면 추가 cmd 프로세스를 시작해야합니다.

이상하게도이 세 명령은 더 간단한 상황에서 다르게 실패합니다.

for /f %%a in (' "c:\windows\system32\find.exe" /c /v "" something.txt ') do @echo %%a 

시스템에서 지정된 경로를 찾을 수 없습니다.

for /f %%a in (' "c:\windows\system32\findstr.exe" /n "." something.txt ') do @echo %%a 

디렉토리 이름이 잘못되었습니다.

for /f %%a in (' "c:\windows\notepad" "something.txt" ') do @echo %%a 

'c : \ windows \ notepad ""something.txt'는 내부 또는 외부 명령, 작동 가능한 프로그램 또는 배치 파일로 인식되지 않습니다.

마지막이 하나는 바깥 쪽 따옴표가 제거된다는 단서를 제공합니다.

윈도우 8.1 32 비트

나는 자식 프로세스가 호출 될 때 따옴표 문제가 cmd /? 여기에 설명되어 있습니다 생각 :

If /C or /K is specified, then the remainder of the command line after 
the switch is processed as a command line, where the following logic is 
used to process quote (") characters: 

    1. If all of the following conditions are met, then quote characters 
     on the command line are preserved: 

     - no /S switch 
     - exactly two quote characters 
     - no special characters between the two quote characters, 
      where special is one of: &<>()@^| 
     - there are one or more whitespace characters between the 
      two quote characters 
     - the string between the two quote characters is the name 
      of an executable file. 

    2. Otherwise, old behavior is to see if the first character is 
     a quote character and if so, strip the leading character and 
     remove the last quote character on the command line, preserving 
     any text after the last quote character. 
+2

+1 물론 !! 잊었다! 명령이 새 cmd 인스턴스 내에서 실행 중입니다. 고맙습니다. 나는 이것을 볼 수 없었다. 이 경우 두 번째 옵션 (경로)은 원래의 문제가 실행에 간섭하지 않는 원시'find' 명령과 관련이있는 것으로서 폐기되었습니다. –

+2

'in ('^ ""some cmd.exe ""some | arg "^"')'명령에서 따옴표를 사용하지 않으려면 추가 따옴표를 이스케이프 처리하는 것이 좋습니다. 또한 구체적인 내용은 기억이 나지 않지만 MS 설명서에 약간의 결함이 있다고 생각합니다. 견적 동작을 정확히 설명하지는 않습니다. – dbenham

0

그것의 사용에서 첫 번째 토큰을 넣어 다소 쉽다 for-loop는 따옴표없이 토큰에 넣는다.

for /f "delims=" %%a in (
' if defined OS "C:\my folder with spaces\consoleapp.exe" /param1:"value" ' 
)do @echo %%a 
관련 문제