Sign in to follow this  
Followers 0
Unc3nZureD

Unknown Error (Pendrive Backup)

2 posts in this topic

#1 ·  Posted (edited)

Hi guys,

I've started a simple project but I got stuck. At all it would be a safe copy from a pendrive if you plug it in. First I'd like to write the core, then later I'll add some extras like a loading screen, a question if you really want to make a copy of it or even you could choose where to put the copied files. So my current source is here:

#include <File.au3>
$BackupFolder = @HomeDrive & "PendriveBackups"
DirCreate($BackupFolder)
While 1
$Drives = DriveGetDrive("REMOVABLE")
ToolTip("Step 1", 0, 0)
If IsArray($Drives) Then
  $Count = 0
  While 1
   $Count = $Count + 1
   $FNum = 0
   While 1
    $FNum = $FNum + 1
    $Files = _FileListToArray($Drives[$Count] & "", "*", "1")
    If IsArray($Files) Then
     FileCopy($Drives[$Count] & "" & $Files[$FNum], $BackupFolder & "" & $Files[$FNum], 1)
     If $Files[0] = $FNum Then ExitLoop
    EndIf
    If Not IsArray($Files) Then ExitLoop
   WEnd
   If $Drives[0] = $Count Then ExitLoop
  WEnd
  $Count = 0
  While 1
   $Count = $Count + 1
   $FNum = 0
   While 1
    $FNum = $FNum + 1
    $Files = _FileListToArray($Drives[$Count] & "", "*", "2")
    If IsArray($Files) Then
     DirCopy($Drives[$Count] & "" & $Files[$FNum], $BackupFolder & "" & $Files[$FNum], 1)
     If $Files[0] = $FNum Then ExitLoop
    EndIf
    If Not IsArray($Files) Then ExitLoop
   WEnd
   If $Drives[0] = $Count Then ExitLoop
  WEnd
EndIf
Sleep(200)
WEnd

I filled my pendrive with near 390Mb junk stuffs, but I don't know why sometimes the program stops with the following problem:

C:UsersUnc3nZureDDesktopUSBCopy.au3 (##) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
DirCopy($Drives[$Count] & "" & $Files[$FNum], $BackupFolder & "" & $Files[$FNum], 1)
DirCopy($Drives[$Count] & "" & ^ ERROR

I'm still looking for the problem, I've just posted it if somebody can find it earlier than me.

If you can help me then thanks a lot! (If not then thanks for reading)

~ Unc3nZureD

Edited by Unc3nZureD

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

wow you should really look up how to use for/next loops and managing arrays.

----> http://www.autoitscript.com/wiki/Arrays

Its so much easier (and saver) than using while loops.

Simplified code:

#include <File.au3>
$BackupFolder = @HomeDrive & "PendriveBackups"
DirCreate($BackupFolder)

$Drives = DriveGetDrive("REMOVABLE")
ToolTip("Step 1", 0, 0)


;~ ###### Example 1 #######
For $Count = 1 To UBound($Drives) - 1 ; will not execute if no array (for 1 to 0)

    $Files = _FileListToArray($Drives[$Count] & "", "*", "1")
    For $FNum = 1 To UBound($Files) - 1 ; will not execute if no array (for 1 to 0)
        FileCopy($Drives[$Count] & "" & $Files[$FNum], $BackupFolder & "" & $Files[$FNum], 1)
    Next

    $Folders = _FileListToArray($Drives[$Count] & "", "*", "2")
    For $FNum = 1 To UBound($Folders) - 1 ; will not execute if no array (for 1 to 0)
        DirCopy($Drives[$Count] & "" & $Folders[$FNum], $BackupFolder & "" & $Folders[$FNum], 1)
    Next

    Sleep(200)
Next
Exit


;~ ###### Example 2 #######
For $Count = 1 To UBound($Drives) - 1 ; will not execute if no array (for 1 to 0)

    DirCopy($Drives[$Count], $BackupFolder , 1)

    Sleep(200)
Next

Exit

Example 2: And its even easier if you realize that DirCopy also works for whole drives :oops:

Edit: Oh and you get the error because after every copy operation, you read the whole file list again.

So if you delete or move a file while the operation is running you have less files in the array than you want to index with $FNum therefore you get:

subscript dimension range exceeded

Edited by qsek

Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

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