Sign in to follow this  
Followers 0
GEOSoft

Arrays Again!

10 posts in this topic

I need to get the file attributes and put them into a message box as complete words.

If the Attribs were set RSH then the message box text would be Read Only / System / Hidden or if the Attribs were A then the text would be Archive. I suspect it should be done as an array but how?

Next one is going to get pretty hairy I think. I have two files, $File1 And $File2. I have to compare the two files. If the first line in $File2 does not appear any place in $File1 then Append it to a new file, $File3 and then repeat the process for the next line with any non-matching lines added to $File3. The whole idea is that there will probably be duplicate entries between $File1 and $File2 so what I want to do is extract differences from $File2.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The quick-n-dirty way for your first question:

$attrib = FileGetAttrib ( "filename" )
If @error Then
   MsgBox(4096,"Error", "Could not get file attributes.")
   Exit
EndIf

$tmp = ""
If StringInStr($attrib, "R") Then $tmp = $tmp & "ReadOnly /"
If StringInStr($attrib, "A") Then $tmp = $tmp & "Archive /"
If StringInStr($attrib, "S") Then $tmp = $tmp & "System /"
If StringInStr($attrib, "H") Then $tmp = $tmp & "Hidden /"
If StringInStr($attrib, "N") Then $tmp = $tmp & "Normal /"
If StringInStr($attrib, "D") Then $tmp = $tmp & "Directory /"
If StringInStr($attrib, "O") Then $tmp = $tmp & "Offline /"
If StringInStr($attrib, "C") Then $tmp = $tmp & "Compressed /"
If StringInStr($attrib, "T") Then $tmp = $tmp & "Temporary /"
$tmp = StringTrimRight($tmp, 2);remove trailing slash

MsgBox(4096,"Attribs", $tmp)
Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

That's a start similar to what I was trying but I still think there must be a better way. Thanks CS The second problem is the one I think is going to get real dirty.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

The second one probably won't be as hard as it is time consuming. No matter how you shake it, you're going to be doing lots of redundant parsing which will grow exponentially based on the file size. You have two easy-to-write options. One would be 2 loops, the outer loop reading one line at a time from $file1, the inner loop runs through every line in $file2 until a match or EOF is found. Then $file1 goes to line 2 and the inner loops runs through $file2 again.

The alternative is very similar, but it involves building 2 arrays, one for each file. Then doing the same 2 loop structure. Only this time, any matching lines can be set to "" or 0 or something so they aren't compared quite as heavily.

Share this post


Link to post
Share on other sites

below scripts would work but will slow down a lot when running against a file with many records...

Are the files sorted?? it would be much easier if both files are sorted, then you could match records...

$file1 = "test1.txt"
$file2 = "test2.txt"
$file3 = "test3.txt"
$fh2=fileopen("test2.txt",0)
If $fh2 = -1 Then
     MsgBox(0, "Error", "Unable to open file2.")
     Exit
EndIf
while 1
   $line2 = FileReadLine($fh2)
    If @error = -1 Then ExitLoop
   $fh1=fileopen($file1,0)
   If $fh1 = -1 Then
        MsgBox(0, "Error", "Unable to open file1.")
        Exit
   EndIf
   while 1
      $line1 = FileReadLine($fh1)
       If @error = -1 Then 
          Filewriteline($file3,$line2) 
          ExitLoop
       endif
      if $Line1 == $Line2 then exitloop   
   Wend
   fileclose($fh1)   
Wend
Fileclose($fh2)

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Are the files sorted?? it would be much easier if both files are sorted, then you could match records...

I wish they were but unfortunately they are usually a jumbled mess. I'm going to try working from your script. At least it's going in the right direction. I can see where I will have to make some changes but not serious ones. For instamce $File3 does not have to be created unless there is a difference but it wouldn't really matter, I could just set it to delete the file if it's empty. It gets even worse after but I have that part figured out, if a line is commented out the line has to be deleted. The good part is that this will be a stand-alone file for the app and the user *should* only have to run it one time, right after the installation.

Thanks a lot.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

For instamce $File3 does not have to be created unless there is a difference but it wouldn't really matter

think that file3 will only be created when there is a record to write...

just for anybody interested... below script will match 2 file that are sorted:

$file1 = "test1.txt"
$file2 = "test2.txt"
$file3 = "test3.txt"
$EOF1 = 0
$EOF2 = 0
$Line1 = ""
$Line2 = ""
;
$fh1=fileopen($file1,0)
If $fh1 = -1 Then
   MsgBox(0, "Error", "Unable to open file1.")
   Exit
EndIf
;
$fh2=fileopen("test2.txt",0)
If $fh2 = -1 Then
   MsgBox(0, "Error", "Unable to open file2.")
   Exit
EndIf
;
read1()
read2()
;
while 1
   select
    ; Both EOF
      Case $EOF1 and $EOF2
         ExitLoop
     ; matching records
      Case $Line1 == $Line2 and not $EOF1 and not $EOF2
         Filewriteline("matches.txt" ,$line1 & "=" & $line2 & @LF) 
         read1()
         read2()
     ; file 1 record not in file2
      Case $Line1 < $Line2 or $EOF2
         Filewriteline("1notin2.txt" ,$line1 & @LF) 
         read1()
     ; file 2 record not in file1
      Case $Line1 > $Line2 or $EOF1
         Filewriteline("2notin1.txt" ,$line2 & @LF) 
         read2()
   endselect   
Wend
fileclose($fh1)   
Fileclose($fh2)   

Func read1()
   $line1 = FileReadLine($fh1)
   If @error = -1 Then
      $EOF1 = 1
    EndIf
EndFunc

Func read2()
   $line2 = FileReadLine($fh2)
   If @error = -1 Then
      $EOF2 = 1
    EndIf
EndFunc

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thought of an alternative to your first question which relies on the fact that the captial letters R A S H N D O C T only appear once in the full names of Read-only Archive etc.

$input = StringSplit("R,A,S,H,N,D,O,C,T",",")
$output = StringSplit("Read-only /, Archive /, System /, Hidden /, Normal /, Directory /, Offline /, Compressed /, Temporary /",",")

$attrib = FileGetAttrib("c:\boot.ini")
For $i = 1 to 9
   $attrib = StringReplace($attrib, $input[$i], $output[$i], 0, 1)
  ; last arg in StringReplace means case sensitive
Next
$attrib = StringTrimRight($attrib, 2)

MsgBox(4096,"Attribs", $attrib)

Of course, this could be wrapped in a user function.


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Much better Thanks CS


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

All it needed was a bit of error checking and that made it work just dandy.

$input = StringSplit("R,A,S,H,N,D,O,C,T",",")
$output = StringSplit("Read-only /, Archive /, System /, Hidden /, Normal /, Directory /, Offline /, Compressed /, Temporary /",",")
$Attrib = FileGetAttrib ($File1)
If @error Then
  MsgBox(4096,"Error", "Unable to get file attributes for "&$File1)
  Exit
EndIf
For $i = 1 to 9
  $attrib = StringReplace($attrib, $input[$i], $output[$i], 0, 1)
; last arg in StringReplace means case sensitive
Next
$attrib = StringTrimRight($attrib, 2)

MsgBox(4096,"Attribs", $attrib)
I was able to shorten my code a bit because the file in question would only have a combination of the first 4 so I just deleted the other 5 and changed the For $i statement. Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0