如何在批处理文件中循环匹配通配符的文件

时间:2008-09-02 14:12:03

标签: command-line windows-xp batch-file

我有一组基本文件名,每个名称'f'都有两个文件,'f.in'和'f.out'。我想编写一个批处理文件(在Windows XP中),该文件遍历所有文件名,对于每个文件名应该是:

  • 显示基本名称'f'
  • 对'f.in'
  • 执行操作
  • 对'f.out'
  • 执行其他操作

除了搜索* .in(或* .out)之外,我没有办法列出基本文件名集。

7 个答案:

答案 0 :(得分:269)

假设您有两个程序来处理这两个文件:process_in.exe和process_out.exe:

for %%f in (*.in) do (
    echo %%~nf
    process_in "%%~nf.in"
    process_out "%%~nf.out"
)

%% ~nf是一个替换修饰符,它只将%f扩展为文件名。 请参阅https://technet.microsoft.com/en-us/library/bb490909.aspx(页面中间)或下一个答案中的其他修饰符。

答案 1 :(得分:80)

您可以使用此行打印桌面内容:

FOR %%I in (C:\windows\desktop\*.*) DO echo %%I 

获得%%I变量后,可以轻松地对其执行命令(只需将echo替换为您的程序)

此外,FOR变量引用的替换已得到增强 您现在可以使用以下可选语法:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only (directory with \)
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string


[https://ss64.com/nt/syntax-args.html][1]

在上面的示例%I中,PATH可以替换为其他有效的 值。 %~语法由有效的FOR变量名终止。 选择大写变量名称,如%I,使其更具可读性 避免与修饰符混淆,修饰符不区分大小写。

您可以输入FOR /?

来获取完整的文档

答案 2 :(得分:5)

我认为最简单的方法是使用for循环调用第二个批处理文件进行处理,将第二个文件传递给基本名称。

根据for /? help,basename可以使用nifty~n选项提取。因此,基本脚本将显示为:

for %%f in (*.in) do call process.cmd %%~nf

然后,在process.cmd中,假设%0包含基本名称并相应地执行操作。例如:

echo The file is %0
copy %0.in %0.out
ren %0.out monkeys_are_cool.txt

在一个脚本中可能有更好的方法可以做到这一点,但对于如何在批处理文件中的单个for循环中提取多个命令,我总是有点朦胧。

编辑:太棒了!我在某种程度上错过了文档中的页面,该页面显示您可以在FOR循环中执行多行块。我现在要回去重写一些批处理文件......

答案 3 :(得分:3)

MS服务器中通常使用的工具(据我记忆所知)称为forfiles

上面的链接包含帮助以及指向微软下载页面的链接。

答案 4 :(得分:3)

扩展Nathans帖子。以下内容将在一个批处理文件中完成作业。

@echo off

if %1.==Sub. goto %2

for %%f in (*.in) do call %0 Sub action %%~nf
goto end

:action
echo The file is %3
copy %3.in %3.out
ren %3.out monkeys_are_cool.txt

:end

答案 5 :(得分:0)

下面的代码过滤以给定子字符串开头的文件名。可以通过处理subfname substring extraction和IF语句来改变它以适应不同的需求:

echo off
rem filter all files not starting with the prefix 'dat'
setlocal enabledelayedexpansion
FOR /R your-folder-fullpath %%F IN (*.*) DO (
set fname=%%~nF
set subfname=!fname:~0,3!
IF NOT "!subfname!" == "dat" echo "%%F"
)
pause

答案 6 :(得分:0)

回显f.in和f.out将分离循环的概念和在for / f循环中使用时不循环的概念。

::Get the files seperated
echo f.in>files_to_pass_through.txt
echo f.out>>files_to_pass_through.txt

for /F %%a in (files_to_pass_through.txt) do (
for /R %%b in (*.*) do (
if "%%a" NEQ "%%b" (
echo %%b>>dont_pass_through_these.txt
)
)
)
::I'm assuming the base name is the whole string "f".
::If I'm right then all the files begin with "f".
::So all you have to do is display "f". right?
::But that would be too easy.
::Let's do this the right way.
for /f %%C in (dont_pass_through_these.txt)
::displays the filename and not the extention
echo %~nC
)

虽然您没有问,但将命令传递到f.in和f.out的好方法是......

for /F %%D "tokens=*" in (dont_pass_through_these.txt) do (
for /F %%E in (%%D) do (
start /wait %%E
)
)

指向所有Windows XP命令的链接:link

如果我没有正确回答,我道歉。这个问题我很难阅读。

相关问题