Sign in to follow this  
Followers 0
Unc3nZureD

Noobie problem :)

11 posts in this topic

#1 ·  Posted (edited)

Hi guys,

Here my code is:

Func _DirGetWrongFiles()
    $ForbiddenFiles = _FileListToArray(@ScriptDir, "*", 1)
    $Count = 1

    While 1
        If StringRight($ForbiddenFiles[$Count], 3) <> "exe" or "cfg" or "ini" or "txt" Then
            $Delete = FileDelete(@ScriptDir & "/" & $ForbiddenFiles[$Count])
            If $Delete = 0 Then Msgbox(0,"","Forbidden File Found" & @CRLF & $ForbiddenFiles[$Count])
        EndIf

       $Count = $Count + 1
       If $Count = $ForbiddenFiles[0] + 1 Then ExitLoop
    WEnd
EndFunc

It will delete every file in the directory. Why?

Edited by Unc3nZureD

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Instead of this

If StringRight($ForbiddenFiles[$Count], 3) <> "exe" or "cfg" or "ini" or "txt" Then

use something like this

$ext = StringRight($ForbiddenFiles[$Count], 3)
If $ext <> "exe" or $ext <> "cfg" or $ext <> "ini" or $ext <> "txt" Then

EDIT: Shouldn't be there AND instead of OR?

Edited by Zedna

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

AND? How can a 3 letter text be 'txt' and 'exe' too?! (for example)

Edited by Unc3nZureD

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

"AND? How can a 3 letter text be 'txt' and 'exe' too?! (for example)"

example

ext=exe

if exe<> exe (false) or exe <>cfg (true)

The result: true or false=always true.

Edited by DicatoroftheUSA

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

In my case I can't understand the 'AND' statement.

for example:

$ext = "exe"

If $ext <> "cfg" or "exe" Then ...

Only one has to be true. if th cfg is false it all the same I just need to get if ANY of the list is true.

EDIT: LEARNING STATEMENTS

Edit2:

Argh, still can't understand :S

Func _DirGetWrongFiles()
$ForbiddenFiles = _FileListToArray(@ScriptDir, "*", 1)
$Count = 1

While 1
$ext = StringRight($ForbiddenFiles[$Count], 3)
If $ext <> "exe" or $ext <> "cfg" or $ext <> "ini" or $ext <> "txt" Then
$Delete = FileDelete(@ScriptDir & "/" & $ForbiddenFiles[$Count])
If $Delete = 0 Then _HackFound("Forbidden Files Found" & @CRLF & $ForbiddenFiles[$Count])
EndIf

$Count = $Count + 1
If $Count = $ForbiddenFiles[0] + 1 Then ExitLoop
WEnd
EndFunc

Still not working :S

Edited by Unc3nZureD

Share this post


Link to post
Share on other sites

Here's some pseudo code to explain why it has to use AND and not OR.

$ext = "exe"
If $ext <> "exe" then delete the file ; pretty simple if it's not exe then delete
If $ext <> "exe" Or $ext <> "cfg" Then delete the file ; using Or says if Either one is true delete the file, in this case, it doesn't match "cfg" so the file is deleted
If $ext <> "exe" AND $ext <> "cfg" then delete the file ; Using AND says if ALL matches are false then delete the file, if ANY extension matches then skip it, because $ext = "exe" that comparison is true, and we don't want to delete the file

You can't just string the conditional checks together the way you did, you have to do a comparison for every extension you're looking for. So you need AND $ext <> before every case.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Computer logic

1 = true

0 or 0 = 0

1 or 0 = 1

1 or 1 = 1

0 and 0 = 0

1 and 0 = 0

1 and 1 = 1

eg:

if exe<> exe or exe <>cfg

is logically equivalent to: 0 or 1 = 1

or another eg:

if abc<> exe or abc <>cfg

is logically equivalent to: 1 or 1 = 1

So effectively your expression is always true.

Share this post


Link to post
Share on other sites

AND? How can a 3 letter text be 'txt' and 'exe' too?! (for example)

I think you are thinking along the lines of "=" (Equal to) where your code is "<>" (Not equal to). Yes, you cannot have $ext = exe AND $ext = txt (for the sake of this conversation). But you can have $ext <> exe AND $ext <> txt.

Share this post


Link to post
Share on other sites

If Not StringInStr("exe cfg ini txt",$ext) Then FileDelete(........)


My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

Using AND makes it so that ALL rules must apply. Using OR makes it so that ANY rules apply. So when you say If $ext <> exe OR $ext <> txt, they negate eachother. So if the file is a .txt, it sees the first statement "If $ext <> exe" so automatically it thinks, "It's not exe so then perform the action." If it is exe, then it looks at the first statement, "it's an exe... look at the next statement", and it sees "If $ext <> txt", "well it's not txt so then perform the action." So those first two statements alone targets all file extensions ;)


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
:shhh: I still make this mistake sometimes.. Luckily I don't have to ask what went wrong.

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