Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List file information in a text file for all the files in a directory

Tags:

r

I am trying to list all the information in a text file related to the files in a directory. I used the statement below to do this:

file.info(list.files(path="C:\\temp\\dat\\work",pattern="\\.T$))

The output I got was-

          size isdir mode mtime ctime atime  exe
17517.T   NA    NA <NA>  <NA>  <NA>  <NA> <NA>
17530.T   NA    NA <NA>  <NA>  <NA>  <NA> <NA>
17565.T   NA    NA <NA>  <NA>  <NA>  <NA> <NA>

All I want is to get all the information in a text file in the format below-

Date modified  Time modified         size   file name
10/08/2015      02:39 AM            122055    17517.T
10/08/2015      02:39 AM            122662    17530.T
10/01/2015      08:37 PM             76613    17565.T
like image 505
asmi Avatar asked Oct 08 '15 14:10

asmi


1 Answers

You need to tell list.files to return full paths. Without it I can duplicate your behaviour:

> file.info(list.files(path="/home/rowlings/bin",pattern=".sh$"))
                   size isdir mode mtime ctime atime uid gid uname grname
backupStatus.sh      NA    NA <NA>  <NA>  <NA>  <NA>  NA  NA  <NA>   <NA>
checkFetchmail.sh    NA    NA <NA>  <NA>  <NA>  <NA>  NA  NA  <NA>   <NA>
postgresBackup.sh    NA    NA <NA>  <NA>  <NA>  <NA>  NA  NA  <NA>   <NA>
postgresBackup2.sh   NA    NA <NA>  <NA>  <NA>  <NA>  NA  NA  <NA>   <NA>
roxbuild.sh          NA    NA <NA>  <NA>  <NA>  <NA>  NA  NA  <NA>   <NA>

What happens is that list.files returns just the file name, so then file.info is looking in the working directory, not finding the files, and returning NA everywhere.

With full.names=TRUE then list.files returns the full path, so file.info can find the files and report back okay:

> file.info(list.files(path="/home/rowlings/bin",pattern=".sh$", full.names=TRUE))
                                      size isdir mode               mtime
/home/rowlings/bin/backupStatus.sh    1333 FALSE  755 2013-06-03 14:24:47
/home/rowlings/bin/checkFetchmail.sh   427 FALSE  755 2009-01-17 22:18:40
/home/rowlings/bin/postgresBackup.sh    98 FALSE  755 2008-09-11 16:27:55
/home/rowlings/bin/postgresBackup2.sh  206 FALSE  755 2009-09-09 16:27:10
/home/rowlings/bin/roxbuild.sh         850 FALSE  755 2011-03-08 10:11:06
like image 90
Spacedman Avatar answered Nov 14 '22 21:11

Spacedman