home > 基礎編 >

ForNext

Shut the fuck up and write some code

指定フォルダ以下のファイルを列挙する (更新時刻も表示)

VBScript

JScript

更新日 : 2008.08.02
var fs = WScript.CreateObject("Scripting.FileSystemObject");
putsDis(fs, WScript.Arguments(0));

function putsDis(fs, path)
{
    var folder = fs.GetFolder(path);

    var files   = folder.Files;
    var enmFile = new Enumerator(files)
    for (; !enmFile.atEnd(); enmFile.moveNext())
    {
        var date = new Date(enmFile.item().DateLastModified);
        var y    = date.getFullYear();
        var m    = date.getMonth() + 1;
        var d    = date.getDate();
        var h    = date.getHours();
        var n    = date.getMinutes();
        var s    = date.getSeconds();

        y = LPad(y,"0000") + "\/";
        m = LPad(m,"00")   + "\/";
        d = LPad(d,"00")   + " ";
        h = LPad(h,"00")   + ":";
        n = LPad(n,"00")   + ":";
        s = LPad(s,"00")   + " ";

        WScript.Echo(y + m + d + h + n + s + enmFile.item().Path);
    }
    delete enmFile;

    var subFolders = folder.SubFolders;
    var enmFolder  = new Enumerator(subFolders)
    for (; !enmFolder.atEnd(); enmFolder.moveNext())
    {
        putsDis(fs, enmFolder.item().Path);
    }
    delete enmFolder;
}

function LPad(source, pad)
{
    source = pad + source;
    source = source.substr(source.length - pad.length, pad.length);
    return source;
}
S:\>cscript s:\lesson004.js t:\js //nologo
2008/07/16 10:28:02 t:\js\chapter001\jscript.bat
2008/07/03 12:54:15 t:\js\chapter001\lesson001.js
2008/07/03 12:58:09 t:\js\chapter001\lesson002.js
2008/07/03 12:58:15 t:\js\chapter001\lesson003.js
2008/07/03 12:58:22 t:\js\chapter001\lesson004.js
2008/07/03 12:58:27 t:\js\chapter001\lesson005.js
2008/07/17 11:04:04 t:\js\chapter001\lesson011.js
2008/07/08 13:39:50 t:\js\chapter001\test.txt
2008/07/07 23:05:36 t:\js\chapter002\jscript.bat
2008/07/03 13:23:23 t:\js\chapter002\lesson001.js
2008/07/03 14:25:46 t:\js\chapter002\lesson002.js
2008/07/03 14:32:02 t:\js\chapter002\lesson003.js
2008/07/07 16:31:38 t:\js\chapter002\lesson004.js
2008/07/07 16:35:08 t:\js\chapter002\lesson005.js
2008/07/07 23:07:22 t:\js\chapter002\lesson006.js
2008/07/08 14:01:56 t:\js\chapter003\jscript.bat
2008/07/08 00:01:55 t:\js\chapter003\lesson001.js
2008/07/08 00:17:42 t:\js\chapter003\lesson002.js
2008/07/08 09:58:28 t:\js\chapter003\lesson003.js
2008/07/08 13:27:22 t:\js\chapter003\lesson004.js
2008/05/22 23:41:42 t:\js\chapter003\lesson005.js
2008/07/08 14:00:10 t:\js\chapter003\lesson006.js
2008/07/24 11:03:22 t:\js\chapter004\jscript.bat
2008/07/08 16:10:54 t:\js\chapter004\lesson001.js
2008/07/08 16:14:42 t:\js\chapter004\lesson002.js
2008/07/08 16:17:13 t:\js\chapter004\lesson003.js
2008/07/09 14:33:51 t:\js\chapter004\lesson004.js
2008/07/10 09:51:41 t:\js\chapter004\lesson006.js
2008/07/23 15:20:02 t:\js\chapter004\lesson007.js
2008/07/23 15:38:04 t:\js\chapter004\lesson008.js
2008/07/24 11:03:29 t:\js\chapter004\lesson009.js
2008/07/24 11:03:09 t:\js\chapter004\lesson010.js
2008/07/24 11:02:53 t:\js\chapter004\lesson011.js
2008/08/02 09:58:19 t:\js\chapter005\jscript.bat
2008/08/02 09:58:17 t:\js\chapter005\lesson001.js
2008/08/02 09:58:11 t:\js\chapter005\lesson002.js
2008/08/02 09:58:04 t:\js\chapter005\lesson003.js
2008/08/02 10:45:09 t:\js\chapter005\lesson004.js

Perl

更新日 : 2008.08.02
putsDir($ARGV[0]);

sub putsDir
{
    my($path) = @_;
    if (-d $path)
    {
        opendir HANDLE, $path;
        my @fileList = readdir HANDLE;

        foreach $file (@fileList)
        {
            next if ($file eq ".");  # 自分
            next if ($file eq ".."); # 親

            putsDir("$path\\$file");
        }

        closedir HANDLE, $path;
    }
    else
    {
        @fileInfo = stat $path;
        ($s, $n, $h, $d, $m, $y) = localtime($fileInfo[9]);
        $y += 1900;
        $m += 1;
        printf "%.4d\/%.2d\/%.2d %.2d:%.2d:%.2d %s", $y, $m, $d, $h, $n, $s, "$path\n";
    }
}
S:\>perl s:\lesson004.pl t:\perl
2008/07/09 22:14:38 t:\perl\chapter001\lesson001.pl
2008/07/09 22:15:21 t:\perl\chapter001\lesson002.pl
2008/07/03 10:38:47 t:\perl\chapter001\lesson003.pl
2008/07/09 22:15:35 t:\perl\chapter001\lesson004.pl
2008/07/17 13:42:56 t:\perl\chapter001\lesson011.pl
2008/07/17 11:24:32 t:\perl\chapter001\perl.bat
2008/07/03 14:42:40 t:\perl\chapter002\lesson001.pl
2008/07/03 15:00:24 t:\perl\chapter002\lesson002.pl
2008/07/03 14:42:34 t:\perl\chapter002\lesson003.pl
2008/07/03 22:34:54 t:\perl\chapter002\lesson004.pl
2008/07/03 22:34:51 t:\perl\chapter002\lesson005.pl
2008/07/03 22:24:58 t:\perl\chapter002\perl.bat
2008/05/21 11:36:05 t:\perl\chapter003\lesson001.pl
2008/05/22 15:34:11 t:\perl\chapter003\lesson002.pl
2008/05/22 15:33:46 t:\perl\chapter003\lesson003.pl
2008/07/08 13:17:09 t:\perl\chapter003\lesson004.pl
2008/07/08 13:32:01 t:\perl\chapter003\lesson005.pl
2008/05/23 00:01:06 t:\perl\chapter003\lesson006.pl
2008/07/08 13:45:13 t:\perl\chapter003\perl.bat
2008/07/08 13:32:23 t:\perl\chapter003\test.txt
2008/07/08 15:26:03 t:\perl\chapter004\lesson001.pl
2008/07/08 15:24:41 t:\perl\chapter004\lesson002.pl
2008/07/09 10:00:59 t:\perl\chapter004\lesson004.pl
2008/07/10 14:43:08 t:\perl\chapter004\lesson006.pl
2008/07/25 14:17:28 t:\perl\chapter004\lesson007.pl
2008/07/25 14:55:02 t:\perl\chapter004\lesson008.pl
2008/07/25 14:53:40 t:\perl\chapter004\lesson009.pl
2008/07/25 15:08:14 t:\perl\chapter004\lesson010.pl
2008/07/25 15:05:29 t:\perl\chapter004\lesson011.pl
2008/07/25 15:14:47 t:\perl\chapter004\perl.bat
2008/07/30 12:07:44 t:\perl\chapter005\lesson001.pl
2008/07/30 12:09:30 t:\perl\chapter005\lesson002.pl
2008/07/30 13:18:42 t:\perl\chapter005\lesson003.pl
2008/07/30 16:09:18 t:\perl\chapter005\lesson004.pl
2008/07/30 15:41:08 t:\perl\chapter005\perl.bat

PHP

更新日 : 2008.08.02
<?php
putsDir($argv[1]);

function putsDir($dir)
{
    if (is_dir($dir))
    {
        $dh  = opendir($dir);

        while ($file = readdir($dh))
        {
            if ($file == ".")  continue;  # 自分
            if ($file == "..") continue;  # 親

            putsDir("$dir\\$file");
        }

        closedir($dh);
    }
    else
    {
        print date("Y\/m\/d H:i:s ", filemtime($dir)) . "$dir\n";
    }
}
?>
S:\>php s:\lesson004.php t:\php
2008/07/03 10:41:31 t:\php\chapter001\lesson001.php
2008/07/03 10:41:49 t:\php\chapter001\lesson002.php
2008/07/03 10:42:11 t:\php\chapter001\lesson003.php
2008/07/08 13:54:45 t:\php\chapter001\lesson004.php
2008/07/03 10:44:46 t:\php\chapter001\lesson005.php
2008/07/03 10:49:46 t:\php\chapter001\lesson006.php
2008/07/03 10:47:16 t:\php\chapter001\lesson007.php
2008/07/03 10:47:39 t:\php\chapter001\lesson008.php
2008/07/03 10:48:00 t:\php\chapter001\lesson009.php
2008/07/17 13:29:39 t:\php\chapter001\lesson011.php
2008/07/17 13:30:23 t:\php\chapter001\php.bat
2008/07/08 13:36:02 t:\php\chapter001\test.txt
2008/07/03 14:51:41 t:\php\chapter002\lesson001.php
2008/07/03 14:51:29 t:\php\chapter002\lesson002.php
2008/07/03 14:51:35 t:\php\chapter002\lesson003.php
2008/07/03 23:09:01 t:\php\chapter002\lesson004.php
2008/07/03 23:31:42 t:\php\chapter002\lesson005.php
2008/07/03 23:31:34 t:\php\chapter002\lesson006.php
2008/07/03 23:31:47 t:\php\chapter002\php.bat
2008/07/07 23:56:49 t:\php\chapter003\lesson001.php
2008/07/08 00:13:28 t:\php\chapter003\lesson002.php
2008/07/08 09:53:00 t:\php\chapter003\lesson003.php
2008/07/08 13:17:57 t:\php\chapter003\lesson004.php
2008/05/22 23:39:18 t:\php\chapter003\lesson005.php
2008/07/08 13:47:09 t:\php\chapter003\lesson006.php
2008/07/08 14:13:36 t:\php\chapter003\lesson007.php
2008/07/07 23:56:49 t:\php\chapter003\lesson011.php
2008/07/08 14:17:18 t:\php\chapter003\php.bat
2008/07/09 12:39:13 t:\php\chapter004\lesson001.php
2008/07/09 13:02:52 t:\php\chapter004\lesson003.php
2008/07/09 11:31:53 t:\php\chapter004\lesson004.php
2008/07/09 12:43:47 t:\php\chapter004\lesson005.php
2008/07/10 14:14:07 t:\php\chapter004\lesson006.php
2008/07/25 11:10:58 t:\php\chapter004\lesson007.php
2008/07/25 11:28:28 t:\php\chapter004\lesson008.php
2008/07/25 14:03:27 t:\php\chapter004\lesson009.php
2008/07/25 14:07:57 t:\php\chapter004\lesson010.php
2008/07/25 14:09:17 t:\php\chapter004\lesson011.php
2008/07/25 14:10:42 t:\php\chapter004\php.bat
2008/08/01 10:48:10 t:\php\chapter005\lesson001.php
2008/08/01 10:49:20 t:\php\chapter005\lesson002.php
2008/08/01 10:50:19 t:\php\chapter005\lesson003.php
2008/08/01 11:10:56 t:\php\chapter005\lesson004.php
2008/08/01 10:38:02 t:\php\chapter005\php.bat

Python



Ruby

更新日 : 2008.08.02
def putsDir(path)
    if FileTest.directory?(path)
        Dir.open(path) do |dir|
            dir.each do |name|
                next if name == "."  # 自分
                next if name == ".." # 親

                putsDir path + "/" + name
            end
        end
    else
        print File.mtime(path).strftime("%Y/%m/%d %H:%M:%S"), " ", path.gsub(/\//, "\\"), "\n"
    end                                                                       
end

putsDir(ARGV[0])
S:\>ruby s:\lesson004.rb t:\ruby
2008/07/03 12:29:53 t:\ruby\chapter001\lesson001.rb
2008/07/03 12:29:56 t:\ruby\chapter001\lesson002.rb
2008/07/03 12:30:40 t:\ruby\chapter001\lesson003.rb
2008/07/03 12:31:50 t:\ruby\chapter001\lesson004.rb
2008/07/03 12:32:39 t:\ruby\chapter001\lesson005.rb
2008/07/03 12:33:36 t:\ruby\chapter001\lesson006.rb
2008/07/03 12:34:38 t:\ruby\chapter001\lesson007.rb
2008/07/03 12:35:19 t:\ruby\chapter001\lesson008.rb
2008/07/03 12:35:54 t:\ruby\chapter001\lesson009.rb
2008/07/03 12:36:31 t:\ruby\chapter001\lesson010.rb
2008/07/17 13:51:42 t:\ruby\chapter001\lesson011.rb
2008/07/17 13:46:42 t:\ruby\chapter001\ruby.bat
2008/07/08 13:37:54 t:\ruby\chapter001\test.txt
2008/07/03 15:01:59 t:\ruby\chapter002\lesson001.rb
2008/07/03 15:14:33 t:\ruby\chapter002\lesson002.rb
2008/07/03 15:14:39 t:\ruby\chapter002\lesson003.rb
2008/07/05 11:55:37 t:\ruby\chapter002\lesson004.rb
2008/07/05 11:55:18 t:\ruby\chapter002\lesson005.rb
2008/07/05 11:55:26 t:\ruby\chapter002\ruby.bat
2008/07/07 23:58:47 t:\ruby\chapter003\lesson001.rb
2008/07/08 00:15:52 t:\ruby\chapter003\lesson002.rb
2008/07/08 09:55:48 t:\ruby\chapter003\lesson003.rb
2008/07/08 13:21:53 t:\ruby\chapter003\lesson004.rb
2008/07/08 13:37:34 t:\ruby\chapter003\lesson005.rb
2008/07/08 13:57:34 t:\ruby\chapter003\lesson006.rb
2008/07/08 14:01:59 t:\ruby\chapter003\ruby.bat
2008/07/10 10:21:41 t:\ruby\chapter004\lesson001.rb
2008/07/09 13:59:31 t:\ruby\chapter004\lesson004.rb
2008/07/10 11:44:02 t:\ruby\chapter004\lesson006.rb
2008/07/24 15:44:41 t:\ruby\chapter004\lesson007.rb
2008/07/24 15:44:49 t:\ruby\chapter004\lesson008.rb
2008/07/24 15:50:34 t:\ruby\chapter004\lesson009.rb
2008/07/24 15:56:01 t:\ruby\chapter004\lesson010.rb
2008/07/24 15:56:03 t:\ruby\chapter004\lesson011.rb
2008/07/24 15:59:11 t:\ruby\chapter004\ruby.bat
2008/07/08 13:37:54 t:\ruby\chapter004\test.txt
2008/07/29 16:00:36 t:\ruby\chapter005\lesson001.rb
2008/07/29 16:00:03 t:\ruby\chapter005\lesson002.rb
2008/07/29 16:33:31 t:\ruby\chapter005\lesson003.rb
2008/07/30 09:37:16 t:\ruby\chapter005\lesson004.rb
2008/07/29 16:34:01 t:\ruby\chapter005\ruby.bat

PowerShell

Scala

更新日 : 2012.05.15
def putDir(file: java.io.File) = {
    System.out.print(new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss ").format(new java.util.Date(file.lastModified())));
    System.out.println(file);
}

def getAllFiles(file: java.io.File): Seq[java.io.File] = file.isDirectory match {
    case true  => file.listFiles.toSeq.flatMap(getAllFiles)
    case false => Seq(file)
}

getAllFiles(new java.io.File("u:\\test")).map(putDir)
scala> def putDir(file: java.io.File) = {
     |     System.out.print(new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss ").format(new java.util.Date(file.lastModified())));
     |     System.out.println(file);
     | }
putDir: (file: java.io.File)Unit

scala> def getAllFiles(file: java.io.File): Seq[java.io.File] = file.isDirectory match {
     |     case true  => file.listFiles.toSeq.flatMap(getAllFiles)
     |     case false => Seq(file)
     | }
getAllFiles: (file: java.io.File)Seq[java.io.File]

scala> getAllFiles(new java.io.File("u:\\test")).map(putDir)
2012/05/14 14:48:43 u:\test\01 フォルダ\01 フォルダ\01 ファイル.txt
2012/05/14 14:48:43 u:\test\01 フォルダ\02 ファイル.txt
2012/05/14 14:48:43 u:\test\02 ファイル.txt
res3: Seq[Unit] = ArrayBuffer((), (), ())

F#

更新日 : 2012.05.15
let putDir f =
    let dt = System.IO.File.GetLastWriteTime(f)
    System.Console.Write     (dt.ToString("yyyy/MM/dd HH:mm:ss "))
    System.Console.WriteLine f
System.IO.Directory.GetFiles(@"u:\test", "*.*", System.IO.SearchOption.AllDirectories)
    |>  Seq.iter putDir
> let putDir f =
-     let dt = System.IO.File.GetLastWriteTime(f)
-     System.Console.Write     (dt.ToString("yyyy/MM/dd HH:mm:ss "))
-     System.Console.WriteLine f
- System.IO.Directory.GetFiles(@"u:\test", "*.*", System.IO.SearchOption.AllDirectories)
-     |>  Seq.iter putDir
- ;;
2012.05.14 14:48:43 u:\test\02 ファイル.txt
2012.05.14 14:48:43 u:\test\01 フォルダ\02 ファイル.txt
2012.05.14 14:48:43 u:\test\01 フォルダ\01 フォルダ\01 ファイル.txt

val putDir : string -> unit

C

C++

C++Builder

VC++

C#

更新日 : 2012.05.15
using System;
using System.IO;
 
class lesson0004
{
    static void Main(string[] args)
    {
        foreach (string f in Directory.GetFiles(@"u:\test", "*.*", System.IO.SearchOption.AllDirectories))
        {
            Console.Write(System.IO.File.GetLastWriteTime(f).ToString("yyyy/MM/dd HH:mm:ss "));
            Console.WriteLine(f);
        }
    }
}

T:\>csc lesson0004.cs /nologo

T:\>lesson0004
2012.05.14 14:48:43 u:\test\02 ファイル.txt
2012.05.14 14:48:43 u:\test\01 フォルダ\02 ファイル.txt
2012.05.14 14:48:43 u:\test\01 フォルダ\01 フォルダ\01 ファイル.txt

Java

更新日 : 2012.05.15
import java.io.*;
 
public class lesson0004 {
    public static void main(String args[]) { // throws Exception {

        String path = "u:\\test";

        getAllFiles(new File(path));
    }

    static void getAllFiles(File file) {
        if (file.isDirectory()) {
            for (File f : new File(file.getPath()).listFiles()) {
                getAllFiles(f);
            }
        } else {
            System.out.print(new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss ").format(new java.util.Date(file.lastModified())));
            System.out.println(file);
        }
    }

}
T:\>javac lesson0004.java

T:\>java -cp . lesson0004
2012/05/14 14:48:43 u:\test\01 フォルダ\01 フォルダ\01 ファイル.txt
2012/05/14 14:48:43 u:\test\01 フォルダ\02 ファイル.txt
2012/05/14 14:48:43 u:\test\02 ファイル.txt

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system