10 年来我没有对批处理文件做过任何事情,但我发现自己需要列出一堆文件的文件大小,这些文件名为 CG100.mpg 虽然 CG999.mpg
必须有一种方法可以让批处理文件使用 FOR 循环一个一个地查看一系列类似命名的文件?
10 年来我没有对批处理文件做过任何事情,但我发现自己需要列出一堆文件的文件大小,这些文件名为 CG100.mpg 虽然 CG999.mpg
必须有一种方法可以让批处理文件使用 FOR 循环一个一个地查看一系列类似命名的文件?
如果您能够使用 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.
绝对有一种简单的方法可以使用 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