批处理文件,用于显示一系列具有相似名称的文件的属性

时间:2012-07-08 18:20:02

标签: batch-file

我在10年内没有对批处理文件做过任何事情,但我发现自己需要列出一堆文件的文件大小,名为CG100.mpg虽然CG999.mpg

必须有办法让批处理文件使用FOR循环逐个查看一系列类似命名的文件吗?

2 个答案:

答案 0 :(得分:0)

如果您能够使用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.

答案 1 :(得分:0)

绝对有一种使用FOR获取结果的简单方法 - 阅读帮助末尾的FOR循环扩展修饰符 - 从命令行输入HELP FORFOR /?

您甚至不需要批处理文件。这个衬管将在命令行中完全按照您的要求执行:

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