Jump to content

Open any file and check if extansion matched then remove if not ?


Recommended Posts

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>
$Form1 = GUICreate("My Redirect",400,100)
GUISetState(@SW_SHOW)
$openfile = FileOpen(@WorkingDir & "","")
$FileEXT = "uz2"
$noEXT = ""
While 1
                    Dim $szDrive, $szDir, $szFName, $szExt
$filename = _PathSplit($openfile, $szDrive, $szDir, $szFName, $szExt)
$EXTvalue = _ArrayToString($filename,'',4,4)
MsgBox (0,'',"extvaue is "&$EXTvalue)
If $EXTvalue = 'uz2' Then
    MsgBox (0,'','its uz2')
Elseif $EXTvalue = @error Then
msgBox (0,'','its not UZ2')
    EndIf
   $nMsg = GUIGetMsg()
   Switch $nMsg
       Case $GUI_EVENT_CLOSE
           Exit

   EndSwitch
WEnd

.

I am trying to create simple program that will contentiously look for files that are not uz2 extension, if not then remove them.

I cant figure out how to open file in @workdir, check if ext is uz2 or not and then remove it or keep it if it is uz2

Any ideas ?

Thanks

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>
$Form1 = GUICreate("My Redirect",400,100)
GUISetState(@SW_SHOW)
$openfile = FileOpen(@WorkingDir & "","")
$FileEXT = "uz2"
$noEXT = ""
While 1
                    Dim $szDrive, $szDir, $szFName, $szExt
$filename = _PathSplit($openfile, $szDrive, $szDir, $szFName, $szExt)
$EXTvalue = _ArrayToString($filename,'',4,4)
MsgBox (0,'',"extvaue is "&$EXTvalue)
If $EXTvalue = 'uz2' Then
    MsgBox (0,'','its uz2')
Elseif $EXTvalue = @error Then
msgBox (0,'','its not UZ2')
    EndIf
   $nMsg = GUIGetMsg()
   Switch $nMsg
       Case $GUI_EVENT_CLOSE
           Exit

   EndSwitch
WEnd

.

I am trying to create simple program that will contentiously look for files that are not uz2 extension, if not then remove them.

I cant figure out how to open file in @workdir, check if ext is uz2 or not and then remove it or keep it if it is uz2

Any ideas ?

Thanks

Hi,

i would code it like this:

#include <file.au3>

#include <array.au3>

$sPath = @WorkingDir

$filelist = _FileListToArray ($sPath).

For $i = 1 To UBound ($filelist) - 1

If StringInStr ($filelist [$i], ".uz2") = 0 Then FileDelete ($sPath & "\" & $filelist [$i]

Next

Attention: See help FileDelete ("path"): If the "path" passed to FileDelete is a folder, the files therein will be deleted just as if you had used the *.* mask.

So the code above is only useful, if you don't have any subfolders in your @WorkingDir.

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Well... you beat me to it. I was working on a solution but it looks like you beat me. :) I'll go ahead and post mine anyway:

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
   $fileext = StringRight($file, 4)
    If  $fileext == ".au3" Then
        ContinueLoop
    ElseIf $fileext == ".uz2" Then
        ContinueLoop
    EndIf
    FileDelete($file)
WEnd

; Close the search handle
FileClose($search)

Mine does same exact as yours only without any need for arrays. Mine does however require you to have the script in the same directory as the uz2 files and you must change the .au3 to .exe after you compile this. You wouldn't want it to delete itself, now would you? :) lol

Edit: BTW, i copied directly from the help file and then added an if statement so basically it is from the help file.

Edited by netman74501
Link to comment
Share on other sites

Hi,

@netman74501: You wouldn't want it to delete itself, now would you? lol

Hoooooo, :)

i missed that one:

If StringInStr ($filelist [$i], ".uz2") = 0 And StringInStr ($filelist [$i], "MyAutoit.exe") = 0 Then......

;-)

Stefan

P.S: There are always many ways to the target.......

Edited by 99ojo
Link to comment
Share on other sites

Hi,

@netman74501: You wouldn't want it to delete itself, now would you? lol

Hoooooo, :)

i missed that one:

If StringInStr ($filelist [$i], ".uz2") = 0 And StringInStr ($filelist [$i], "MyAutoit.exe") = 0 Then......

;-)

Stefan

P.S: There are always many ways to the target.......

You may have overlooked one critical point, the OP mentioned @WorkingDir NOT @ScriptDir which may or may not be the same Directory. Also @WorkingDir can be set using FileChangeDir() so there is absolutly no need to have the script in the same folder that he is acting on. On top of that, the proper way to handle

StringInStr ($filelist [$i], "MyAutoit.exe") = 0

would be

StringInStr ($filelist [$i], @ScriptName) = 0

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!"

Link to comment
Share on other sites

You may have overlooked one critical point, the OP mentioned @WorkingDir NOT @ScriptDir which may or may not be the same Directory. Also @WorkingDir can be set using FileChangeDir() so there is absolutly no need to have the script in the same folder that he is acting on. On top of that, the proper way to handle

StringInStr ($filelist [$i], "MyAutoit.exe") = 0

would be

StringInStr ($filelist [$i], @ScriptName) = 0

oooo.. Neat. I never thought about @ScriptName. That is better and I did not know about @WorkingDir either. I realized that he asked for @WorkingDir but did not know what it was and didn't bother to look it up. Instead I just assumed it was a way to get the current dir the script is in. My bad. I'll go look it up next time. Should have done that in the first place, me being a newbie and all. Oh well. All's well that ends well. :)
Link to comment
Share on other sites

oooo.. Neat. I never thought about @ScriptName. That is better and I did not know about @WorkingDir either. I realized that he asked for @WorkingDir but did not know what it was and didn't bother to look it up. Instead I just assumed it was a way to get the current dir the script is in. My bad. I'll go look it up next time. Should have done that in the first place, me being a newbie and all. Oh well. All's well that ends well. :)

No problem as long as the OP doesn't get led astray. It's important that people read and understand the available Macros though. You will notice that we are constantly telling people to read the help file and Macros are only one of the many things they will learn about when they do that.

Another thing that was overlooked; the posters compiled script is an in-use file at that point. Have you attempted to delete a file that is in use recently?

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!"

Link to comment
Share on other sites

Well... you beat me to it. I was working on a solution but it looks like you beat me. :) I'll go ahead and post mine anyway:

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
   $fileext = StringRight($file, 4)
    If  $fileext == ".au3" Then
        ContinueLoop
    ElseIf $fileext == ".uz2" Then
        ContinueLoop
    EndIf
    FileDelete($file)
WEnd

; Close the search handle
FileClose($search)

Mine does same exact as yours only without any need for arrays. Mine does however require you to have the script in the same directory as the uz2 files and you must change the .au3 to .exe after you compile this. You wouldn't want it to delete itself, now would you? :) lol

Edit: BTW, i copied directly from the help file and then added an if statement so basically it is from the help file.

Your example is gerat, i love it but i cant make it loop forevers.

Can you please show me how to make this thing never end unless i would press a button or something ?

THanks

Link to comment
Share on other sites

Your example is gerat, i love it but i cant make it loop forevers.

Can you please show me how to make this thing never end unless i would press a button or something ?

THanks

Well, why would you want it to keep going when there is no more files in a given folder? As it is now, it goes through all files in the same folder the script is in. Once it has gone through all files in the folder, it stops...

@GEOSoft:

Indeed, the compiled script cannot delete itself. I never compiled it to check, just went off the behaviour it was exhibiting when running the script in a non-compiled state. (It would delete itself.) I guess I never thought about it really. In an non-compiled state it runs wrapped as AutoIt and not as itself. Thanks for the corrections, I am a newb still as I have played with Autoit off and on and have no other experience with any language except HTML. I guess the blind shouldn't lead the blind. :)

Edited by netman74501
Link to comment
Share on other sites

i tried doing this

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("",200,100)

GUISetState(@SW_SHOW)
$Start = GUICtrlCreateButton ('Start',0,20)
$pathnameinput = GUICtrlCreateInput("",0,0,200,20)
While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
       
    Case $Start
        $Dir = FileSelectFolder ("Specify directory",'')
        $pathnamevalue = GUICtrlRead ($pathnameinput)
        GUICtrlSetData ($pathnameinput, $Dir)
        $search = FileFindFirstFile($pathnamevalue & "*.*")  
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
EndIf

While 1
    If $Start = "3" Then
        MsgBox(0,'','button ID is 3')
    $file = FileFindNextFile($search)
    If @error Then
    MsgBox (0,'ERROR', 'nothing to delete')
    ExitLoop
EndIf
   $fileext = StringRight($file, 4)
    If  $fileext == ".au3" Then
        ContinueLoop
    ElseIf $fileext == ".uz2" Then
        ContinueLoop
    EndIf
    FileDelete($file)
;WEnd
MsgBox (0,'','deleted')
EndIf
Wend
       Case $GUI_EVENT_CLOSE
           Exit

   EndSwitch
WEnd

But it deletes files in script dir and selected folder.

It deletes everything from selected folder lol idono why.

Can you help please ?

Link to comment
Share on other sites

How many file types are you getting in that folder and what are they? Don't include the .uz2 files that you want to keep.

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!"

Link to comment
Share on other sites

How many file types are you getting in that folder and what are they? Don't include the .uz2 files that you want to keep.

there could be 10's or 1000's of files as they continue to be uploaded.

Yes i only want to keep UZ2 but for other folders i would like to have an option to keep multiple other files as well.

It should continue searching all files and never stop unless i press stop button.

Maybe later ill add some LOG window with few lines to see what files were deleted but thats low priority right now.

Thanks for asking, i hope u can help.

Edited by Gettingsmarter
Link to comment
Share on other sites

there could be 10's or 1000's of files as they continue to be uploaded.

Yes i only want to keep UZ2 but for other folders i would like to have an option to keep multiple other files as well.

It should continue searching all files and never stop unless i press stop button.

Maybe later ill add some LOG window with few lines to see what files were deleted but thats low priority right now.

Thanks for asking, i hope u can help.

I think this does what you want:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("",200,100)

GUISetState(@SW_SHOW)
$Start = GUICtrlCreateButton ('Start',0,20)
$pathnameinput = GUICtrlCreateInput("",0,0,200,20)
While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
       
    Case $Start
        $Dir = FileSelectFolder ("Specify directory",'')
        $pathnamevalue = GUICtrlRead ($pathnameinput)
        GUICtrlSetData ($pathnameinput, $Dir)
        FileChangeDir($Dir)
        $search = FileFindFirstFile("*.*")  
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
EndIf

While 1
If $Start = "3" Then
    ;MsgBox(0,'','button ID is 3')
    $file = FileFindNextFile($search)
    If @error Then
    ;MsgBox (0,'ERROR', 'nothing to delete')
    ExitLoop
EndIf
   $fileext = StringRight($file, 4)
    If  $fileext == ".au3" Then
        ContinueLoop
    ElseIf $fileext == ".uz2" Then
        ContinueLoop
    EndIf
    FileDelete($file)
;WEnd
;MsgBox (0,'','deleted')
EndIf
WEnd
Case $GUI_EVENT_CLOSE
    Exit
EndSwitch
WEnd

But it doesn't loop continuously still. I have been working on getting this to loop continuously for the last 2 hours but failed. Maybe I'll work on it tomorrow when I have time and a fresh brain as it is 1:40 in the morning where I am located. :)

Edited by netman74501
Link to comment
Share on other sites

Here's another way of looking at things:

Global $sTargetFolder = @ScriptDir


Global $sTempFolder
Global $i
While 1
    $sTempFolder = $sTargetFolder & "\TempFolder" & $i
    If Not FileExists($sTempFolder) Then
        DirCreate($sTempFolder)
        ExitLoop
    EndIf
    $i += 1
WEnd

FileMove($sTargetFolder & "\*.au3", $sTempFolder)
FileMove($sTargetFolder & "\*.uz2", $sTempFolder)

FileDelete($sTargetFolder) 

FileMove($sTempFolder, $sTargetFolder) 

DirRemove($sTempFolder, 1)
DirRemove($sTempFolder

But if you gonna loop continuously ignore it.

Link to comment
Share on other sites

OK. This is what I have come up with. It doesn't use a GUI but instead a tray icon. You can hover over the tray icon to see what folder is being monitored. Do be warned though, I have yet to figure out how to make it work correctly with sub-folders. Maybe someone else can figure that one out but I do not know how to get around "if the folder has sub-folders then do this on the sub-folder not this". What it does with sub-folders is like above; it deletes EVERYTHING in them. You did not say that you wanted or needed a GUI so I just used a tray icon instead. For me, it was easier then having a GUI. Hope it helps. :)

#NoTrayIcon
#Include <Constants.au3>

Opt("TrayMenuMode",1)

$changefolder = TrayCreateItem("Change Folder")
TrayCreateItem("")
$exit = TrayCreateItem("Exit")

TraySetState()
$file = ""
$Dir = FileSelectFolder ("Specify directory",'')
TraySetToolTip($Dir)
If @error Then
    _close()
EndIf

While 1
        $msg = TrayGetMsg()
    Select
        Case $msg = $changefolder
            $Dir = FileSelectFolder ("Specify directory",'')
            TraySetToolTip($Dir)
            If @error Then
                _close()
            EndIf
            ConsoleWrite($dir)
        Case $msg = $exit
            _close()
        EndSelect

    _run()
    Sleep(100)
WEnd

Func _run()
If $file = "" Then
    FileChangeDir($Dir)
    Global  $search = FileFindFirstFile("*.*")
    ConsoleWrite("Search Changed" & @CRLF)
    $file = FileFindNextFile($search)
    $fileext = StringRight($file, 4)
    ConsoleWrite("File extensions is: " & $fileext & " File is: " & $file & @CRLF)
    If $fileext <> ".uz2" Then
        FileDelete($file)
    EndIf
Else
    $file = FileFindNextFile($search)
    $fileext = StringRight($file, 4)
    ConsoleWrite("File extensions is: " & $fileext & " File is: " & $file & @CRLF)
    If $fileext <> ".uz2" Then
        FileDelete($file)
    EndIf
EndIf
EndFunc

Func _close()
    FileClose($search)
    Exit
EndFunc
Edited by netman74501
Link to comment
Share on other sites

Thanks for your great example. It works like a charm but i need my gui so i cant really use your code.

It was difficult enough for me to create GUI :) so as you can see with my level of experience i have 1000 problems converting your thing into my :)

Your other example with GUI was more clear to me that this one with functions. I have yet learned the use or even need for functions.

All i know is how to use Case "very little i know" I know that if there is variable Button in GUI then what if i create Case with that button variable it will trigger this buttons events.

I just need to add loop to this code you gave me earlier.

Can you please change it as simply as possible so i can learn how to make things loop?

please .....

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>
$Form1 = GUICreate("",200,100)

GUISetState(@SW_SHOW)
$Start = GUICtrlCreateButton ('Start',0,0,200,20)
$pathnameinput = GUICtrlCreateInput("",0,20,200,80)
While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
       
    Case $Start
        $Dir = FileSelectFolder ("Specify directory",'')
        $pathnamevalue = GUICtrlRead ($pathnameinput)
        GUICtrlSetData ($pathnameinput, $Dir)
        FileChangeDir($Dir)
        $search = FileFindFirstFile("*.*")  
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
EndIf

While 1
If $Start = "3" Then
    ;MsgBox(0,'','button ID is 3')
    $file = FileFindNextFile($search)
    If @error Then
    ;MsgBox (0,'ERROR', 'nothing to delete')
    ExitLoop
EndIf
   $fileext = StringRight($file, 4)
    If  $fileext == ".au3" Then
        ContinueLoop
    ElseIf $fileext == ".uz2" Then
        ContinueLoop
    EndIf
    FileDelete($file)
;WEnd
;MsgBox (0,'','deleted')
EndIf
WEnd
Case $GUI_EVENT_CLOSE
    Exit
EndSwitch
WEnd
Edited by Gettingsmarter
Link to comment
Share on other sites

Thanks for your great example. It works like a charm but i need my gui so i cant really use your code.

It was difficult enough for me to create GUI :) so as you can see with my level of experience i have 1000 problems converting your thing into my ;)

Your other example with GUI was more clear to me that this one with functions. I have yet learned the use or even need for functions.

All i know is how to use Case "very little i know" I know that if there is variable Button in GUI then what if i create Case with that button variable it will trigger this buttons events.

I just need to add loop to this code you gave me earlier.

Can you please change it as simply as possible so i can learn how to make things loop?

please .....

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>
$Form1 = GUICreate("",200,100)

GUISetState(@SW_SHOW)
$Start = GUICtrlCreateButton ('Start',0,0,200,20)
$pathnameinput = GUICtrlCreateInput("",0,20,200,80)
While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
       
    Case $Start
        $Dir = FileSelectFolder ("Specify directory",'')
        $pathnamevalue = GUICtrlRead ($pathnameinput)
        GUICtrlSetData ($pathnameinput, $Dir)
        FileChangeDir($Dir)
        $search = FileFindFirstFile("*.*")  
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
EndIf

While 1
If $Start = "3" Then
    ;MsgBox(0,'','button ID is 3')
    $file = FileFindNextFile($search)
    If @error Then
    ;MsgBox (0,'ERROR', 'nothing to delete')
    ExitLoop
EndIf
   $fileext = StringRight($file, 4)
    If  $fileext == ".au3" Then
        ContinueLoop
    ElseIf $fileext == ".uz2" Then
        ContinueLoop
    EndIf
    FileDelete($file)
;WEnd
;MsgBox (0,'','deleted')
EndIf
WEnd
Case $GUI_EVENT_CLOSE
    Exit
EndSwitch
WEnd

I see you've been playing around with the GUI a bit, or is that a fluke? lol. Threw me off when I copied your code and it was different. Anyway, kept what you had for the GUI and made mine into a loop. The reason I had functions in the last one is because it was easier then having the two loops as below. Functions just tell it to jump to a part of the script and do whatever happens to be in that part of the script. Atleast, that is how I understand them... I'm a noob too ya know. In fact, you are the first person I have ever helped on any forum I have ever been on. Feel privileged? lol :) I thought that maybe you just created the GUI to have something in front of you telling you what directory was being used and a way to start the script. Though, I have to admit, the tray icon was much easier to create. Probably 'cause I used functions in that one. The only part I could not get running in this one was the close event but, it works like a charm now. It finally dawned on me, after about an hour of thinking, that it was the second while loop stopping it from working correctly. Wow. I didn't realize I could talk this much, I'll shut up now.

Here's the revised code:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("",200,100)
GUISetState(@SW_SHOW)
$Start = GUICtrlCreateButton ('Start',0,0,200,20)
$pathnameinput = GUICtrlCreateInput("",0,20,200,80)
Global $file = ""
Global $Dir = ""
Global $search = ""

While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $Start
        While 1
            $msg = GUIGetMsg()
            Select
                Case $msg = $GUI_EVENT_CLOSE
                    If $search <> "" Then FileClose($search)
                    Exit
            EndSelect
            If $Dir == "" Then
                Global $Dir = FileSelectFolder ("Specify directory",'')
                If @error Then
                    Exit
                EndIf
                GUICtrlSetData($pathnameinput, $Dir)
            EndIf
            If $file = "" Then
                FileChangeDir($Dir)
                Global  $search = FileFindFirstFile("*.*")
                Global $file = FileFindNextFile($search)
                $fileext = StringRight($file, 4)
                If $fileext <> ".uz2" Then
                    FileDelete($file)
                EndIf
            Else
                Global $file = FileFindNextFile($search)
                $fileext = StringRight($file, 4)
                If $fileext <> ".uz2" Then
                    FileDelete($file)
                EndIf
            EndIf
            Sleep(100)
        WEnd
    Case $msg = $GUI_EVENT_CLOSE
        If $search <> "" Then FileClose($search)
        Exit
    EndSelect
    Sleep(100)
WEnd

P.S.

Sorry for seeming to be overly complicated. I just don't know how else to do this if this is not what you need or want. Also, sorry about all the consolewrite commands in that last post. I use them for seeing what the code is actually doing when. Same concept as using Msgbox to display a value only the code can keep going and doesn't have to wait for you to click ok. Plus you basically have a log to see what went wrong after exiting the program. Anyway, I meant to take them out before posting it.

Edited by netman74501
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...