为什么不是所有启动的应用程序保存在文本文件中的信息通缉如预期? [英] Why do not all started applications save the wanted information in the text files as expected?

查看:236
本文介绍了为什么不是所有启动的应用程序保存在文本文件中的信息通缉如预期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建将运行.exe,它的文本保存到一个文本文件,然后关闭该.exe USB驱动器上的批处理文件。

I am trying to create a batch file on a USB drive that will run an .exe, save its text to a .txt file, and then close the .exe.

我目前遇到了一个奇怪的问题,只有18名为.exe的5实际上是节省了他们的文本文件。

I am currently running into a weird problem, only 5 of the 18 .exe's are actually saving their text to a file.

这是我使用来完成我的任务的约定:

This is the convention I am using to complete my task:

start IEPassView.exe /stext Results/IEPassView.txt 
taskkill /f /im IEPassView.exe

start MailPassView.exe /stext Results/MailPassView.txt
taskkill /f /im MailPassView.exe

start MessenPass.exe /stext Results/MessenPass.txt
taskkill /f /im MessenPass.exe

start RouterPassView.exe /stext Results/RouterPassView.txt
taskkill /f /im RouterPassView.exe

start ProtectedStoragePassView.exe /stext Results/ProtectedStoragePassView.txt
taskkill /f /im ProtectedStoragePassView.exe

start DialUpPassView.exe /stext Results/DialUpPassView.txt
taskkill /f /im DialUpPassView.exe

我有以上块的18行中的所有调用不同的小程序,即使其中5实际上保存文件没有任何与该保存.cfg文件,因为他们有时做的。任何帮助将大大AP preciated,谢谢。

I have 18 of the above blocks in a row all calling different small programs and even though 5 of them actually save the files none of them save a .cfg file as they sometimes do. Any help would be greatly appreciated, thanks.

推荐答案

主要 3种不同类型的可执行文件:

There are mainly 3 different types of executables:


  • A 控制台应用程序标准输入或一个文件读取和写入的标准输出或一个文件,输出错误信息< STRONG>标准错误

  • A console application is reading from stdin or a file and writing to stdout or a file and outputs error messages to stderr.

一个批处理文件的处理被暂停在开始一个控制台应用程序,直到控制台应用程序自行终止。因此,正确的说法是:呼叫一个控制台应用程序

The processing of a batch file is halted on starting a console application until the console application terminated itself. The correct term is therefore: calling a console application.

控制台应用程序的退出code被分配到环境变量中的 ERRORLEVEL ,它也可以直接评估例如使用如果ERRORLEVEL点¯xREM做点什么

The exit code of the console application is assigned to environment variable ERRORLEVEL and can be also directly evaluated for example with if errorlevel X rem do something

很多的* .exe在Windows中的 System32下目录是这样的控制台应用程序,如 FIND.EXE 的Findstr.exe ping.exe的 ...

Many *.exe in System32 directory of Windows are such console applications, like find.exe, findstr.exe, ping.exe, ...

A GUI (图形用户界面)应用程序的开始作为新的进程,这意味着Windows命令处理器,直到GUI应用程序终止本身并没有停止批处理

A GUI (graphical user interface) application is started as new process which means the Windows command processor does not halt batch processing until the GUI application terminates itself.

这是不容易可以从这些应用程序的命令进程中得到的东西。 GUI应用程序被设计用于经由图形用户界面与用户,而不是通过标准的数据流或文件与命令处理的交互。

It is not easily possible to get something from within a command process from such applications. GUI applications are designed for interacting via a graphical user interface with a user and not via standard streams or files with a command process.

对于这种应用的典型例子是的 Windows资源管理器的Explorer.exe 在Windows目录下。

A typical example for such applications is Windows Explorer explorer.exe in Windows directory.

A 混合应用程序支持两种接口,可以从命令过程中以及由通过GUI用户来使用,因此

A hybrid application supports both interfaces and can be therefore used from within a command process as well as by a user via GUI.

混合应用程序是罕见的,因为不容易code。从一个批处理文件中使用情况的混合应用程序的行为必须找出通过测试。

Hybrid applications are rare because not easy to code. The behavior of hybrid applications on usage from within a batch file must be find out by testing.

举例此类应用程序的 Windows注册表编辑器 REGEDIT.EXE 或共享归档工具的 WinRAR的的WinRAR.exe

Example for such applications are Windows Registry Editor regedit.exe or shareware archiver tool WinRAR WinRAR.exe.

这是最好看的简单的例子,看看就开始分歧/调用所有3种类型的应用程序从一个批处理文件中。

It is best to look on simple examples to see the differences regarding starting/calling all 3 types of applications from within a batch file.

示例控制台应用程序:

@echo off
cls
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 5
echo.
echo Ping finished pinging the own computer (localhost).
echo.
pause

命令处理被停止,直到 ping.exe的终止本身这需要4秒。没有必要为启动呼叫,对于这样的应用,除了控制台应用程序应该在一个单独的过程中有意执行

The command processing is halted until ping.exe terminated itself which takes 4 seconds. There is no need for start or call for such applications, except the console application should be intentionally executed in a separate process.

的GUI应用程序示例:

@echo off
cls
%WinDir%\Explorer.exe
echo.
echo Windows Explorer opened and is still running!
echo.
pause

此批处理文件启动Windows资源管理器,表明Windows资源管理器开始作为独立的进程与Windows命令处理器将立即继续批处理文件的下一行后立即输出文本和消息提示preSS任意键。因此,尽管Windows资源管理器启动,并仍在运行,批量处理继续了。

This batch file outputs the text and message prompt to press any key immediately after starting Windows Explorer indicating that Windows Explorer was started as separate process and Windows command processor immediately continued on the next lines of the batch file. So although Windows Explorer was started and is still running, the batch processing continued, too.

的混合应用程序示例:

@echo off
cls
"%ProgramFiles%\WinRAR\WinRAR.exe"
echo.
echo User exited WinRAR.
echo.
pause

此批处理文件开始的 WinRAR的的,如果没有被安装在所有的默认安装目录中的任何参数(一般不从一个批处理文件中非常有用)。但批量处理暂停,直到用户退出的 WinRAR的的例如通过单击 X 的象征的 WinRAR的的的应用程序窗口。

This batch file starts WinRAR without any parameter (usually not useful from within a batch file) if being installed at all in default installation directory. But batch processing is halted until the user exited WinRAR for example by clicking on X symbol of the WinRAR´s application window.

不过,打开命令提示符窗口,在窗口中从执行

But opening a command prompt window and executing from within the window

"%ProgramFiles%\WinRAR\WinRAR.exe"

结果立即得到提示早在命令窗口中键入并执行下一个命令。所以的 WinRAR的的发现是什么父进程和采取相应的行动。

results in getting immediately the prompt back in command window to type and execute the next command. So WinRAR finds out what is the parent process and acts accordingly.

Windows注册表编辑器的显示相同的行为。从命令提示符窗口中执行

Windows Registry Editor shows the same behavior. Executing from within a command prompt window

%WinDir%\regedit.exe

在开放的 Windows注册表编辑器的,但下一个命令结果可以在命令提示符窗口中立即进入。但在停止批处理批处理文件的结果,直到的GUI窗口中使用此命令的 Windows注册表编辑器的被用户关闭。

results in opening Windows Registry Editor, but next command can be immediately entered in command prompt window. But using this command in a batch file results in halting batch processing until GUI window of Windows Registry Editor is closed by the user.

因此​​混合应用从一个批文件主要与参数内使用,以避免用户交互的必要性。

Therefore hybrid applications are used from within a batch file mainly with parameters to avoid the necessity of user interaction.

好了,回到关于各类应用这个简短的课后的问题。

Okay, back to the question after this brief lesson about various types of applications.

第一个建议是使用

Result\TextFileName.txt

而不是

Result/TextFileName.txt

作为Windows目录分隔符是反斜杠字符,除了可执行文件要求正斜杠因为从Unix / Linux的严重被移植到Windows目录分隔符。

as the directory separator on Windows is the backslash character, except the executables require forward slashes as directory separator because of being badly ported from Unix/Linux to Windows.

第二个建议是找出应用程序的类型。是命令启动真的有必要,因为应用程序不会自行启动在单独的进程,需要用户交互来终止自己?

Second suggestion is finding out type of application. Is command start really necessary because the applications don't start itself in a separate process and need user interaction to terminate itself?

请注意:命令启动间$ P $点第一个双引号的字符串作为标题字符串。因此,这是一件好事,作为指定在启动GUI或混合应用程序作为一个单独的进程第一个参数(空标题字符串)。在启动控制台应用程序作为一个独立的过程中,它是一般一个好主意,让控制台窗口一个有意义的标题。

Note: Command start interprets first double quoted string as title string. Therefore it is always good to specify as first parameter "" (empty title string) on starting a GUI or hybrid application as a separate process. On starting a console application as a separate process it is in general a good idea to give the console window a meaningful title.

而在去年,如果启动的应用程序确实需要用户交互终止,这将是肯定更好要么开始等待开始至1秒或更长时间后,杀了他们,杀死或启动所有这些,等待几秒钟,最后杀死他们的一次。

And last if the started applications really would need user interaction to terminate, it would be definitely better to either start and kill them after waiting 1 or more seconds between start and kill or start them all, wait a few seconds and finally kill them all at once.

例用于启动和单独杀死每个应用程序第一个解决方案:

Example for first solution with starting and killing each application separately:

@echo off
setlocal
set TimeoutInSeconds=3
call :RunApp IEPassView
call :RunApp MailPassView
call :RunApp MessenPass
call :RunApp RouterPassView
call :RunApp ProtectedStoragePassView
call :RunApp DialUpPassView
endlocal
goto :EOF

:RunApp
start "" "%~1.exe" /stext "Results\%~1.txt"
set /A RetryNumber=TimeoutInSeconds + 1
%SystemRoot%\System32\ping.exe 127.0.0.1 -n %RetryNumber% >nul
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF

也可以使用暂停而不是迟迟如果批处理文件仅适用于Windows Vista和更高的Windows版本。

It is also possible to use timeout instead of ping for the delay if the batch file is only for Windows Vista and later Windows versions.

举例开头的所有应用程序,等待几秒钟,终于把他们全部杀死第二个解决方案:

Example for second solution with starting all applications, wait some seconds and kill them finally all:

@echo off
call :StartApp IEPassView
call :StartApp MailPassView
call :StartApp MessenPass
call :StartApp RouterPassView
call :StartApp ProtectedStoragePassView
call :StartApp DialUpPassView

%SystemRoot%\System32\ping.exe 127.0.0.1 -n 6 >nul

call :KillApp IEPassView
call :KillApp MailPassView
call :KillApp MessenPass
call :KillApp RouterPassView
call :KillApp ProtectedStoragePassView
call :KillApp DialUpPassView

goto :EOF

:StartApp
start "" "%~1.exe" /stext "Results\%~1.txt"
goto :EOF

:KillApp
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • 呼叫/?

  • 回声/?

  • ENDLOCAL /?

  • 转到/?

  • 平/?

  • 设置/?

  • SETLOCAL /?

  • 启动/?

  • TASKKILL /?

  • call /?
  • echo /?
  • endlocal /?
  • goto /?
  • ping /?
  • set /?
  • setlocal /?
  • start /?
  • taskkill /?

另请参阅有关 Microsoft文章使用命令重定向操作符

PS:最后两个批处理code块不是由我,因为没有可用的应用程序的测试

PS: The last two batch code blocks were not tested by me because of not having available the applications.

这篇关于为什么不是所有启动的应用程序保存在文本文件中的信息通缉如预期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆