Jump to content

Selected Folders to Array. - (Moved)


Recommended Posts

I am trying to clean up files and folders in Windows 10.  This script works great when  1 folder is selected. However, I usually have at least 4 subfolders that I want the script to run through. Is there a way to have this run for each folder selected? TIA  Trip

; Purpose: Moves all files in subfolders from selected folder

Opt('SendKeyDelay', 0)
Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info


#Include <String.au3>
 #include <Array.au3>
#include <WinAPIFiles.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>

dim $sSource, $sDestination, $Files, $i

HotKeySet("{ESC}", "Quit")



;~ Gets file path of Explorer window; must select folder!

While 1
    ; Windows Explorer  10
    $hExplorer = WinGetHandle( "[REGEXPCLASS:^(Cabinet|Explore)WClass$]" )
    If Not $hExplorer Then Exit

    ; Shell object"M:\Cricut\Cards\Digital Craft Emporium\dce-26\3. Digi Papers"
    $oShell = ObjCreate( "Shell.Application" )


    ; Find window
    For $oWindow In $oShell.Windows()
      If $oWindow.HWND() = $hExplorer Then ExitLoop
    Next

    ; Selected items
    For $oItem In $oWindow.Document.SelectedItems()
          ConsoleWrite( $oItem.Path() & @CRLF )
     
     if $oItem.Path() ="" then 
         $iMsgAnswer=MsgBox (0,"Empty", "Please select a file or folder")

         EndIf
     

     $sSource=$oItem.Path() 

        $iPos = StringInStr($sSource, "\", 0, -1)
            ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iPos = ' & $iPos & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

;~      $sSource=stringleft($sSource,$ipos-1)
;~          ConsoleWrite($sSource & @CRLF )
     
         
     $sMid=$oItem.Path() 
       ConsoleWrite($sSource & @CRLF )

      Local $Files =_FileListToArrayRec ( $sSource, "*", $FLTAR_FOLDERS ,  $FLTAR_RECUR,  $FLTAR_NOSORT, $FLTAR_FULLPATH )
    ;~        _ArrayDisplay($Files, "Sorted tree")
    ;~      ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Files = ' & $Files & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

        $iPos = StringInStr($sSource, "\", 0, -1)
    ;~      ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iPos = ' & $iPos & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

        $sDestination=stringleft($sSource,$ipos-1)
    ;~         ConsoleWrite($sDestination & @CRLF )

    Next
    ExitLoop
WEnd


Local $FileList = _FileListToArray($sSource)
        If @error = 1 Then
        MsgBox(0, "", "No Folders Found.")
        Exit
        EndIf
        If @error = 4 Then
        MsgBox(0, "", "No Files Found.")
        Exit
        EndIf
;~ _ArrayDisplay($FileList, "$FileList")

        ConsoleWrite("Files: " & $FileList[0] & @LF)



For $i = 0 To UBound($Files) - 1

Global $iSuccess = _XCopy_DirMove($Files[$i], $sSource)
        ConsoleWrite("Folders: " & $Files[$i] & @LF)


next 

Func _XCopy_DirMove($sSourcePath, $sTargetPath)
    Local $iErrorlevel = RunWait('xcopy "' & $sSourcePath & '" "' & $sTargetPath & '" /E /Y /R /H /K /I', "", @SW_HIDE)
    If $iErrorlevel = 0 Then ; Copying without error
        DirRemove($sSourcePath, 1)
    EndIf
    Return SetError($iErrorlevel, 0, $iErrorlevel == 0)
EndFunc   ;==>_XCopy_DirMove

 

Link to post
Share on other sites
Posted (edited)

Not following your script completely.  But here another way :

#include <Array.au3>

Local $hExplorer = WinGetHandle("[REGEXPCLASS:^(Cabinet|Explore)WClass$]")
If Not $hExplorer Then Exit

Local $oShell = ObjCreate("Shell.Application")
For $oWindow In $oShell.Windows()
  If $oWindow.HWND = $hExplorer Then ExitLoop
Next

With $oWindow.Document.SelectedItems()
  If Not .count Then Exit MsgBox(0, "Empty", "Please select a file or folder")
  ConsoleWrite("Items selected " & .count & @CRLF)
  Local $aFile[.count]
  For $i = 0 To .count - 1
    $aFile[$i] = .item($i).Path
  Next
EndWith
_ArrayDisplay($aFile)

For $i = 0 to UBound($aFile) - 1
  ; do the delete here
Next

 

Edited by Nine
Remove parenthesis on properties
Link to post
Share on other sites
  • Developers

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...