Jump to content

code to create file folders according to their extension and move them to the corresponding folders


Go to solution Solved by Andreik,

Recommended Posts

Hello dears,

Along with saying hello, I will tell you about a code to create file folders according to their extension and move them to the corresponding folders.

I leave 2 screenshots with what I need

 

Best regards

SNAP 01.jpg

SNAP 02.jpg

Link to comment
Share on other sites

copy-move-folders-or-files-according-to-its-extension-to-the-favorite-folders


Edit:
 

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <File.au3>
#include <Array.au3>

_Example()

;--------------------------------------------------------------------------------------------------------------------------------
Func _Example()
    Local $aFiles = _FindAll(@UserProfileDir & "\Pictures", "*.jpg;*.png;*.bmp;*.tiff")
;~  _ArrayDisplay($aFiles)
    Local $iFileExists, $sNewFilePath, $sDestination ; , $sDrive, $sDir, $sFileName, $sExtension

    For $i = 1 To $aFiles[0]
        Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "", $iCnt = 0
        _PathSplit($aFiles[$i], $sDrive, $sDir, $sFileName, $sExtension)
        Switch $sExtension
            Case ".jpg"
                $sDestination = "D:\Pictures\jpg\"
            Case ".png"
                $sDestination = "D:\Pictures\png\"
            Case ".bmp"
                $sDestination = "D:\Pictures\bmp\"
            Case ".tiff"
                $sDestination = "D:\Pictures\tif\"
            Case Else
                ConsoleWrite("?? " & $aFiles[$i] & @CRLF)
                ContinueCase
        EndSwitch

        Do ; Increase the value of $iCnt until File not Exists
            If $iCnt = 0 Then
                $sNewFilePath = $sDestination & $sFileName & $sExtension
            Else
                $sNewFilePath = $sDestination & $sFileName & "~" & $iCnt & $sExtension
            EndIf
            $iFileExists = FileExists($sNewFilePath)
            $iCnt += 1
        Until $iFileExists = 0

        FileMove($aFiles[$i], $sNewFilePath, $FC_OVERWRITE + $FC_CREATEPATH)
    Next

EndFunc   ;==>_Example
;--------------------------------------------------------------------------------------------------------------------------------
Func _FindAll($dir, $sMask = "*", $iReturn = $FLTAR_FILES, $iRecur = $FLTAR_RECUR)
    Local $aList = _FileListToArrayRec($dir, $sMask, $iReturn, $iRecur, $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If Not IsArray($aList) Then
        Local $aEmpty[] =[0]
        ConsoleWrite($dir & " There are not '" & $sMask & "' in " & $dir & @CRLF)
        Return SetError(1, 0, $aEmpty)
    Else
        Return $aList
    EndIf
EndFunc   ;==>_FindAll

 

Edited by ioa747
UpDate

I know that I know nothing

Link to comment
Share on other sites

This is another way:

#include <File.au3>

CreateExtDirs(@ScriptDir)

Func CreateExtDirs($sPath, $fRec = False)
    Local $aFile = _FileListToArrayRec(@ScriptDir, Default, $FLTAR_FILES, ($fRec ? $FLTAR_RECUR : $FLTAR_NORECUR), $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If @error Then Return SetError(1, 0, 0)
    Local $sDrive, $sDir, $sFileName, $sExt, $sExtDir, $vIdx = ''
    For $Index = 1 To $aFile[0]
        $vIdx = ''
        _PathSplit($aFile[$Index], $sDrive, $sDir, $sFileName, $sExt)
        $sExtDir = @ScriptDir & '\' & StringReplace($sExt, '.', '')
        If Not FileExists($sExtDir) Then DirCreate($sExtDir)
        While FileExists($sExtDir & '\' & $sFileName & $vIdx & $sExt)
            $vIdx += 1
        WEnd
        FileMove($aFile[$Index], $sExtDir & '\' & $sFileName & $vIdx & $sExt)
    Next
EndFunc

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

16 hours ago, ioa747 said:

copy-move-folders-or-files-according-to-its-extension-to-the-favorite-folders


Edit:
 

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <File.au3>
#include <Array.au3>

_Example()

;--------------------------------------------------------------------------------------------------------------------------------
Func _Example()
    Local $aFiles = _FindAll(@UserProfileDir & "\Pictures", "*.jpg;*.png;*.bmp;*.tiff")
;~  _ArrayDisplay($aFiles)
    Local $iFileExists, $sNewFilePath, $sDestination ; , $sDrive, $sDir, $sFileName, $sExtension

    For $i = 1 To $aFiles[0]
        Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "", $iCnt = 0
        _PathSplit($aFiles[$i], $sDrive, $sDir, $sFileName, $sExtension)
        Switch $sExtension
            Case ".jpg"
                $sDestination = "D:\Pictures\jpg\"
            Case ".png"
                $sDestination = "D:\Pictures\png\"
            Case ".bmp"
                $sDestination = "D:\Pictures\bmp\"
            Case ".tiff"
                $sDestination = "D:\Pictures\tif\"
            Case Else
                ConsoleWrite("?? " & $aFiles[$i] & @CRLF)
                ContinueCase
        EndSwitch

        Do ; Increase the value of $iCnt until File not Exists
            If $iCnt = 0 Then
                $sNewFilePath = $sDestination & $sFileName & $sExtension
            Else
                $sNewFilePath = $sDestination & $sFileName & "~" & $iCnt & $sExtension
            EndIf
            $iFileExists = FileExists($sNewFilePath)
            $iCnt += 1
        Until $iFileExists = 0

        FileMove($aFiles[$i], $sNewFilePath, $FC_OVERWRITE + $FC_CREATEPATH)
    Next

EndFunc   ;==>_Example
;--------------------------------------------------------------------------------------------------------------------------------
Func _FindAll($dir, $sMask = "*", $iReturn = $FLTAR_FILES, $iRecur = $FLTAR_RECUR)
    Local $aList = _FileListToArrayRec($dir, $sMask, $iReturn, $iRecur, $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If Not IsArray($aList) Then
        Local $aEmpty[] =[0]
        ConsoleWrite($dir & " There are not '" & $sMask & "' in " & $dir & @CRLF)
        Return SetError(1, 0, $aEmpty)
    Else
        Return $aList
    EndIf
EndFunc   ;==>_FindAll

 

119 / 5.000
 

 

Thanks for your response, this code changes the names of the files that have already been moved again, this is wrong

Link to comment
Share on other sites

8 hours ago, Andreik said:

This is another way:

#include <File.au3>

CreateExtDirs(@ScriptDir)

Func CreateExtDirs($sPath, $fRec = False)
    Local $aFile = _FileListToArrayRec(@ScriptDir, Default, $FLTAR_FILES, ($fRec ? $FLTAR_RECUR : $FLTAR_NORECUR), $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If @error Then Return SetError(1, 0, 0)
    Local $sDrive, $sDir, $sFileName, $sExt, $sExtDir, $vIdx = ''
    For $Index = 1 To $aFile[0]
        $vIdx = ''
        _PathSplit($aFile[$Index], $sDrive, $sDir, $sFileName, $sExt)
        $sExtDir = @ScriptDir & '\' & StringReplace($sExt, '.', '')
        If Not FileExists($sExtDir) Then DirCreate($sExtDir)
        While FileExists($sExtDir & '\' & $sFileName & $vIdx & $sExt)
            $vIdx += 1
        WEnd
        FileMove($aFile[$Index], $sExtDir & '\' & $sFileName & $vIdx & $sExt)
    Next
EndFunc

 

This code works fine, it is possible to modify the code so that it does not move the *.exe and *.au3 files

Link to comment
Share on other sites

  • Solution
6 minutes ago, Netol said:

This code works fine, it is possible to modify the code so that it does not move the *.exe and *.au3 files

It should be easy to check and exclude some extensions:

#include <File.au3>

CreateExtDirs(@ScriptDir)

Func CreateExtDirs($sPath, $fRec = False)
    Local $aFile = _FileListToArrayRec(@ScriptDir, Default, $FLTAR_FILES, ($fRec ? $FLTAR_RECUR : $FLTAR_NORECUR), $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If @error Then Return SetError(1, 0, 0)
    Local $sDrive, $sDir, $sFileName, $sExt, $sExtDir, $vIdx = ''
    For $Index = 1 To $aFile[0]
        $vIdx = ''
        _PathSplit($aFile[$Index], $sDrive, $sDir, $sFileName, $sExt)
        If StringLower($sExt) = '.au3' Or StringLower($sExt) = '.exe' Then ContinueLoop
        $sExtDir = @ScriptDir & '\' & StringReplace($sExt, '.', '')
        If Not FileExists($sExtDir) Then DirCreate($sExtDir)
        While FileExists($sExtDir & '\' & $sFileName & $vIdx & $sExt)
            $vIdx += 1
        WEnd
        FileMove($aFile[$Index], $sExtDir & '\' & $sFileName & $vIdx & $sExt)
    Next
EndFunc

Note that you can also pass wanted extensions to _FileListToArrayRec().

When the words fail... music speaks.

Link to comment
Share on other sites

16 minutes ago, Andreik said:

It should be easy to check and exclude some extensions:

#include <File.au3>

CreateExtDirs(@ScriptDir)

Func CreateExtDirs($sPath, $fRec = False)
    Local $aFile = _FileListToArrayRec(@ScriptDir, Default, $FLTAR_FILES, ($fRec ? $FLTAR_RECUR : $FLTAR_NORECUR), $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If @error Then Return SetError(1, 0, 0)
    Local $sDrive, $sDir, $sFileName, $sExt, $sExtDir, $vIdx = ''
    For $Index = 1 To $aFile[0]
        $vIdx = ''
        _PathSplit($aFile[$Index], $sDrive, $sDir, $sFileName, $sExt)
        If StringLower($sExt) = '.au3' Or StringLower($sExt) = '.exe' Then ContinueLoop
        $sExtDir = @ScriptDir & '\' & StringReplace($sExt, '.', '')
        If Not FileExists($sExtDir) Then DirCreate($sExtDir)
        While FileExists($sExtDir & '\' & $sFileName & $vIdx & $sExt)
            $vIdx += 1
        WEnd
        FileMove($aFile[$Index], $sExtDir & '\' & $sFileName & $vIdx & $sExt)
    Next
EndFunc

Note that you can also pass wanted extensions to _FileListToArrayRec().

Excellent, this work fine.

Thanks a lot!

Link to comment
Share on other sites

Posted (edited)
7 hours ago, Andreik said:

It should be easy to check and exclude some extensions:

#include <File.au3>

CreateExtDirs(@ScriptDir)

Func CreateExtDirs($sPath, $fRec = False)
    Local $aFile = _FileListToArrayRec(@ScriptDir, Default, $FLTAR_FILES, ($fRec ? $FLTAR_RECUR : $FLTAR_NORECUR), $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If @error Then Return SetError(1, 0, 0)
    Local $sDrive, $sDir, $sFileName, $sExt, $sExtDir, $vIdx = ''
    For $Index = 1 To $aFile[0]
        $vIdx = ''
        _PathSplit($aFile[$Index], $sDrive, $sDir, $sFileName, $sExt)
        If StringLower($sExt) = '.au3' Or StringLower($sExt) = '.exe' Then ContinueLoop
        $sExtDir = @ScriptDir & '\' & StringReplace($sExt, '.', '')
        If Not FileExists($sExtDir) Then DirCreate($sExtDir)
        While FileExists($sExtDir & '\' & $sFileName & $vIdx & $sExt)
            $vIdx += 1
        WEnd
        FileMove($aFile[$Index], $sExtDir & '\' & $sFileName & $vIdx & $sExt)
    Next
EndFunc

Note that you can also pass wanted extensions to _FileListToArrayRec().

Just a question,
the code does not work with variables

example

$Path_all = GUICtrlRead($Combo1)

CreateExtDirs($Path_all)

Can you modify the code to do that?

Edited by Netol
Link to comment
Share on other sites

9 hours ago, Netol said:

this code changes the names of the files that have already been moved again

the conditions for this to happen are

you overlooked the parameter  $iRecur  in function  _FindAll($dir, $sMask = "*", $iReturn = $FLTAR_FILES, $iRecur = $FLTAR_RECUR)

and the folders jpg, png, bmp, tiff  is sub-folders in the search folder

with the result that in every search it also searches the subfolders

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

4 hours ago, Netol said:

Just a question,
the code does not work with variables

example

$Path_all = GUICtrlRead($Combo1)

CreateExtDirs($Path_all)

Can you modify the code to do that?

You know that this is non sense. Use ConsoleWrite() to see what $Path_all contains.

When the words fail... music speaks.

Link to comment
Share on other sites

i did it but doesnt work

 

 

    case $ButMoveAll

    $Path_all = GUICtrlRead($Combo1)

    ConsoleWrite($Path_all)

    CreateExtDirs($Path_all)

 

 

the variable put the correct path that i put in a combo

Link to comment
Share on other sites

20 hours ago, Andreik said:

You know that this is non sense. Use ConsoleWrite() to see what $Path_all contains.

Dear, with the idea of creating a very good code, it would be an excellent idea for you to add a rollback to the code, with the idea that when the files are moved to each folder, a .bat file is created that when clicked leaves it again. the files as they were at the beginning, can you add this code

Link to comment
Share on other sites

You missed the main idea of the forum where you come to get help when you are stuck after you did some work. Simply asking for code it won't work well for you on a long term. Yet I will give you a hint to solve your issue: it doesn't have to be a bat file, you can simply save a report with source and destination for each moved file so you can restore them to initial state.

When the words fail... music speaks.

Link to comment
Share on other sites

10 hours ago, Andreik said:

You missed the main idea of the forum where you come to get help when you are stuck after you did some work. Simply asking for code it won't work well for you on a long term. Yet I will give you a hint to solve your issue: it doesn't have to be a bat file, you can simply save a report with source and destination for each moved file so you can restore them to initial state.

Yes dear, I understand, I will try to make the code, thank you very much for your help.
sincerely

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