Jump to content

Moving a file from "Point A" to "Point B"


Recommended Posts

Greetings fellow Autoit-ers!

I have been working on some coding and I had some questions. I’m still new to Auto-it and so I’m working to get my footing here.

What I’m trying to do:

There is a .txt file called “report.txt” that is generated each day and it sets at C:\Reports. I use my thumb drive at work because I’m mobile most of the time. I need to click on my Autoit program and have it copy “report.txt” from C:\Reports to where ever my drive is plugged in (E:\Reports, F:\ Reports, G:\ Reports, H:\ Reports, etc). The Autoit program will be setting on the thumb in the “Reports” folder. I understand how to use:

FileMove("C:\Reports\report.txt", "E:\Reports\report.txt", 0)

But how do I account for the changing of the destination location? Also, is there a FileCopy() option that will work? I don't want to "move" it just make a copy.

The drive letter may change depending on which computer at work I pull this report from. The “C:\Reports” location never changes, but I am pulling this report from different computers at different times of the day.

Then it gets even more tricky! I need to time/date stamp or even place in a line within the document as I move it. This way I know that this document has been moved by me (and not just an old one I had setting there from before). The fourth (4th) line within the report.txt document has miscellaneous information I want to delete and then rewrite something like:

“This document has been moved by me on:” " & @MON & @MDAY & @YEAR & @HOUR & @MIN & @MIN & @MSEC”

Or even if date/time cannot be inserted how could I simply delete the line of text and have my own text auto-inserted?

Any assistance on this would be greatly appreciated!

Your geek,

Matt~

Link to comment
Share on other sites

On your flashdrive make an empty file called report.check.

$CheckFile = "\Report.check"

$Drive = DriveGetDrive("ALL")

For $D = 1 To $Drive[0]
    $ReportFolder = $Drive[$D] & "\Reports\"
    $ReportFile = $ReportFolder & "Report.txt"
    If FileExists($Drive[$D] & $CheckFile) Then 
        If Not FileExists($ReportFolder) Then DirCreate($ReportFolder)
        FileCopy("C:\Reports\Report.txt", $ReportFile, 1)
    EndIf
Next

That should work, although it's untested and I'm tired. :idea:

Edit: Also look at _FileReadToArray() and _FileWriteFromArray(). Instead of copying, read it to an array, change what you need, and write it to the new path.

If the line you want to change isn't always fixed (example: line 4) then look at StringInStr() or StringRegExp(). Otherwise, you can make changes to the array returned by _FileReadToArray().

Edited by darkjohn20
Link to comment
Share on other sites

#include <file.au3>

AutoItSetOption("MustDeclareVars", 1)

Dim $SourceFileName = "c:\Reports\report.txt"
Dim $DestinFileName
Dim $Drives
Dim $i

; ---------------------------------------------------------
; search destination directory. this is done by looking for
;     a "\reports\" directiory on every drive letter. the
;     first one found will be taken
; ---------------------------------------------------------
$Drives = DriveGetDrive( "all" )
If NOT @error Then
    ;MsgBox(4096,"", "Found " & $var[0] & " drives")
    For $i = 1 to $Drives[0]
        ;MsgBox(4096,"Drive " & $i, $Drives[$i])
        If $Drives[$i] <> "c:" Then
            If FileExists( $Drives[$i] & "\Reports\" ) Then
                $DestinFileName = $Drives[$i] & "\Reports\Report.txt"
                ExitLoop
            EndIf
        Endif
    Next
EndIf

#cs
If $DestinFileName = "" Then
    MsgBox(4096,"Destination", "destination directory not found")
Else
    MsgBox(4096,"Destination", "Destination = " & $DestinFileName)
EndIf
#ce

; ---------------------------------
; copy file (with changing line #4)
; ---------------------------------

;  first open the source file ...
; -------------------------------
Dim $aRecords
If Not _FileReadToArray($SourceFileName, $aRecords) Then
   MsgBox(4096,"Error", " Error reading file to Array     error:" & @error)
   Exit
EndIf
If $aRecords[0] < 4 Then
    Msgbox(0, "ERROR", "to less records in sourcefile: " & $aRecords[0])
    Exit
EndIf

; ... then change line #4 ...
; ---------------------------
$aRecords[4] = "This document has been moved by me on: " & @MON & @MDAY & @YEAR & @HOUR & @MIN & @MIN & @MSEC

; ... finally write file to destinaton
; -------------------------------------
_FileWriteFromArray($DestinFileName, $aRecords, 1)


; E N D ; =========================

A-Jay

Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Link to comment
Share on other sites

Assuming your script is going to be on the flash drive, it's pretty simple to get the drive designation.

$sDrive = StringLeft(@ScriptFullPath, 3)

To get the destination path without going through the above step, use the same principal

$sDestFldr = StringLeft(@ScriptFullPath, 3) & "Reports\"

Then you can use something like this for changing the file line

$sFile = "C:\Reports\report.txt"
$sLine = FileReadLine($sFile, 4)
$sStr = FileRead($sFile)
$sStr = StringReplace($sStr, $sLine, "This document has been moved by me on: " & @MON & @MDAY & @YEAR & @HOUR & @MIN & @MIN & @MSEC)
$hFile = FileOpen($sFile, 2)
If $hFile<> -1 Then
    FileWrite($hFile, $sStr)
    FileClose($hFile)
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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