windows dos 脚本 遍历目录 每个文件

想对windows下某个目录里所有的jpg文件转变成pgm,用Python的pilconvert.py进行转换。已经用python写好了遍历,

def scan(arg, dirname, names):

    for file in names:

        ......

        调用pilconvert

 

但是在这里始终没能成功调用pilconvert.py。

 

于是想用dos批处理来遍历所有文件,对每个文件执行pilconvert.py。因为在dos窗口下直接执行pilconvert.py还是可以的。

 

怎么遍历呢,好像windows 提供了一个命令:forfiles,正好符合我的要求,仔细看了下微软的帮助,如下

 

 

Forfiles

Updated: April 25, 2007

Selects and executes a command on a file or set of files. This command is useful for batch processing.

For examples of how to use this command, see Examples .

Syntax

forfiles [/p ] [/m ] [/s] [/c ""] [/d [{+|-}][{|}]]

Parameters

 

Parameter Description

/p

Specifies the path from which to start the search. By default, searching starts in the current working directory.

/m

Searches files according to the specified search mask. The default search mask is *.* .

/s

Instructs the forfiles command to search into subdirectories recursively.

/c ""

Runs the specified command on each file. Command strings should be enclosed in quotation marks. The default command is "cmd /c echo @file" .

/d [{+|-}][{|}]

Selects files with a last modified date within the specified time frame.

  • Selects files with a last modified date later than or equal to (+ ) or earlier than or equal to (- ) the specified date, where Date is in the format MM/DD/YYYY.
  • Selects files with a last modified date later than or equal to (+ ) the current date plus the number of days specified, or earlier than or equal to (- ) the current date minus the number of days specified.
  • Valid values for Days include any number in the range 0–32,768. If no sign is specified, + is used by default.

/?

Displays help at the command prompt.

Remarks

  • Forfiles is most commonly used in batch files.
  • Forfiles /s is similar to dir /s.
  • You can use the following variables in the command string as specified by the /c command-line option.

     

    Variable Description

    @FILE

    File name.

    @FNAME

    File name without extension.

    @EXT

    File name extension.

    @PATH

    Full path of the file.

    @RELPATH

    Relative path of the file.

    @ISDIR

    Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE.

    @FSIZE

    File size, in bytes.

    @FDATE

    Last modified date stamp on the file.

    @FTIME

    Last modified time stamp on the file.

  • With forfiles , you can run a command on or pass arguments to multiple files. For example, you could run the type command on all files in a tree with the .txt file name extension. Or you could execute every batch file (*.bat) on drive C, with the file name "Myinput.txt" as the first argument.
  • With forfiles , you can do any of the following:
    • Select files by an absolute date or a relative date by using the /d parameter.
    • Build an archive tree of files by using variables such as @FSIZEand @FDATE.
    • Differentiate files from directories by using the @ISDIRvariable.
    • Include special characters in the command line by using the hexadecimal code for the character, in 0xHH format (for example, 0x09 for a tab).
  • Forfiles works by implementing the recurse subdirectories flag on tools that are designed to process only a singl


 

于是依样画葫芦写了个脚本,却得到个提示 forfiles 无法识别,既不是内部命令也不是外部命令,仔细检查半天,还是不行,后来再搜,才发现原来xp根本不支持forfiles,真是的,Mircrosoft在帮助里就应该指明适用平台吗。

 

好像可以用在NT/2000, 2003/vista中,

有个帖子介绍了用法 (http://answers.yahoo.com/question/index?qid=20080324215719AAeNAvP)

 

Syntax NT/2000:
FORFILES [-pPath] [-s] [-dDate] [-mMask] [-cCommand]

Syntax 2003/Vista:
FORFILES [-p Path] [-s] [-d Date] [-m Mask] [-c Command]

key
Path : Path to search (default=current folder)

-s : Recurse into sub-folders

Date : This can be
+DDMMYY to select files newer than a given date
(filedate >=DDMMYY) or
-DDMMYY to select files older than a given date
(filedate <=DDMMYY) or
(Recent versions format the date mask as: +MM/DD/YY

+DD to select files newer than DD days ago or
-DD to select files older than DD days ago

Mask : Search mask (wildcards allowed) default=*.*
Command : Command to execute on each file. default="CMD /C Echo @FILE"


Version 1.0 of FORFILES will only search for files newer than a specified date.
Version 1.1 (described above) can search for file dates Newer or Older then a specified date.
The Vista/2003 version of ForFiles is not compatible with Windows 2000

** Some people state they've managed to have it run under Windows XP. I've tried numerous times and it has continually failed.

Hope this helps you understand the command and OS usage better

Source(s):

Microsoft TechNet
MCSE/MCSA

 

唉,为什么偏偏xp不支持呢?

 

你可能感兴趣的:(windows dos 脚本 遍历目录 每个文件)