Jump to content

copy file afterunzip from folder to script folder


Recommended Posts

hi guys i try to run this script  , but i not understund why not  copy my file  inside a folder ,after unzip in root source script anyone can help me ? thanks

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>

; Cartella di destinazione
Global $destinationFolder = @ScriptDir

; Percorso della cartella in cui cercare i file ZIP
Global $sourceFolder = "MYFOLDER"

; Trova tutti i file ZIP nella cartella di origine
Global $zipFiles = _FileListToArray($sourceFolder, "*.zip", $FLTA_FILES, True)

; Controlla se sono stati trovati file ZIP
If Not @error Then
    ; Loop attraverso ogni file ZIP
    For $i = 1 To $zipFiles[0]
        ; Esegui l'estrazione senza la creazione di sottodirectory
        _UnZip($zipFiles[$i], $destinationFolder)
        _CopyFilesFromSubfolders($destinationFolder)
    Next
Else
    MsgBox($MB_ICONERROR, "Errore", "Nessun file ZIP trovato nella cartella specificata.")
EndIf

Func _UnZip($sZipFile, $sDestFolder)
    If Not FileExists($sZipFile) Then Return SetError(1) ; Il file di origine non esiste
    If Not FileExists($sDestFolder) Then
        If Not DirCreate($sDestFolder) Then Return SetError(2) ; Impossibile creare la cartella di destinazione
    ElseIf Not StringInStr(FileGetAttrib($sDestFolder), "D") Then
        Return SetError(3) ; La destinazione non è una cartella
    EndIf

    Local $oShell = ObjCreate("shell.application")
    Local $oZip = $oShell.NameSpace($sZipFile)
    Local $iZipFileCount = $oZip.Items.Count

    If Not $iZipFileCount Then Return SetError(4) ; Il file ZIP è vuoto

    For $oFile In $oZip.Items
        $oShell.NameSpace($sDestFolder).CopyHere($oFile)
    Next

    ; Attendiamo un po' prima di terminare per dare il tempo di copiare i file
    Sleep(500)

    ; Recuperiamo la lista dei file copiati nella sotto-cartella
    Local $aCopiedFiles = _FileListToArray($sDestFolder, "*", $FLTA_FILES, True)

    ; Copiamo i file dalla sotto-cartella alla root dello script
    For $i = 1 To $aCopiedFiles[0]
        Local $sSourceFile = $sDestFolder & "\" & $aCopiedFiles[$i]
        Local $sDestinationFile = @ScriptDir & "\" & $aCopiedFiles[$i]
        FileCopy($sSourceFile, $sDestinationFile, $FC_OVERWRITE)
    Next

    Return 0 ; Successo
EndFunc   ;==>UnZip


Func _CopyFilesFromSubfolders($sFolder)
    Local $aCopiedFiles = _FileListToArray($sFolder, "*", $FLTA_FILES, True)
    For $i = 1 To $aCopiedFiles[0]
        Local $sSourceFile = $sFolder & "\" & $aCopiedFiles[$i]
        Local $sDestinationFile = @ScriptDir & "\" & $aCopiedFiles[$i]
        FileCopy($sSourceFile, $sDestinationFile, $FC_OVERWRITE)
    Next
EndFunc

 

Link to comment
Share on other sites

:)  Looks like code I have written before...

At first glance, you ask _FileListToArray to return full path (True as last parameter), then you concatenate the folder ($sFolder & "\" & $aCopiedFiles[$i].  It cannot find the file of course.

Link to comment
Share on other sites

You're right @Nine.

Like you can (hopefully) recognize in the screenshot, the concatenate file path strings are the problem.

image.thumb.png.88b168d6f221137883236e2d61201502.png

Don't worry, the other marked lines are changes from my side to reproduce your question.
Please just give attention to the red rectangle.

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

45 minutes ago, Nine said:

:)  Looks like code I have written before...

yes of  course you inspire me

:D

i try to mod the code  but  not  copy again mmmm 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

; Cartella di destinazione
Global $destinationFolder = @ScriptDir

; Percorso della cartella in cui cercare i file ZIP
Global $sourceFolder = "C:\Users\e-Office\Documents\ditta\2024\XPROGRAMMA\FATTURE INVIATE"

; Trova tutti i file ZIP nella cartella di origine
Global $zipFiles = _FileListToArray($sourceFolder, "*.zip", $FLTA_FILES, True)

; Controlla se sono stati trovati file ZIP
If Not @error Then
    ; Loop attraverso ogni file ZIP
    For $i = 1 To $zipFiles[0]
        ; Esegui l'estrazione senza la creazione di sottodirectory
        _UnZip($zipFiles[$i], $destinationFolder)
        _CopyFilesFromSubfolders($destinationFolder)
    Next
Else
    MsgBox($MB_ICONERROR, "Errore", "Nessun file ZIP trovato nella cartella specificata.")
EndIf

Func _UnZip($sZipFile, $sDestFolder)
    If Not FileExists($sZipFile) Then Return SetError(1) ; Il file di origine non esiste
    If Not FileExists($sDestFolder) Then
        If Not DirCreate($sDestFolder) Then Return SetError(2) ; Impossibile creare la cartella di destinazione
    ElseIf Not StringInStr(FileGetAttrib($sDestFolder), "D") Then
        Return SetError(3) ; La destinazione non è una cartella
    EndIf

    Local $oShell = ObjCreate("shell.application")
    Local $oZip = $oShell.NameSpace($sZipFile)
    Local $iZipFileCount = $oZip.Items.Count

    If Not $iZipFileCount Then Return SetError(4) ; Il file ZIP è vuoto

    For $oFile In $oZip.Items
        $oShell.NameSpace($sDestFolder).CopyHere($oFile)
    Next

    ; Attendiamo un po' prima di terminare per dare il tempo di copiare i file
    Sleep(500)

    ; Recuperiamo la lista dei file copiati nella sotto-cartella
    Local $aCopiedFiles = _FileListToArray($sDestFolder, "*", $FLTA_FILES, True)

    ; Copiamo i file dalla sotto-cartella alla root dello script
    For $i = 1 To $aCopiedFiles[0]
        Local $sSourceFile = $sDestFolder & "\" & $aCopiedFiles[$i]
        Local $sDestinationFile = @ScriptDir & "\" & $aCopiedFiles[$i]
        FileCopy($sSourceFile, $sDestinationFile, $FC_OVERWRITE)
    Next

    Return 0 ; Successo
EndFunc   ;==>UnZip

Func _CopyFilesFromSubfolders($sFolder)
    Local $aCopiedFiles = _FileListToArray($sFolder, "*", $FLTA_FILES, True)
    For $i = 1 To _ArrayMax($aCopiedFiles)
        Local $sSourceFile = $sFolder & "\" & $aCopiedFiles[$i]
        Local $sDestinationFile = @ScriptDir & "\" & $aCopiedFiles[$i]
        FileCopy($sSourceFile, $sDestinationFile, $FC_OVERWRITE)
    Next

    ; Recuperiamo la lista delle eventuali sottocartelle
    Local $aSubfolders = _FileListToArray($sFolder, "*", $FLTA_FOLDERS, True)
    ; Per ogni sottocartella, richiamiamo ricorsivamente la funzione
    For $i = 1 To _ArrayMax($aSubfolders)
        _CopyFilesFromSubfolders($sFolder & "\" & $aSubfolders[$i])
    Next
EndFunc

 

Link to comment
Share on other sites

You still have the same problem.  You use True in _FileListToArray as last parameter, it should be False if you insist concatenating Folder & File.

ps. comments in other language (italian ?) than english does not help us one bit...

pps. use ConsoleWrite to follow the path (and variables) of your script, you will understand rapidly what are the issues.  If not post full script with ConsoleWrite along with Scite console.

Link to comment
Share on other sites

thanks  for suggest

now  work

this a right  code  thanks  again

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

; Destination folder
Global $destinationFolder = @ScriptDir

; Path to the folder where ZIP files are located
Global $sourceFolder = "C:\Users\e-Office\Documents\ditta\2024\XPROGRAMMA\FATTURE INVIATE"

; Find all ZIP files in the source folder
Global $zipFiles = _FileListToArray($sourceFolder, "*.zip", $FLTA_FILES, True)

; Check if ZIP files were found
If Not @error Then
    ; Loop through each ZIP file
    For $i = 1 To $zipFiles[0]
        ; Extract without creating subdirectories
        _UnZip($zipFiles[$i], $destinationFolder)
        ; Copy files from subfolders to the root
        _CopyFilesFromSubfolders($destinationFolder)
    Next
Else
    MsgBox($MB_ICONERROR, "Error", "No ZIP files found in the specified folder.")
EndIf

Func _UnZip($sZipFile, $sDestFolder)
    If Not FileExists($sZipFile) Then Return SetError(1) ; Source file does not exist
    If Not FileExists($sDestFolder) Then
        If Not DirCreate($sDestFolder) Then Return SetError(2) ; Unable to create destination folder
    ElseIf Not StringInStr(FileGetAttrib($sDestFolder), "D") Then
        Return SetError(3) ; Destination is not a folder
    EndIf

    Local $oShell = ObjCreate("shell.application")
    Local $oZip = $oShell.NameSpace($sZipFile)
    Local $iZipFileCount = $oZip.Items.Count

    If Not $iZipFileCount Then Return SetError(4) ; ZIP file is empty

    For $oFile In $oZip.Items
        $oShell.NameSpace($sDestFolder).CopyHere($oFile)
    Next

    ; Wait a bit before finishing to allow time for file copying
    Sleep(500)

    ; Retrieve the list of copied files in the subfolder
    Local $aCopiedFiles = _FileListToArray($sDestFolder, "*", $FLTA_FILES, False)

    ; Copy files from the subfolder to the root of the script
    For $i = 0 To UBound($aCopiedFiles) - 1
        Local $sSourceFile = $sDestFolder & "\" & $aCopiedFiles[$i]
        Local $sDestinationFile = @ScriptDir & "\" & $aCopiedFiles[$i]
        ConsoleWrite("Copying: " & $sSourceFile & " to " & $sDestinationFile & @CRLF)
        FileCopy($sSourceFile, $sDestinationFile, $FC_OVERWRITE)
    Next

    Return 0 ; Success
EndFunc   ;==>UnZip

Func _CopyFilesFromSubfolders($sFolder)
    ; Get the list of copied files in the subfolder
    Local $aCopiedFiles = _FileListToArray($sFolder, "*", $FLTA_FILES, False)
    ; Copy files from the subfolder to the root
    For $i = 0 To UBound($aCopiedFiles) - 1
        Local $sSourceFile = $sFolder & "\" & $aCopiedFiles[$i]
        FileCopy($sSourceFile, @ScriptDir & "\" & $aCopiedFiles[$i], $FC_OVERWRITE)
    Next

    ; Retrieve the list of any subfolders
    Local $aSubfolders = _FileListToArray($sFolder, "*", $FLTA_FOLDERS, False)
    ; For each subfolder, recursively call the function
    For $i = 0 To UBound($aSubfolders) - 1
        _CopyFilesFromSubfolders($sFolder & "\" & $aSubfolders[$i])
    Next
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...