Sign in to follow this  
Followers 0
tubaguy0

Compare Array to Variable

5 posts in this topic

#1 ·  Posted (edited)

Hello,

How can I compare each string in an array against a variable?

If $LogfileArray[0] <> $FullFilePath AND $FileModifiedDiff <= 2 Then

I can get $LogFileArray[1] or [2] and so on to compare 1 on 1 to $FullFilePath, but not the whole array.

Thanks for any help.

Edited by tubaguy0

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello,

How can I compare each string in an array against a variable?

If $LogfileArray[0] <> $FullFilePath AND $FileModifiedDiff <= 2 Then

I can get $LogFileArray[1] or [2] and so on to compare 1 on 1 to $FullFilePath, but not the whole array.

Thanks for any help.

You still have to compare each element, but you can do it in an efficient loop:
$fMatch = True
For $n = 0 To UBound($LogfileArray) - 1
    If $LogfileArray[$n] <> $FullFilePath Then
        $fMatch = False
        ExitLoop
    EndIf
Next

After that runs, $fMatch = True if all elements match $FullFilePath, or False if not. Because of the ExitLoop, it doesn't bother to compare the rest of the array once it finds a mis-match. So the remaining part is just:

If $fMatch AND ($FileModifiedDiff <= 2) Then

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks so much. I inserted your code, and tried it a few ways, but it's not behaving as expected, and I'm sure it's my fault not yours--maybe you can see what I'm missing?

$fMatch = True
For $n = 0 To UBound($LogfileArray) - 1
If $LogfileArray[$n] <> $FullFilePath Then
$fMatch = False
 ExitLoop
 EndIf
Next
If $fMatch AND ($FileModifiedDiff <= 30) Then ; If any selected file has been modified in 30 days or less
 If StringInStr($FullFilePath, ".pdf", 0) Then ; And is a pdf document
  FileCopy($FullFilePath, $CopyFolder & $FullPathString, 1) ; Copy it to the CopyFolder
  CopiedLogFile($FullFilePath) ; Log its file path
 EndIf
EndIf

That code should be copying a number of files from one directory to another because their modified date is in the last 30 days *except* for the 6 files that are listed in the log file, which are formatted as $FullFilePath.

When I use the code as shown, nothing copies. When I try swapping True and False for your $fMatch variable, all of the files copy.

Does that make any sense?

Thanks again.

Edited by tubaguy0

Share this post


Link to post
Share on other sites

Thanks so much. I inserted your code, and tried it a few ways, but it's not behaving as expected, and I'm sure it's my fault not yours--maybe you can see what I'm missing?

$fMatch = True
For $n = 0 To UBound($LogfileArray) - 1
If $LogfileArray[$n] <> $FullFilePath Then
$fMatch = False
 ExitLoop
 EndIf
Next
If $fMatch AND ($FileModifiedDiff <= 30) Then ; If any selected file has been modified in 30 days or less
 If StringInStr($FullFilePath, ".pdf", 0) Then ; And is a pdf document
  FileCopy($FullFilePath, $CopyFolder & $FullPathString, 1) ; Copy it to the CopyFolder
  CopiedLogFile($FullFilePath) ; Log its file path
 EndIf
EndIf

That code should be copying a number of files from one directory to another because their modified date is in the last 30 days *except* for the 6 files that are listed in the log file, which are formatted as $FullFilePath.

When I use the code as shown, nothing copies. When I try swapping True and False for your $fMatch variable, all of the files copy.

Does that make any sense?

Thanks again.

For $n = 0 To UBound($LogfileArray) - 1
    If $LogfileArray[$n] = $FullFilePath Then
        If $FileModifiedDiff <= 30 Then FileCopy($FullFilePath, $CopyFolder & $FullPathString, 1)
        If StringInStr($FullFilePath, ".pdf", 0) Then CopiedLogFile($FullFilePath)
    EndIf
Next

Untested.

Should copy only files > 30 days old. I think that's what you were after anyway.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for your help PsaltyDS and jebus495... changing the <> to = in PsaltyDS' example resulted in a perfectly working chunk of code that was the last piece of my OCR script. I really, really appreciate the help, that was the last thing I needed to finish my script.

Edited by tubaguy0

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