FOR / F关闭CMD的批处理文件,COMSPEC已经设置 [英] cmd batch file for/f closes, comspec is already set
问题描述
所以香港专业教育学院一直在寻找解决这个问题一段时间了,我到处看看大家只是说:设置compsec指向CMD......这是超级有益的事业竟没有一个人甚至说该怎么做。
so ive been searching for the solution to this problem for awhile now, everywhere i look everyone just says "set the compsec to point to cmd"...which is super helpful cause no one actually even says how to do that.
但是当我打开CMD,然后键入set和按回车,就说明COMSPEC = C:\\ WINDOWS \\ SYSTEM32 \\ CMD.EXE
but when i open cmd, and type "Set" and hit ENTER, it shows ComSpec=C:\Windows\system32\cmd.exe
我检查了那里,果然,CMD.EXE就在那里,它工作得很好。但FOR / F仍然关闭执行任何操作之前。
I checked there and sure enough, cmd.exe is in there, it works just fine. But for/f still closes before performing any operation.
我该如何解决这个问题?
How do I fix this?
@echo off
for /f "tokens=2*" %%a in ('dir /b /s findstr "Find Me Testing"') do set "AppPath=%%~b"
set "AppPath=%AppPath%"
echo %AppPath%
for /f "tokens=2*" %%a in ('dir /b /s /a-d ^| findstr "Find Me Testing"') do set "AppPath=%%~b"
set "AppPath=%AppPath%"
echo %AppPath%
pause
for /f "usebackq" %a in ('dir /b /s /a-d ^| findstr "To Be Deleted.me"') do set fileLocation=%~pa
echo %fileLocation%
pause
pause
stop
pause
wait 50
正如你可以看到我一直在测试在做我想做的各种方法。
As you can see I've been testing various methods of doing what I want.
推荐答案
我打好可能性,你的问题与cmd.exe的自动运行功能。
I lay good odds that your problem is with the cmd.exe autorun feature.
如果你打开一个命令会话并输入 CMD /
,然后在大约第5款,你会看到以下内容:
If you open a command session and enter cmd /?
, then at about the 5th paragraph you will see the following:
If /D was NOT specified on the command line, then when CMD.EXE starts, it
looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
我愿意打赌,那些两个注册表设置之一被设置为命令或脚本,是造成你的问题。编辑您的注册表,并删除这些设置,你的问题应该走了。
I'd be willing to bet that one of those two registry settings is set to a command or script that is causing your problem. Edit your registry and remove those settings, and your problem should go away.
您可以看到具有的 https://blogs.msdn.microsoft.com/oldnewthing/20071121-00/?p=24433 。
在FOR / F命令通过一个新的cmd.exe进程你的内执行你的命令('...')条款,而该过程将一直运行任何自动运行的设置,可能是present。不幸的是不可能禁用此FOR / F功能 - 我认为这是一个可怕的设计缺陷
The FOR /F command executes your commands within your IN('....') clause via a new cmd.exe process, and that process will always run any autorun setting that may be present. Unfortunately it is impossible to disable this FOR /F "feature" - I think this is a horrible design flaw.
Windows管道还用子CMD.EXE进程 - 一个用于管道的两侧。但CMD.EXE的管道实例包括 / D
选项,这样自动运行是禁用的。您可以通过在命令行中运行以下命令看到这一点:
Windows pipes also use child cmd.exe processes - one for each side of the pipe. But the pipe instantiation of cmd.exe includes the /D
option, so autorun is disabled. You can see this by running the following command from the command line:
echo %^cmdcmdline% | findstr "^"
在我的机器上它产生以下内容:
On my machine it produces the following:
C:\WINDOWS\system32\cmd.exe /S /D /c" echo %cmdcmdline% "
现在做FOR / F等同(健康的机器上)
Now do the equivalent with FOR /F (on a healthy machine)
for /f "delims=" %a in ('echo %^cmdcmdline%') do @echo %a
我的机器生产:
C:\WINDOWS\system32\cmd.exe /c echo %cmdcmdline%
没有 / D
选项:(
这篇关于FOR / F关闭CMD的批处理文件,COMSPEC已经设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!