Sign in to follow this  
Followers 0
Uronacid

Copy and Delete Directores Within Directories

8 posts in this topic

Alright,

I have used auto it to install applications, create quick fixes to repeated problems, but I'm stumped on this one. Here it goes:

We have a network drive, Drive A:\, that we need to copy specific folders and sub directories from and into Drive B:\. We also need to maintain the directory structure currently in drive A when we copy the files into drive B. When everything is said and done we need to delete the folders that we copied into drive B from drive A.

Example:

Drive A:\

-Folder A

-- Folder 1

-- Folder 2

-- Folder 3

-- Folder 4

-Folder B

-- Folder 1

-- Folder 3

-- Folder 4

-Folder C

-- Folder 3

-- Folder 4

COPY FOLDERS/FILES FROM DRIVE A

Drive B:\

-Folder A

-- Folder 1

-- Folder 2

-Folder B

-- Folder 1

DELETE FOLDERS AND FILES FROM DRIVE A

Drive A:\

-Folder A

-- Folder 3

-- Folder 4

-Folder B

-- Folder 3

-- Folder 4

-Folder C

-- Folder 3

-- Folder 4

I hope you understood all of that. Anyway, I was thinking that I would put the entire file and folder structure from drive A into a multi-tiered array and use that array to copy and delete folders from drive A. So, for example. I would push the entire directory structure from drive A into an array.

Example:

drv1array

[0][0]Folder A

[0][1]Folder 1

[0][2]Folder 2

[0][3]Folder 3

[0][4]Folder 4

[1][0]Folder B

[1][1]Folder 1

[1][2]Folder 3

[1][3]Folder 4

[2][0]Folder C

[2][1]Folder 3

[2][2]Folder 4

Then I would run a loop down the entire array extracting the folders and running copy commands as it ran down the array. Does anyone have a way to place the directory structure into an array like that, or perhaps an easier way to do this?

Share this post


Link to post
Share on other sites



maybe you'd like to have a look at

the filelisttoarray UDF


In the beginning there was nothing and then even that exploded - anonymous

Share this post


Link to post
Share on other sites

Thanks,

I'll try this. I'll have to see if it works.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Alright,

I used the thread you gave me to come up with this.

$drivesource = "T:\Test\Drive A";source path
$drivedest = "T:\Test\Drive B";source destination
$drivearray = _FileListToArray3($drivesource, "*", 2);put the folders within the drive path into an array
$drivelen =  $drivearray[0];find the number of folders in the drive path

Do;run through all the folders within the drive path
    $dirpath = $drivearray[$drivelen];get the poath to the first dir
    $dirarray = _FileListToArray3($dirpath, "*", 2);put the folders within that dir into an array
    $dirlen = $dirarray[0];find the number of folders withing the dir
    $startpos = stringlen($drivearray[$drivelen]) + 2;find the starting position for search

    Do; do loop for finding the years on the names of the folders
        $Year = StringMid($dirarray[$dirlen], $startpos, 4)
        If $Year < 2002 Then 
            $drivepathlen = StringLen($drivesource)
            $copysource = $dirarray[$dirlen]
            $copydest = $drivedest & StringRight($dirarray[$dirlen], StringLen($dirarray[$dirlen]) - StringLen($drivesource))
            DirCopy($copysource, $copydest)             
        EndIf
        $dirlen = $dirlen - 1
    Until $dirlen = 0

    $drivelen = $drivelen - 1
Until $drivelen = 0

It works flawlessly.

However,

After researching deeper into the issue, I have found that I'm going to need to find the date of the files and folders have been modified and extract them from the drive based upon that date. Does anyone have any tip son how to extract the date a file or folder has been modified.

Edited by Uronacid

Share this post


Link to post
Share on other sites

FileGetTime()


In the beginning there was nothing and then even that exploded - anonymous

Share this post


Link to post
Share on other sites

FileGetTime()

God, you are amazing.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Well, I finally finished my script. I figured I would post it for anyone who might need it. All it does is copy all directories from one location to another location while maintaining directory structure.

Keep in mind, I used the _FileListToArray3 script from this link.

Func _FindNumofDir($sPath)
    $aArray = _FileListToArray3($sPath, "*", 2)
    Return $aArray[1]
EndFunc

Func _FindLastDir($sPath2)
    $aSplit = StringSplit($sPath2, "\")
    $aLen = UBound($aSplit)-1
    Return StringLeft($sPath2, StringLen($sPath2) - StringLen($aSplit[$aLen]) - 1)
EndFunc

Func _FindStartPos($lastPath)
    $lastDir = _FileListToArray3(_FindLastDir($lastPath), "*", 2)
    $nStartPos = $lastDir[0]
    $blnTest = False
    Do
        If $lastDir[$nStartPos] = $lastPath Then
            $blnTest = True
        Else
            $nStartPos = $nStartPos - 1
        EndIf
    Until $blnTest = True
    Return $nStartPos
EndFunc

Func _FindLastArray($aPath)
    

EndFunc

Func _FindLastDirPos($sPath)
    $lastPath = _FindLastDir($sPath)
    $lastDir = _FileListToArray3($lastPath, "*", 2)
    $nStartPos = $lastDir[0]
    $blnTest = False
    Do
        If $lastDir[$nStartPos] = $sPath Then
            $blnTest = True
        Else
            $nStartPos = $nStartPos - 1
        EndIf
    Until $blnTest = True
    Return $nStartPos
EndFunc


;Type Source and Destination
$src = "T:\Test\Drive 1"
$dst = "T:\Test\Drive 2"

;Check the DriveGetDrive
$aryDir = _FileListToArray3($src, "*", 2)
$strPos = $aryDir[0]
$strFld = $aryDir[$strPos]
$blnCheck = False
$blnForward = False
$strBounds = 1

Do

    $blnCheck = False
    $blnForward = False
    
    If $blnCheck = False Then
        If StringIsDigit(StringRight($strFld, 4)) Then
            If StringRight($strFld, 4) < 2002 Then
                DirCopy($strFld, $dst & StringRight($strFld, StringLen($strFld) - StringLen($src)), 1)
        ;MsgBox(0, "Copied", "Copied: " & $strFld & " to " & $dst & StringRight($strFld, StringLen($strFld) - StringLen($src)))
            EndIf
        EndIf
    EndIf
;======================================================
;Checks to see if the folder date is before 2001
;======================================================
    
    
    
    If $blnCheck = False Then
        If StringLen($strFld) > 1 Then
            $aGetLastModified = FileGetTime($strFld)
            If $aGetLastModified[0] < 2002 Then
                DirCopy($strFld, $dst & StringRight($strFld, StringLen($strFld) - StringLen($src)), 1)
            EndIf
        EndIf
    EndIf
;======================================================
;Checks to see if the date modified is before 2001
;======================================================
    
    If $blnCheck = False Then
    If StringLen($strFld) > 1 Then
        If StringLen(_FindNumofDir($strFld)) > 0 Then
            $aryDir = _FileListToArray3($strFld, "*", 2)
            $strPos = $aryDir[0]
            $strFld = $aryDir[$strPos]
            $blnCheck = True
            $blnForward = True
            $strBounds = $strBounds + 1
    ;MsgBox(0, "Forward!", "Move Forward!")
        EndIf
    EndIf
    EndIf
;======================================================
;Checks to see if there are folders within the folder and moves in if there are
;======================================================
    $count = 0
    
;MsgBox(0, "strPos and strBounds", $strPos & " and " & $strBounds)
    If $blnForward = False Then
    If $strPos > 1 Then
;MsgBox(0, "Up!", "Move UP!")
        $strPos = $strPos - 1
        $strFld = $aryDir[$strPos]
        $blnCheck = True
    ElseIf $strPos = 1 Then
        If $strBounds - 1 > 0 Then
            Do
        ;MsgBox(0, "find last last dir", _FindLastDir($strFld))
                $aryDir = _FileListToArray3(_FindLastDir(_FindLastDir($strFld)))
        ;MsgBox(0, "find last last dir pos", _FindLastDirPos(_FindLastDir($strFld)))
                $strPos = _FindLastDirPos(_FindLastDir($strFld))
        ;MsgBox(0, "find last last dir pos", $aryDir[$strPos])
                $strFld = $aryDir[$strPos]
                $strBounds = $strBounds - 1
        ;MsgBox(0, "Back!", "Move Back!")
            Until $strPos > 1
    ;MsgBox(0, "Up!", "Move UP!")
            $strPos = $strPos - 1
            $strFld = $aryDir[$strPos]
            $blnCheck = True
        EndIf
    EndIf
    EndIf
;======================================================
;Checks positioning moves up if there are more folders and moves back if there aren't.
;======================================================


Until $strBounds < 1
Edited by Uronacid

Share this post


Link to post
Share on other sites

Why not use Robocopy from Microsoft? Free tool. :P

Share this post


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
Sign in to follow this  
Followers 0