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

  • 显示基本名称“f”
  • 对“f.in”执行操作
  • 对“f.out”执行另一个操作

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

有帮助吗?

解决方案

假设您有两个程序处理这两个文件,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 (页面中间)或在下一个答案中。

其他提示

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

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

一旦你拥有了 %%I 变量很容易对其执行命令(只需将单词 echo 替换为您的程序)

此外,替换可变参考已得到增强,您现在可以使用以下可选语法:

%~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 路径可以用其他有效值代替。这 %~ 语法以有效的 FOR 变量名终止。选择大写变量名称,例如 %I 使其更具可读性,并避免与修饰符混淆,而修饰符不敏感。

您可以通过输入以下内容获取完整文档 FOR /?

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

根据 for /?帮助,可以使用 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 循环中执行多行块的页面。我现在要回去重写一些批处理文件......

有一个在 MS 服务器中通常使用的工具(据我记得)称为 福菲莱斯:

上面的链接包含帮助以及 Microsoft 下载页面的链接。

扩展于 内森斯 邮政。以下将在一个批处理文件中完成大量作业。

@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

下面的代码过滤以给定子字符串开头的文件名。可以通过处理 subfname 子字符串提取和 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

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

::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 命令的链接:关联

如果我没有正确回答这个问题,我深表歉意。这个问题对我来说很难读。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top