我在10年内没有对批处理文件进行任何操作,但是我发现自己需要列出一堆文件的文件大小,通过CG999命名为CG100.mpg。 mpg批处理文件显示一系列命名相似的文件的属性
必须有办法让一个批处理文件逐个查看一系列类似命名的文件,使用FOR循环吗?
我在10年内没有对批处理文件进行任何操作,但是我发现自己需要列出一堆文件的文件大小,通过CG999命名为CG100.mpg。 mpg批处理文件显示一系列命名相似的文件的属性
必须有办法让一个批处理文件逐个查看一系列类似命名的文件,使用FOR循环吗?
绝对有一个简单的办法让你的结果使用FOR - 阅读帮助结束处的FOR循环扩展修饰符 - 从命令行输入HELP FOR
或FOR /?
。
你甚至不需要批处理文件。这一个班轮将不正是你想要在命令行上什么:
for /l %N in (100 1 999) do @for %F in (GC%N.mpg) do @if exist %F echo %F size = %~zF
改变所有的%
到%%
如果您在批处理文件中使用的命令。
命令更加简单,如果你只是想列出匹配模式GC*.mpg
的所有文件大小:
for %F in (GC*.mpg) do @echo %F size = %~zF
你给的第一个答案正是我会做的,如果我能记住如何使用一个变量作为文件名的一部分。你的第二个答案非常紧凑。谢谢! – 2012-07-08 19:53:02
@RichHarrison - 如果您的问题已得到满意答复,请不要忘记接受答案,方法是单击答案左上角附近的复选框。该行为可让其他人知道该问题已得到解答,它会奖励您抽出2点时间接受,并将答案海报奖励15分。只能接受1个答案。一旦你积累了15分,你将有权投票选出任何有用的答案,无论是你的问题的答案,还是你在网站上找到的答案。您可以对同一个问题投多个答案。 – dbenham 2012-07-08 20:03:00
如果你能够利用Python的那么下面将工作:
from os.path import getsize
results = [('CG%d.mpg = ' % i) + str(getsize('CG%d.mpg' % i)) for i in range(100, 999)]
print results
否则,对于批处理文件,你可以使用FORFILES:
Select a file (or set of files) and execute a command on each file. Batch processing.
Syntax
FORFILES [/p Path] [/m Mask] [/s] [/c Command] [/d [+ | -] {dd/MM/yyyy | dd}]
Key /p Path The Path to search (default=current folder)
/s Recurse into sub-folders
/C command The command to execute for each file.
Wrap the command string in double quotes.
Default = "cmd /c echo @file"
The Command variables listed below can also be used in the
command string.
/D date Select files with a last modified date greater than or
equal to (+), or less than or equal to (-),
the specified date using the "dd/MM/yyyy" format;
/D + dd Select files with a last modified date greater than or
equal to the current date plus "dd" days. (in the future)
/D - dd Select files with a last modified date less than or
equal to the current date minus "dd" days. (in the past)
A valid "dd" number of days can be any number in
the range of 0 to 32768. (89 years)
"+" is taken as default sign if not specified.
Command Variables:
@file The name of the file.
@fname The file name without extension.
@ext Only the extension of the file.
@path Full path of the file.
@relpath Relative path of the file.
@isdir Returns "TRUE" if a file type is a directory,
and "FALSE" for files.
@fsize Size of the file in bytes.
@fdate Last modified date of the file.
@ftime Last modified time of the file.
FORFILES看起来很有希望,我不知道它存在,所以非常感谢。我喜欢Python代码的紧凑外观,但我还没有学习或安装Python。 – 2012-07-08 18:48:20
可以只使用一个批处理文件?那么Python或Perl呢? – RobB 2012-07-08 18:22:42