Jump to content

"Extract' multiple dirs to one


Pakku
 Share

Recommended Posts

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
Link to comment
Share on other sites

Can't this throw a recursion error?

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

Why?

Edited by Pakku
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Updated script, check out the first post!

Edited by Pakku
Link to comment
Share on other sites

  • 2 weeks later...

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

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...