Contents [hide]
出力結果
1 2 3 4 | example_dir1/1.txt example_dir1/example_dir2/4.txt example_dir1/example_dir2/3.txt example_dir1/example_dir2/2.txt |
前準備
以下のようなファイルを用意
1 2 3 4 | example_dir1/1.txt example_dir1/example_dir2/4.txt example_dir1/example_dir2/3.txt example_dir1/example_dir2/2.txt |
スクリプトのパラメーターにはフォルダ名(example_dir1)を指定
プログラム例
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 | var recursive = require( "recursive-readdir" ); let dir = "." ; if ( typeof (process.argv[2]) !== "undefined" ){ dir = process.argv[2]; } else { dir = "." ; } recursive(dir, function (err, files) { for (const i in files) { console.log(files[i]); } }); |
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php if (isset( $argv [1])) { $dir = $argv [1]; } else { $dir = '.' ; } $it = new RecursiveDirectoryIterator( $dir ); foreach ( new RecursiveIteratorIterator( $it ) as $file ) { if (! is_dir ( $file )) { print $file . "\n" ; } } |
Perl
1 2 3 4 5 6 7 8 | use File::Find; my $dir = $ARGV [0] || '/.' ; my $wanted = sub { if (-f $_ ) { print $File::Find::name . "\n" ; } }; &find ( $wanted , $dir , ); |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import sys from os import walk import os import re path = "" try : sys.argv[ 1 ] except : path = "." else : path = sys.argv[ 1 ] def find_files( dir ): for root, dirs, files in walk(path): for file in files: if os.path.isdir( file ): find_files( file ) else : print (root, end = '') if not re.search(r "/$" , root): print ( "/" , end = "") else : pass print ( file ) find_files(path) |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 | require 'find' dir = "" if ARGV [ 0 ] dir = ARGV [ 0 ] else dir = "." end Find.find(dir) do |file| if File .file?(file) puts(file) end end |
プログラミング言語比較サイトProgrammingLang.comでは、同じ問題を複数のプログラミング言語がそれぞれどのような記述で解決できるのかの例を提供。
複数の言語を比較し、貴方の問題を解決するのに最適な言語の選択と、その言語での解法を得る事を手助けします。
全問題カバー: JavaScript Perl PHP Python Ruby | 一部: C C# C++ Go Java Rust Shell
問題解法大分類(50音順)
Class | 時間 | 数値 | System | Database | Test | Network | 配列 | ファイルシステム | 変数 | 文字列
その他役立ちコンテンツ
複数の言語を比較し、貴方の問題を解決するのに最適な言語の選択と、その言語での解法を得る事を手助けします。
全問題カバー: JavaScript Perl PHP Python Ruby | 一部: C C# C++ Go Java Rust Shell
問題解法大分類(50音順)
Class | 時間 | 数値 | System | Database | Test | Network | 配列 | ファイルシステム | 変数 | 文字列
その他役立ちコンテンツ

※当サイトではアフィリエイトプログラムを利用して商品を紹介しています。