Sign in to follow this  
Followers 0
Eru

USB Drive Backup & FileCopy()

4 posts in this topic

#1 ·  Posted (edited)

I have a USB drive that I carry around in a little pouch in my wallet with a lot of information I'd hate to lose, namely my homework. :whistle: I'm trying to make a script that will detect the drive, then copy my homework from it to my documents folder. (I usually do my homework at school and keep the info on the jump drive, but I want it backed up at home just in case.)

I found a nice function that detects the drive but my copying isn't working. :D I want it to copy my .txt and .doc files to my documents.

Local $FileType[2]
$FileType[0] = "*.txt"
$FileType[1] = "*.doc"
$End = Ubound($FileType) - 1
        FT=0
    For $FT=0 to $End Step 1
           $file = FileFindFirstFile($FileType[$FT])
           Backup()
           $file = FileFindNextFile($FileType[$FT])
           Backup()
        Next
               MsgBox(1, "Done", "All Copied!")

$FileType is an array for the types of files I want to backup. Right now, I'm just working with .txt and .doc files, but if I start having to make spreadsheets in my class, I want to be able to add that functionality easily.

The Backup() function is just a one line function:

FileCopy ($file, "My Documents Path", 1)

Problem is, nothing's copying. I get the "All Copied!" message, but the first part doesn't work. :lmao:

Possible Problems I've Thought of:

*The FileFindFirstFile and FileFindNextFile don't know what directory I'm looking in and I need to tell it to look in the USB drive. (but how?)

*Syntax error (but where?)

*The $FileType array is set up wrong... (I do need the quotes right?)

*I have a potato for a brain (Very likely.)

Thanks for the input. :P I'll be back on later to check for replies.

Edited by Eru

Share this post


Link to post
Share on other sites



You should read the help for FileFindFirst! You shouldn't post problem code which you haven't even checked yourself. It doesn't return a file as you think in your script but a handle to be used for FileFindNext. FileFindNext uses the handle as a paramater not a file name.

If you have problems with a script then try adding some lines to find out what's wrong. Maybe like this-

MsgBox(0,"$file returned by FileFindFirst is",$file)

Then you would see something was going wrong without needing to ask.

Also, you might need to include the path to your usb drive unless you have already changed directory to it.

For Example

$sr = FileFindFirst('F:\' & Filetype[0])


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Hi, try this one:

#include <Array.au3>

$Extensions = "*.doc|*.txt"
$UsbDrive = "I:"
$DestDir = @MyDocumentsDir

$ReturnedVal = _BackupFiles($Extensions, $UsbDrive, $DestDir, 0) ; If last parameter is 1, then files that exists will be replaced.

If $ReturnedVal = 1 Then MsgBox(64, "Finish!", "All Files was seccesefull copied")
If IsArray($ReturnedVal) Then _ArrayDisplay($ReturnedVal, "These files was not copied:")

Func _BackupFiles($PaternStr, $Drive, $DestBackupDir, $Replace=0)
    If Not FileExists($Drive) Then Return SetError(1, 0, -1)
    Local $ExtArray = StringSplit($PaternStr, "|")
    Local $NotBackupedFilesArr[1], $FileFind, $CurrentFile, $CopyErr
    For $i = 1 To UBound($ExtArray)-1
        $FileFind = FileFindFirstFile($Drive & "\" & $ExtArray[$i])
        While $FileFind <> -1
            $CurrentFile = FileFindNextFile($FileFind)
            If @error Then ExitLoop
            $CopyErr = FileCopy($Drive & "\" & $CurrentFile, $DestBackupDir & "\" & $CurrentFile, $Replace)
            If $CopyErr = 0 Then
                ReDim $NotBackupedFilesArr[UBound($NotBackupedFilesArr)+1]
                $NotBackupedFilesArr[UBound($NotBackupedFilesArr)-1] = $CurrentFile
            EndIf
        WEnd
        FileClose($FileFind)
    Next
    If UBound($NotBackupedFilesArr) > 1 Then Return $NotBackupedFilesArr
    Return 1
EndFunc

Syntax error (but where?)

FT=0 - The variable must start with $ :whistle:

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

This script doesn't look for the sub folders on the c: drive. How can I change it so that it looks for the whole drive?

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