PDA

View Full Version : ASCI File data and rename files



GalacTek
10-01-2004, 01:09 PM
We all know how great these tools are. I use them a lot for hammering fast utility issues that come up. What I am looking to do is read in a ASCII common delimited file and them renaming the file from the data. below is a sample of the data

"PatientID","First","Last","FileName"
"15","Steven","Johnson","2_3_0.TIF"
"15","Linda","Alexander","2_3_1.TIF"
"15","Linda","Alexander","2_3_2.TIF"
"15","Linda","Alexander","2_3_3.TIF"

Then I want to rename the Tif

2_3_0.TIF to 15_Steven_Johnson.TIF

Can anyone help me with reading in the data and creating the loop? Thanks in advance

JXBURNS
10-02-2004, 09:16 AM
I have not tested this but this is way I would do this although probably not perfect. Would have been easier if there had been a simple Awk parser or you had used something like the "|" character as a field delimiter.

Zipped up lua file attached as would not paste properly with indents here.

John

GalacTek
10-02-2004, 11:59 AM
Thanks John... I will advised on Monday when I am back in the office. I agree on the field delimiter, but the format is from another products output and I have no contolr over it. Again thanks for you help and I will let you know how it goes.

GalacTek
10-04-2004, 11:33 AM
Some tweaking, but got the job done with your help... Thanks again.

Ted Sullivan
10-04-2004, 11:45 AM
John,

Just for future reference, in order to keep indents and spaces in forum post, you can use the "code" tags along with copy and paste into the forum.



-- This is some sample code
a = 5;
b = 7;
c = a*b;
if (c > 20) then
d = 100;
else
d = 200;
endif


This is done by enclosing your script between <code> script here </code>, but replacing the angle brackets < > with square brackets [ ].

Ted Sullivan
10-04-2004, 11:46 AM
Here is the original example:



-- Read the contents of a text file to a table.
text_contents = TextFile.ReadToTable("C:\\MyFile.txt");

-- Get the error code of the last action.
error = Application.GetLastError();

-- If an error occurred, display the error code message.
if (error ~= 0) then
Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
else
NumberOfTableItems = Table.Count(text_contents);
for j,k in text_contents do
-- Ignore 1st line as contains headers
if j>1 then
LengthOfLine = String.Length(k);
StartOfField = true;
FieldCount = 0;
for x=1, LengthOfLine do
Char2Test = String.Mid(k,x,1);
if Char2Test == String.Char(34) then
-- Char (34) is quotes
if StartOfField == true then
-- We are working within a field
FieldCount = FieldCount + 1;
StartOfField = false;
else
-- We have come to the end quote
if FieldCount < 4 then
if FieldCount ==1 then
NewFileName = FieldName;
else
NewFileName = NewFileName + "_" + FieldName;
end
FieldName = "";
StartOfField = true;
else
OldFileName = FieldName ;
-- Should now be at end of string
end
end
else
if StartOfField == false then
-- Only add the character if working within a field to ignore the comma delimiters
FieldName=FieldName + Char2Test;
end
end
end
-- Rename the file
File.Rename(OldFileName, NewFileName);

-- Get the error code of the last action.
error = Application.GetLastError();
-- If an error occurred, display the error code message.
if (error ~= 0) then
Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
end
end
end
end

JXBURNS
10-04-2004, 12:08 PM
Ted,

Thanks for that. I run PHPBB on forums that I run on my server so was having problems inserting the variables I ususally use for that in this forum! Shall remember in future as knew it could be done as I had seen your previous examples..

Well at least the code appears to have worked anyway!

Rgds John