Sign in to follow this  
Followers 0
Pakku

"Extract' multiple dirs to one

7 posts in this topic

#1 ·  Posted (edited)

Hi all,

I had a dir with several dirs in it and in there several files. I wanted to 'extract' all of these files in just one dir. So I came up with the following:

Func _ExtractDir($dir,$dest)
    $search = FileFindFirstFile($dir & "*.*")  
    If $search <> -1 Then
        While 1
            $file = FileFindNextFile($search) 
            If @error Then
                ExitLoop
            EndIf
            If StringInStr(StringRight($file,4),".") = 1 Then
                $destfile = $dest & StringReplace($dir & $file,"\","-")
                FileCopy($dir & "\" & $file,$destfile)
            Else
                _ExtractDir($dir & $file & "\",$dest)
            EndIf
        WEnd
        FileClose($search)
    EndIf
EndFunc

To ensure that no filename is the same, the files will be copied to the destination with the pathe where it was in but the backslahses are replaced by a "-" sign. This also makes it easy to 'group' them and to keep the overview :lmao:

Note:

  • If you want to 'extract' the dir where it is in: also know as the @ScriptDir, the first parameter has to be blanck: "" :whistle:
  • All parameters have to end with a backslash: "\", but the above rule is overruling this one
  • I havn't test to extract TO the same dir as you want to 'extract', but I don't think it's a good idea.
  • As you may see, this script splits the files from the folders by checking if the dot (".") is on the 4th place counted from the end. So it 'thinks' that files always have a 3 char extension!
Feedback, bug reports and stuff is more than welcome ;) Edited by Pakku

Share this post


Link to post
Share on other sites



Can't this throw a recursion error?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Can't this throw a recursion error?

Not that I know of, I used it myself and it didn't.

Why?

Edited by Pakku

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I fix it a bit

Func _ExtractDir($dir,$dest )
    $search = FileFindFirstFile($dir & "*.*") 
    If $search <> -1 Then
        While 1
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
            If StringInStr($file,".") = 1 Then;Remember File ext can be more than or less than 3-
                $dest = $dest & StringReplace($dir & $file,"\","-")
                FileCopy($file,$dest)
            Else
                _ExtractDir($dir & $file & "\", $dest)
            EndIf
        WEnd
        FileClose($search)
    EndIf
EndFunc
Edited by athiwatc

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I fix it a bit

Func _ExtractDir($dir,$dest )
    $search = FileFindFirstFile($dir & "*.*") 
    If $search <> -1 Then
        While 1
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
            If StringInStr($file,".") = 1 Then;Remember File ext can be more than or less than 3-
                $dest = $dest & StringReplace($dir & $file,"\","-")
                FileCopy($file,$dest)
            Else
                _ExtractDir($dir & $file & "\", $dest)
            EndIf
        WEnd
        FileClose($search)
    EndIf
EndFuncoÝ÷ Ûú®¢×¶+)¢Èëhr·µæ¢W^¯­§h¶)â·}ÚºÚ"µÍYÝ[Ò[ÝÝ[ÔYÚ
    ÌÍÙ[K

JK  ][ÝË][ÝÊHH
HÜ

This might work, it your extension is 3 or 4 char long

Edited by Pakku

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Updated script, check out the first post!

Edited by Pakku

Share this post


Link to post
Share on other sites

When I define the source and destination and attempt to run your script, nothing happens. What am I doing wrong?

$dir="C:\salesreports\"

$dest="C:\archive\sales\"

Func _ExtractDir($dir,$dest)

$search = FileFindFirstFile($dir & "*.*")

If $search <> -1 Then

While 1

$file = FileFindNextFile($search)

If @error Then

ExitLoop

EndIf

If StringInStr(StringRight($file,4),".") = 1 Then

$destfile = $dest & StringReplace($dir & $file,"\","-")

FileCopy($dir & "\" & $file,$destfile)

Else

_ExtractDir($dir & $file & "\",$dest)

EndIf

WEnd

FileClose($search)

EndIf

EndFunc

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