Sign in to follow this  
Followers 0
Rogue5099

FileFindNextFile

10 posts in this topic

#1 ·  Posted (edited)

I found this little program that resizes pictures (made 4GB to 225MB awesome), sad thing is that it puts them back in the same folder with original picture but with a different file name.

I.E. Picture.jpg to Picture-400.jpg

So I want to FileMove these pictures to another folder with same Directory Tree FileMove("", "", 8).

Problem is moving files from Subfolders

also .jpeg (StringTrim Brain Fart can't figure it out)

$ext = StringTrimLeft($file, StringLen($file) - StringInStr($file, ".")) -- This right?

$searchdir = FileFindFirstFile("*.*")

While 1
    $file = FileFindNextFile($searchdir)
    If @error <> 0 Then ExitLoop
        If StringInStr($file, "-400") > 0 Then
            $file400 = StringTrimRight($file, 8)
            $ext = StringTrimLeft($file, (StringLen($file) - 4))
            $NewFile = $file400 & $ext
            FileMove(@SCRIPTDIR & "\" & $file, $NewDir & $NewFile, 8)
        Else
            ContinueLoop
        EndIf
WEnd
FileClose($searchdir)
Edited by rogue5099

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Rogue5099,

With regard to getting the file extention :

$pos = stringinstr("myfile.sdsdsd",'.',0,-1)
$ext = stringright("myfile.sdsdsd",stringlen("myfile.sdsdsd")-$pos)
consolewrite($ext & @lf)

As for the subfolders, I see that Varian is watching and she can advise you far better than I.

Good Luck,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Rogue5099,

With regard to getting the file extention :

$pos = stringinstr("myfile.sdsdsd",'.',0,-1)
$ext = stringright("myfile.sdsdsd",stringlen("myfile.sdsdsd")-$pos)
consolewrite($ext & @lf)

As for the subfolders, I see that Varian is watching and she can advise you far better than I.

Good Luck,

kylomas

Can't sneak up on you, kylomas!!

For extensions, I use

$Extension = StringRegExpReplace($File, '^.+\.', '')
because not every extension has 3 characters after it.

Rouge5099, are you against using one of the many FileArrayRecursive functions? There are many floating around, and many are not overly complicated. you will end up making your own if you continue down this road. Here is one from Melba23...

;===============================================================================
; $iRetItemType: 0 = Files and folders, 1 = Files only, 2 = Folders only
; $iRetPathType: 0 = Filename only, 1 = Path relative to $sPath, 2 = Full path/filename
;===============================================================================

Func _FileListToArray_Recursive($sPath, $sFilter = "*", $iRetItemType = 0, $iRetPathType = 0, $bRecursive = False)
    Local $sRet = "", $sRetPath = ""
    $sPath = StringRegExpReplace($sPath, "[\\/]+\z", "")
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If StringRegExp($sFilter, "[\\/ :> <\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
    $sPath &= "\|"
    $sOrigPathLen = StringLen($sPath) - 1
    While $sPath
        $sCurrPathLen = StringInStr($sPath, "|") - 1
        $sCurrPath = StringLeft($sPath, $sCurrPathLen)
        $Search = FileFindFirstFile($sCurrPath & $sFilter)
        If @error Then
            $sPath = StringTrimLeft($sPath, $sCurrPathLen + 1)
            ContinueLoop
        EndIf
        Switch $iRetPathType
            Case 1 ; relative path
                $sRetPath = StringTrimLeft($sCurrPath, $sOrigPathLen)
            Case 2 ; full path
                $sRetPath = $sCurrPath
        EndSwitch
        While 1
            $File = FileFindNextFile($Search)
            If @error Then ExitLoop
            If ($iRetItemType + @extended = 2) Then ContinueLoop
            $sRet &= $sRetPath & $File & "|"
        WEnd
        FileClose($Search)
        If $bRecursive Then
            $hSearch = FileFindFirstFile($sCurrPath & "*")
            While 1
                $File = FileFindNextFile($hSearch)
                If @error Then ExitLoop
                If @extended Then $sPath &= $sCurrPath & $File & "\|"
            WEnd
            FileClose($hSearch)
        EndIf
        $sPath = StringTrimLeft($sPath, $sCurrPathLen + 1)
    WEnd
    If Not $sRet Then Return SetError(4, 4, "")
    Return StringSplit(StringTrimRight($sRet, 1), "|")
EndFunc   ;==>_FileListToArray_Recursive
Edited by Varian

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks so mush I have learn more with the help you guys gave. Not to good with Array's even though that's all my "MP3 Inventory" is. Forgot to check there for reclusive.

The program I was using to resize is PhotoResize400.exe. Check out the results below:

Posted Image

Here it is with Melba23 _FiletoArray_Recursive():

$Path = FileSelectFolder("Move files from:", "", 1, @UserProfileDir & "\") & "\"
If @error = 1 Then Exit

$NewDir = FileSelectFolder("Choose a destination folder.", "", 1, @UserProfileDir & "\") & "\"
If @error = 1 Then Exit

;~ =================================================================================
;~        Moves the files from the current directory with -400 in name
;~               to specific folder and renames files without -400
;~ =================================================================================
$Files = _FileListToArray_Recursive($Path, "*.*", 1, 1, True)

For $i = 1 To $Files[0]
    If StringInStr($Files[$i], "-400") > 0 Then
            $ext = "." & StringRight($Files[$i], StringLen($Files[$i]) - StringInStr($Files[$i],'.',0,-1))
            $file400 = StringTrimRight($Files[$i], (StringLen($Files[$i]) - StringInStr($Files[$i],'.',0,-1)) + 5)
            $NewFile = $file400 & $ext
            FileMove($Path & $Files[$i], $NewDir & $NewFile, 8)
    EndIf
Next

Exit
;===============================================================================
; $iRetItemType: 0 = Files and folders, 1 = Files only, 2 = Folders only
; $iRetPathType: 0 = Filename only, 1 = Path relative to $sPath, 2 = Full path/filename
;===============================================================================

Func _FileListToArray_Recursive($sPath, $sFilter = "*", $iRetItemType = 0, $iRetPathType = 0, $bRecursive = False)
    Local $sRet = "", $sRetPath = ""
    $sPath = StringRegExpReplace($sPath, "[\\/]+\z", "")
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If StringRegExp($sFilter, "[\\/ :> <\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
    $sPath &= "\|"
    $sOrigPathLen = StringLen($sPath) - 1
    While $sPath
        $sCurrPathLen = StringInStr($sPath, "|") - 1
        $sCurrPath = StringLeft($sPath, $sCurrPathLen)
        $Search = FileFindFirstFile($sCurrPath & $sFilter)
        If @error Then
            $sPath = StringTrimLeft($sPath, $sCurrPathLen + 1)
            ContinueLoop
        EndIf
        Switch $iRetPathType
            Case 1 ; relative path
                $sRetPath = StringTrimLeft($sCurrPath, $sOrigPathLen)
            Case 2 ; full path
                $sRetPath = $sCurrPath
        EndSwitch
        While 1
            $File = FileFindNextFile($Search)
            If @error Then ExitLoop
            If ($iRetItemType + @extended = 2) Then ContinueLoop
            $sRet &= $sRetPath & $File & "|"
        WEnd
        FileClose($Search)
        If $bRecursive Then
            $hSearch = FileFindFirstFile($sCurrPath & "*")
            While 1
                $File = FileFindNextFile($hSearch)
                If @error Then ExitLoop
                If @extended Then $sPath &= $sCurrPath & $File & "\|"
            WEnd
            FileClose($hSearch)
        EndIf
        $sPath = StringTrimLeft($sPath, $sCurrPathLen + 1)
    WEnd
    If Not $sRet Then Return SetError(4, 4, "")
    Return StringSplit(StringTrimRight($sRet, 1), "|")
EndFunc   ;==>_FileListToArray_Recursive
Edited by rogue5099

Share this post


Link to post
Share on other sites

You didn't need this!

From the website of PhotoResize400:

Changing the output folder

By default, Picture Resizer creates the images in the folder, where the original images were found. Version 1.2 can create the resized images in another folder. If the letter C is specified in application name (PhotoResize400C.exe), images are created in current folder. Current folder is controlled by the parent process. If you are using PhotoResize from command line, current folder is displayed in the command prompt. If you are using drag and drop in Windows Explorer, the current folder is unpredictable - a shortcut must be used to specify it.

But it's good practice for your AutoIT skills!

William

Share this post


Link to post
Share on other sites

You didn't need this!

Ya, I completly didn't read all the functions of that program. Not only is what I wrote unneeded but also I found out just by renaming the program I can do SOOO much more thanks for the eye opener!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Here is one from Melba23...

Not to be a whiner, but I am a stickler for details.

That _FileListToArrayRecursive() routine happens to be a byte-for-byte exact match of this post:

That function resulted from the efforts of a LOT of people who worked on it back in July 2009.

Mature versions in that thread were eventually tagged with:

Author(s):        Half the AutoIt Community

(Edit: I knew those comments at the top also looked familiar, but was surprised not to find them in the July 2009 post. I'd reposted that function 15 months later and added the comments to give the OP in that later thread some sort of documentation: )

Edited by Spiff59

Share this post


Link to post
Share on other sites

Spiff59,

Just to make it absolutely clear - I did not claim that code as my own, it was mistakenly atributed to me by Varian. ;)

My own version of a recursive file search UDF (RecFileListToArray) is in my sig. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

As a matter of fact I think that function got so messed up in the end that nobody wanted credit. I still don't use it and never will.

I should add that I believe I've read at least 10 or 12 versions of recursive file searches as well as about 4 more I've written myself. I don't remember who it was that wrote all those functions nor do I care. If they fit the bill use them, give credit when possible but if you can't do that then don't worry about it.

I think we have all had code "stolen" on these forums but few of us really give a damn. If you don't want it used then don't post it.

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

#10 ·  Posted (edited)

Knowing your dislike (or disdain) of #includes, UDF's in general, and your preference of using your own code, I have no problem with your post, with one exception. I was a little surprised by the very first, derisive sentence:

As a matter of fact I think that function got so messed up in the end that nobody wanted credit.

There is nothing "messed up" about that function, after 2 years it still may be the most concise, efficient example of a recursive FLTA. When posts started turning up in the 2009 thread with a list of 9 or 10 authors/contributors, some of us dispensed with keeping track and started plugging in a generic authorship. Edited by Spiff59

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