Jump to content

rules to copy two files from one folder to many folders


Recommended Posts

I have a tree
Y:\folder1\folder2\exp<year>\compo\file1.doc
Y:\folder1\folder2\exp<year>\compo\file2.doc

I need to copy two files from folder
Y:\folder1\folder2\564.21\compo\file1.doc
Y:\folder1\folder2\564.21\compo\file2.doc

to many folders under the same tree
Y:\folder1\folder2\exp<year>

An utility to do this or similar tasks ? I need to establish conditions or rules like

 

copy if the target folder is not empty

or if the target folder is bigger than xx kb, etc

Best regards

 

Traducción AutoIt al Español. http://autoitespa.espanaforo.com/forum.htm . Visita el foro de AutoIt en español http://www.emesn.com/autoitforum/. I am a simple user, not a programmer.

Link to post
Share on other sites

You could use something like _FileListToArrayRec to capture the folders into an array and then use DirGetSize to determine folder conditions, example:

#include <Array.au3>
#include <File.au3>
Global $g_sDir = @ScriptDir
Global $g_iMaxSize = 1024 ;~ 1024kb
Global $g_sFile1 = @ScriptDir & "\File1.doc"
Global $g_sFile2 = @ScriptDir & "\File2.doc"

Global $g_aFolders = _FileListToArrayRec($g_sDir, "*", 2, 0, 0, 2)
    If @error Then Exit MsgBox(4096, "Error", "No folders found")
For $i = 1 To $g_aFolders[0]
    $aDirSize = DirGetSize($g_aFolders[$i], 1)
    ;~ Check if folder size is greater than 1024kb
    If Round($aDirSize[0]/1024, 2) >= $g_iMaxSize Then
        ConsoleWrite("Folder : " & $g_aFolders[$i] & @CRLF & "Size : " & Round($aDirSize[0]/1024, 2) & "kb" & @CRLF & "File Count : " & $aDirSize[1] & @CRLF & "Dir Count : " & $aDirSize[2] & @CRLF)
        ;~ FileCopy stuff here
    EndIf
Next

 

Edited by Subz
Link to post
Share on other sites
3 hours ago, junkew said:

Powershell?

* on 1 machine or more?

 

only 1 machine.

Is easy to manage powershell ?

 

Best Regards

 

Traducción AutoIt al Español. http://autoitespa.espanaforo.com/forum.htm . Visita el foro de AutoIt en español http://www.emesn.com/autoitforum/. I am a simple user, not a programmer.

Link to post
Share on other sites
5 hours ago, Subz said:

You could use something like _FileListToArrayRec to capture the folders into an array and then use DirGetSize to determine folder conditions, example:

#include <Array.au3>
#include <File.au3>
Global $g_sDir = @ScriptDir
Global $g_iMaxSize = 1024 ;~ 1024kb
Global $g_sFile1 = @ScriptDir & "\File1.doc"
Global $g_sFile2 = @ScriptDir & "\File2.doc"

Global $g_aFolders = _FileListToArrayRec($g_sDir, "*", 2, 0, 0, 2)
    If @error Then Exit MsgBox(4096, "Error", "No folders found")
For $i = 1 To $g_aFolders[0]
    $aDirSize = DirGetSize($g_aFolders[$i], 1)
    ;~ Check if folder size is greater than 1024kb
    If Round($aDirSize[0]/1024, 2) >= $g_iMaxSize Then
        ConsoleWrite("Folder : " & $g_aFolders[$i] & @CRLF & "Size : " & Round($aDirSize[0]/1024, 2) & "kb" & @CRLF & "File Count : " & $aDirSize[1] & @CRLF & "Dir Count : " & $aDirSize[2] & @CRLF)
        ;~ FileCopy stuff here
    EndIf
Next

 

I am not a programmer, so I would like to use a good script for this. configurable and versatile. Exist something done about this with a mild learning curve ?

 

Traducción AutoIt al Español. http://autoitespa.espanaforo.com/forum.htm . Visita el foro de AutoIt en español http://www.emesn.com/autoitforum/. I am a simple user, not a programmer.

Link to post
Share on other sites
  1. Backup your data as you start a learning curve
  2. Windows start button
  3. type ISE <enter>           you are now in the simple IDE for powershell
  4. Copy paste below

clear-host 
for ($year=2021; $year -le 2030; $year++) {
    $targetFolder="Y:\folder1\folder2\exp$year"
    write-host copying to $targetFolder 
    if (Test-Path -Path $targetFolder) {
        if (@( Get-ChildItem $targetfolder).Count -ge 1) {
            write-host "There are files in $targetfolder"
        } else {
            $folderSize =  ((Get-ChildItem . | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum)
            if ($foldersize -gt 100) {
                write-host "$targetfolder already has size $foldersize"
            } else
            {
                Copy-Item Y:\folder1\folder2\564.21\compo\file1.doc -destination $targetFolder
                Copy-Item Y:\folder1\folder2\564.21\compo\file2.doc -destination $targetFolder
            }
        }
    } else {
        "Path $targetFolder doesn't exist."
    }
}

Powershell has the benefit in general its on every machine around.

Sometimes its easier then AutoIt sometimes its not. Both are very powerfull

If you are not good in scripting you could create a bat file with excel and notepad but then no conditions

  1. Cell A1 you put Y:\folder1\folder2\exp<year>\compo\file1.doc
  2. Cell B1 you put Y:\folder1\folder2\exp<year>\compo\file2.doc
  3. Cell E1 type a formula="Copy " & B1 &" " & C1
  4. Cell C1 you put "Y:\folder1\folder2\exp2020"
  5. Cell D1 type a formula="Copy " & A1 &" " & C1
  6. You copy the line downwards for each year you need
  7. Copy column D data to notepad file copyit.bat
  8. Copy column E below the other data
  9. save it
  10. doubleclick the copyit.bat

 

 

 

 

 

Link to post
Share on other sites
  • 2 months later...
On 9/28/2021 at 11:23 AM, AutoPepe said:

I have a tree
Y:\folder1\folder2\exp<year>\compo\file1.doc
Y:\folder1\folder2\exp<year>\compo\file2.doc

I need to copy two files from folder
Y:\folder1\folder2\564.21\compo\file1.doc
Y:\folder1\folder2\564.21\compo\file2.doc

to many folders under the same tree
Y:\folder1\folder2\exp<year>

An utility to do this or similar tasks ? I need to establish conditions or rules like

 

copy if the target folder is not empty

or if the target folder is bigger than xx kb, etc

Best regards

 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
$folderSource = @ScriptDir & "\SevenFolder\"
$folderDestin = @ScriptDir & "\Folders\"
$year = "2000"
$month = "11"
$day = "01"
 Local  $aFileList = _FileListToArray($folderSource,Default, Default, True)
 Local  $aFileList2 = _FileListToArray($folderSource,"*")
 for $i = 0 to _ArrayMaxIndex($aFileList,0,1) Step 1
    FileSetTime($aFileList[$i],$year&$month&$day,$FT_MODIFIED)
    DirMove($folderSource&$aFileList2[$i], $folderDestin & $aFileList2[$i])
    Sleep(200)
    Next

maybe this will help you.

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