我有一组基本文件名,每个名称'f'对应两个文件,'f.in'和'f.out'。我想写一个批处理文件(在Windows XP中),它通过所有的文件名,对于每一个它应该:
显示基名'f' 在'f.in'上执行一个动作 在'f.out'上执行另一个操作
除了搜索*之外,我没有任何方法来列出基本文件名集。例如In(或*.out)。
我有一组基本文件名,每个名称'f'对应两个文件,'f.in'和'f.out'。我想写一个批处理文件(在Windows XP中),它通过所有的文件名,对于每一个它应该:
显示基名'f' 在'f.in'上执行一个动作 在'f.out'上执行另一个操作
除了搜索*之外,我没有任何方法来列出基本文件名集。例如In(或*.out)。
当前回答
下面的代码过滤以给定子字符串开头的文件名。它可以通过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
其他回答
在MS服务器中有一个常用的工具(据我所知)叫做forfiles:
上面的链接包含帮助以及微软下载页面的链接。
下面的代码过滤以给定子字符串开头的文件名。它可以通过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
在nathan post上扩展。以下将在一个批处理文件中完成工作批次。
@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
当在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命令的链接:link
如果我没有回答正确,我很抱歉。这个问题对我来说很难理解。
假设你有两个程序来处理这两个文件,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(页面中间)或下一个答案中查看其他修饰语。