Page 1 of 1 1
Topic Options
#147114 - 2005-09-05 01:41 PM Read/Write Fileattribs in Excel
Maik Offline
Getting the hang of it

Registered: 2003-01-14
Posts: 87
Hello,

I have a folder with a lot of textfiles in it. Now I want to get the name of the file and the creation date of it written in an excel sheet like this:

textfile1 | creationdate
textfile2 | creationdate

How do I realize this?

Maik

Top
#147115 - 2005-09-05 02:54 PM Re: Read/Write Fileattribs in Excel
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Read the file via READLINE and use SPLIT to separate the two components.
_________________________
There are two types of vessels, submarines and targets.

Top
#147116 - 2005-09-05 02:58 PM Re: Read/Write Fileattribs in Excel
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Ok, first time on excel com but this does the trick. Manual helped a bit also cause the excel stuff is taken from it and altered a bit.
Requires Kix 4.51beta 1 because of the Getfiletime enhancements.
Also uses the DirPlus() and the CountFiles() UDF’s

Kix 4.51 B1
DirPlus() - a recursive dir tool
CountFiles() - Counts the number of specified files in a folder

I’m sure it can be golfed down by some of the more experienced com scripters on KORG but it’s my first time to really use com so be gentle

Code:

Break on

$folder = "c:\new folder"
$count = CountFiles($Folder,'*.*')

$oXL = CreateObject("EXCEL.application")
If @error = 0
$oXL.Visible = 1 ; make Excel visible to the user
$Rc = $oXL.Workbooks.Add ; add a new workbook
$array = "Filename", "Creation date"
$oXL.Range("A1:B1").Value = $array ;add some columns
$files = dirplus("c:\new folder","/s")
For Each $file In $files
$time = GetFileTime($File,1)
?$file
?$createtime
For $i = 1 To $count
$oXL.Cells(($i+1),1).Value = $file
$oXL.Cells(($i+1),2).Value = $time
Next
Next
;Format the worksheet
$oXL.Range("A1:B1").Font.Bold = 1
$Rc = $oXL.Range("A1:B1").EntireColumn.AutoFit
$oXL.UserControl = 1
Else
? @error + " / " @serror
EndIf



Edited by Mart (2005-09-05 03:20 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#147117 - 2005-09-05 04:20 PM Re: Read/Write Fileattribs in Excel
Maik Offline
Getting the hang of it

Registered: 2003-01-14
Posts: 87
Thank you for your input!

Now I got the problem to check the excel file if the read-out filename with the date already exists in the file.

Is this possible overall?

Maik

Top
#147118 - 2005-09-05 04:42 PM Re: Read/Write Fileattribs in Excel
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Sure. There are some read excel udf's in the udf section for doing this task.
Also found a little problem with my code. It writes the name and the date of the last file (alphabetically) only. I only tested it with one file in the folder Like I said it's my first time at excel com so I'm not sure where the error is for now.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#147119 - 2005-09-05 07:27 PM Re: Read/Write Fileattribs in Excel
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Ok, updated the code I used above. It now reads all filenames and not just the last alphabetical one. No need for dirplus UDF. It still uses count files to see if the folder is not empty.
Just need to find a way to get rid of the . and .. rows.
About reading an existing excel sheet and finding if the line already exists you could use ReadExcel2() - Reads from excel I think. Did not use it yet because I don't have that much time right now to find out how to use it inside this script.

Code:

Break on

Call @SCRIPTDIR + "\countfiles().udf"
$folder = "c:\new folder\"
$count = countfiles($folder, "*.*")
$rc = Exist ($folder)

If $rc = "1" AND $count <> "0"
$oXL = CreateObject("EXCEL.application")
If @error = 0
$oXL.Visible = 1
$rc = $oXL.Workbooks.Add
$array = "Filename", "Creation date"
$oXL.Range("A1:B1").Value = $array
$fileName = Dir($folder + "*.*")
While $FileName <> "" AND @ERROR = 0
$i = $i + 1
$filetime = GetFileTime($folder + $filename,1)
$oXL.Cells(($i+1),1).Value = $filename
$oXL.Cells(($i+1),2).Value = $filetime
$FileName = Dir()
Loop
$oXL.Range("A1:B1").Font.Bold = 1
$Rc = $oXL.Range("A1:B1").EntireColumn.AutoFit
$oXL.UserControl = 1
Else
? @error + " / " @serror
EndIf
Else
?"Folder: " + $folder + " does not exist or is empty."
Sleep 3
EndIf



Edited by Mart (2005-09-05 07:48 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.068 seconds in which 0.037 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org