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

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

问题描述

我正在尝试在 USB 驱动器上创建一个批处理文件,该文件将运行 .exe,将其文本保存到 .txt 文件,然后关闭 .exe.

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 文件.任何帮助将不胜感激,谢谢.

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:

  • 控制台应用程序正在读取 stdin 或文件并写入 stdout 或文件并将错误消息输出到 <强>标准错误.

  • 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.

控制台应用程序的退出代码分配给环境变量ERRORLEVEL,也可以直接评估,例如使用if errorlevel X rem do something

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

Windows System32目录下的很多*.exe就是这样的控制台应用程序,如find.exefindstr.exeping.exe, ...

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

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 目录中的 Windows Explorer explorer.exe.

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

混合应用程序支持这两种界面,因此可以在命令进程中使用,也可以由用户通过 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.

混合应用程序很少见,因为不容易编码.必须通过测试找出混合应用程序在批处理文件中的使用行为.

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%System32ping.exe 127.0.0.1 -n 5
echo.
echo Ping finished pinging the own computer (localhost).
echo.
pause

命令处理会暂停,直到 ping.exe 自行终止,这需要 4 秒.此类应用程序不需要 startcall,除非控制台应用程序应有意在单独的进程中执行.

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 命令处理器立即继续处理批处理文件的下一行.因此,尽管 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%WinRARWinRAR.exe"
echo.
echo User exited WinRAR.
echo.
pause

如果安装在默认安装目录中,此批处理文件会在没有任何参数的情况下启动 WinRAR(通常在批处理文件中没有用处).但是批处理会暂停,直到用户退出 WinRAR,例如单击 WinRAR 应用程序窗口的 X 符号.

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%WinRARWinRAR.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%
egedit.exe

导致打开Windows 注册表编辑器,但下一个命令可以立即在命令提示符窗口中输入.但是在批处理文件中使用此命令会导致批处理暂停,直到用户关闭 Windows 注册表编辑器 的 GUI 窗口.

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.

第一个建议是使用

ResultTextFileName.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.

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

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?

注意:命令 start 将第一个双引号字符串解释为 title 字符串.因此,在将 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%System32ping.exe 127.0.0.1 -n %RetryNumber% >nul
%SystemRoot%System32	askkill.exe /f /im "%~1.exe"
goto :EOF

如果批处理文件仅适用于 Windows Vista 和更高版本的 Windows,也可以使用 timeout 而不是 ping 来延迟.

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%System32ping.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	askkill.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.

  • 调用/?
  • echo/?
  • endlocal/?
  • 转到/?
  • ping/?
  • 设置/?
  • setlocal/?
  • 开始/?
  • taskkill/?

另请参阅有关使用命令重定向运算符的 Microsoft 文章.

See also the Microsoft article about Using command redirection operators.

PS:由于没有可用的应用程序,我没有测试最后两个批处理代码块.

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

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

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