批处理脚本-将在一个目录中找到的所有文件写到一个命令行中? [英] batch script - write all files found in one directory into one command line?

查看:56
本文介绍了批处理脚本-将在一个目录中找到的所有文件写到一个命令行中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录,里面有一堆带有扩展名的文件.我只想使用扩展名为* .abc的文件.然后,应将每个* .abc文件使用某些参数移交给另一个软件.每个文件的参数始终相同.不过,其中一个参数需要由用户定义.

所以我的第一次尝试是这样:

  @ECHO OFFset/p value =输入所需的估算值:"为(* .abc)中的%% f做(START C:\程序文件" \ Software \ startsoftware.exe -parameter1"%%〜nf.abc" -parameter2%value%-parameter3)暂停 

该脚本有效,但会导致内存崩溃,因为该软件基本上可以一次获取所有请求.但是,如果我可以在一个命令行中写入所有文件名,则该软件将一一处理所有文件.它需要像这样被调用:

  START C:\程序文件" \ Software \ startsoftware.exe -parameter1 file1.abc -parameter2%value%-parameter3 -parameter1 file2.abc -parameter2%value%-parameter3 -parameter1 file3.abc-parameter2%value%-parameter3 -parameter1 file4.abc -parameter2%value%-parameter3 

我的想法是生成一个使用列出所有* .abc的files.txt

  dir/b/a-d>files.txt 

,然后将该列表读入我的命令.但是,我不知道如何读取files.txt并将包含变量%value%的参数应用于每个文件.

解决方案

1.在参数字符串中加引号

参数字符串内的

"通常是不正确的.整个参数字符串通常必须用双引号引起来,而不仅仅是部分引号引起来.所以 C:\" Program Files是错误的正确的"\ Software \ startsoftware.exe " C:\ Program Files \ Software \ startsoftware.exe".

这可以通过打开命令提示符,并输入 C:\ Prog并按下 TAB 键,以使Windows命令处理器完成"C:\ Program Files" 的路径.Windows命令处理器会自动在整个路径字符串周围添加所需的双引号.再次按 TAB 键,路径将更改为"C:\ Program Files(x86)" .但是,继续输入通过输入 \ soft 显示的"C:\ Program Files" ,然后再次按 TAB ,显示的是"C:\ Program Files \ Software" .第二个"移动到新路径的末尾.键入next \ start ,然后再按一次 TAB .现在显示" C:\ Program Files \ Software \ startsoftware.exe",这是该可执行文件的正确的完全限定文件名,由于路径中的空格字符,因此根据需要用双引号引起来.

有关Windows命令处理器此功能的更多信息,请在命令提示符窗口 cmd/?中运行,并从第一页顶部到最后一页底部阅读输出帮助.

2.START和TITLE字符串

在命令提示符窗口中运行 start/?时,将输出命令 START 的帮助.

START 将第一个双引号字符串解释为控制台窗口的可选标题字符串.因此,建议先在命令名称 START 之后始终指定双引号的标题.如果启动了Windows GUI应用程序,而根本没有打开任何控制台窗口,或者在后台执行了控制台应用程序而没有打开新的控制台窗口,则可以仅用" 指定标题字符串在 START 之后,它只是一个空的标题字符串.

3.并行运行应用程序

命令 START 用于运行与正在处理批处理文件的Windows命令进程并行的应用程序或脚本.这通常很有用,但绝对不是在此处应执行应用程序来处理大量文件中的一个文件,这些文件需要全部处理.

对于每个* .abc文件,以下命令行将从可执行文件 startsoftware.exe 开始,与正在处理批处理文件的 cmd.exe 并行执行.

(* .abc)中%% f的

 执行START""C:\ Program Files \ Software \ startsoftware.exe" -parameter1"%%〜nf.abc" -parameter2%value%-parameter3 

在Windows由于资源问题而导致Windows无法再运行一个进程的情况下,这会导致当前目录中有许多* .abc文件.

4.连续运行应用程序

通常最好在处理许多文件时运行用于处理文件的应用程序并停止批处理文件的处理,直到应用程序完成并终止自身为止.可以通过不使用命令 START 来实现.

  @echo关闭setlocal EnableExtensions DisableDelayedExpansion如果不存在* .abc(回声错误:文件夹中没有* .abc:%CD%"回声/暂停转到:EOF)设置值=:用户提示设置/P"Value =输入所需的估算值:"如果未定义,请转到"UserPrompt"设置值=%值:" =%如果未定义,请转到"UserPrompt"对于(* .abc)中的%% I,请执行"C:\ Program Files \ Software \ startsoftware.exe" -parameter1"%% I" -parameter2%Value%" -parameter3本地 

从批处理文件中启动可执行文件的行为与在命令提示符窗口中启动可执行文件的行为不同.Windows命令处理器在处理批处理文件期间等待启动的可执行文件的自终止.因此,与使用命令 START 在短时间内快速启动多个实例的上述循环相比,此代码始终仅运行 startsoftware.exe 的一个实例.

5.正在运行包含多个文件的应用程序

似乎可以运行带有多个参数的 startsoftware.exe 来一次处理多个文件.但是,在编写批处理文件时必须考虑最大命令行长度限制为8191个字符,该批处理文件运行可执行文件并带有参数列表,以一次处理多个文件.

  @echo关闭setlocal EnableExtensions DisableDelayedExpansion如果不存在* .abc(回声错误:文件夹中没有* .abc:%CD%"回声/暂停转到:EOF)设置值=:用户提示设置/P"Value =输入所需的估算值:"如果未定义,请转到UserPrompt设置值=%值:" =%如果未定义,请转到"UserPrompt"设置参数=设置"CmdLineLimit =对于/F"eol = | delims =" %% I in('dir * .abc/A-D/B 2 ^> nul'),请调用:AppendFile"%% I"如果已定义,则参数"C:\ Program Files \ Software \ startsoftware.exe"%Arguments%转到:EOF:AppendFileset Arguments =%Arguments%-parameter1%1 -parameter2%Value%" -parameter3设置"CmdLineLimit =%Arguments:〜7800,1%"如果未定义,则CmdLineLimit转到:EOF"C:\ Program Files \ Software \ startsoftware.exe"%Arguments%设置参数=设置"CmdLineLimit =转到:EOF 

(*.abc)do中的%% f的循环在此代码中被修改为/F 循环,以首先获取加载的文件名列表完全处理到内存中,而不是处理目录条目,如果它修改了当前目录中的* .abc文件,则每次执行 startsoftware.exe 时都可能会更改.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,然后在此处执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • 呼叫/?
  • dir/?
  • echo/?
  • 用于/?
  • 转到/?
  • 如果/?
  • 暂停/?
  • 设置/?
  • setlocal/?

另请参见 GOTO:EOF返回哪里?

I have a directory with a bunch of files with a mix of extension. I only want to work with files with extension *.abc. Each *.abc file should then be handed over to another software with some parameters. The parameters are always the same for each file. One of the parameters needs to be defined by the user, though.

So my first try was this:

@ECHO OFF
set /p value="Enter required imput value: "
for %%f in (*.abc) do (
START C:\"Program Files"\Software\startsoftware.exe -parameter1 "%%~nf.abc" -parameter2 %value% -parameter3
)
PAUSE

The script works but is causing a memory crash, as the software is getting all request basically at once. However, if I could manage to write all file names in one command line the software would process all files one by one. It needs to be called like this:

START C:\"Program Files"\Software\startsoftware.exe -parameter1 file1.abc -parameter2 %value% -parameter3 -parameter1 file2.abc -parameter2 %value% -parameter3 -parameter1 file3.abc -parameter2 %value% -parameter3 -parameter1 file4.abc -parameter2 %value% -parameter3

My idea was to generate a files.txt with listing all *.abc using

dir /b /a-d > files.txt

and then read that list into my command. However, I don't know how to read out the files.txt and apply parameters including the variable %value% to each file.

解决方案

1. Quote inside an argument string

" inside an argument string is usually not correct. The entire argument string must be usually enclosed in double quotes and not just parts of it. So wrong is C:\"Program Files"\Software\startsoftware.exe and correct would be "C:\Program Files\Software\startsoftware.exe".

That can be seen by opening a command prompt, typing C:\Prog and hitting key TAB to let Windows command processor complete the path to "C:\Program Files". The Windows command processor added automatically the required double quotes around entire path string. The path would change to "C:\Program Files (x86)" on pressing once more key TAB. However, continue typing with "C:\Program Files" displayed by entering \soft and press again TAB and displayed is "C:\Program Files\Software". The second " moved to end of new path. Type next \start and press once more TAB. Now is displayed "C:\Program Files\Software\startsoftware.exe" which is the correct full qualified file name of this executable enclosed in double quotes as required because of the space character in path.

For more information about this feature of Windows command processor run in command prompt window cmd /? and read the output help from top of first page to bottom of last page.

2. START and TITLE string

The help for command START is output on running start /? in a command prompt window.

START interprets the first double quoted string as optional title string for the console window. For that reason it is advisable to specify first after command name START always a title in double quotes. In case of a Windows GUI application is started on which no console window is opened at all or a console application is executed in background without opening a new console window, the title string can be specified with just "" after START which is just an empty title string.

3. Running applications parallel

The command START is used to run an application or script parallel to the Windows command process which is processing the batch file. This is often useful, but definitely not here on which an application should be executed to process a file of a large set of files which need to be processed all.

The following command line would start for each *.abc file the executable startsoftware.exe for execution parallel to cmd.exe which is processing the batch file.

for %%f in (*.abc) do START "" "C:\Program Files\Software\startsoftware.exe" -parameter1 "%%~nf.abc" -parameter2 %value% -parameter3

This results with many *.abc files in current directory in a situation on which Windows fails to run one more process due to a resource problem as too many processes are running already more or less parallel.

4. Running application in series

It is usually better on processing many files to run an application for processing a file and halt processing of the batch file until the application finished and terminated itself. That can be achieved by not using the command START.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist *.abc (
    echo ERROR: There are no *.abc in folder: "%CD%"
    echo/
    pause
    goto :EOF
)
set "Value="
:UserPrompt
set /P "Value=Enter required imput value: "
if not defined Value goto UserPrompt
set "Value=%Value:"=%"
if not defined Value goto UserPrompt
for %%I in (*.abc) do "C:\Program Files\Software\startsoftware.exe" -parameter1 "%%I" -parameter2 "%Value%" -parameter3
endlocal

The behavior on starting an executable from within a batch file is different to doing that from within a command prompt window. The Windows command processor waits for the self-termination of the started executable on being started during processing of a batch file. Therefore this code runs always just one instance of startsoftware.exe in comparison to the loop above using command START to start multiple instances quickly in a short time.

5. Running application with multiple files

It looks like it is possible to run startsoftware.exe with multiple arguments to process several files at once. But the maximum command line length limit of 8191 characters must be taken into account on writing a batch file which runs the executable with a list of arguments to process multiple files at once.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist *.abc (
    echo ERROR: There are no *.abc in folder: "%CD%"
    echo/
    pause
    goto :EOF
)
set "Value="
:UserPrompt
set /P "Value=Enter required imput value: "
if not defined Value goto UserPrompt
set "Value=%Value:"=%"
if not defined Value goto UserPrompt

set "Arguments="
set "CmdLineLimit="
for /F "eol=| delims=" %%I in ('dir *.abc /A-D /B 2^>nul') do call :AppendFile "%%I"
if defined Arguments "C:\Program Files\Software\startsoftware.exe"%Arguments%
goto :EOF

:AppendFile
set Arguments=%Arguments% -parameter1 %1 -parameter2 "%Value%" -parameter3
set "CmdLineLimit=%Arguments:~7800,1%"
if not defined CmdLineLimit goto :EOF
"C:\Program Files\Software\startsoftware.exe"%Arguments%
set "Arguments="
set "CmdLineLimit="
goto :EOF

The loop for %%f in (*.abc) do is modified in this code to a for /F loop to get first a list of file names loaded completely into memory instead of processing the directory entries which could change on each execution of startsoftware.exe if it modifies the *.abc files in current directory.

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.

  • call /?
  • dir /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?

See also Where does GOTO :EOF return to?

这篇关于批处理脚本-将在一个目录中找到的所有文件写到一个命令行中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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