ASCI File data and rename files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • GalacTek
    Forum Member
    • Oct 2003
    • 63

    ASCI File data and rename files

    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
    Forum Member
    • Apr 2001
    • 363

    #2
    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
    Attached Files

    Comment

    • GalacTek
      Forum Member
      • Oct 2003
      • 63

      #3
      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.

      Comment

      • GalacTek
        Forum Member
        • Oct 2003
        • 63

        #4
        Some tweaking, but got the job done with your help... Thanks again.

        Comment

        • Ted Sullivan
          Indigo Rose Staff Member
          • Oct 2003
          • 963

          #5
          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.

          Code:
          -- 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 [ ].
          New Release: Setup Factory 9.6 adds Windows 11 compatibility and support for hardware token OV/EV Code Signing certificates.

          Comment

          • Ted Sullivan
            Indigo Rose Staff Member
            • Oct 2003
            • 963

            #6
            Here is the original example:

            Code:
            -- 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
            New Release: Setup Factory 9.6 adds Windows 11 compatibility and support for hardware token OV/EV Code Signing certificates.

            Comment

            • JXBURNS
              Forum Member
              • Apr 2001
              • 363

              #7
              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

              Comment

              Working...
              X