Jump to content

Text file as counter


tazdev
 Share

Recommended Posts

I am looking for a way to make a text file a counter for a script. Here is what I am trying to do. I want a script to run everyday but only actually back up some files on the C:\ to a network drive. Because this is going to occur everyday I want the backup only to occur twice a month.

I want to know how I can extract a number from a text file. Say from the last line. I want to extract that string, convert it to a number, add 1 to it, append the file and if the number = 15 to execute the backup.

I have looked in the help files and searched the forums and I cannot find a function that will convert a String to an Interger.

Anyone have any ideas?

Link to comment
Share on other sites

RTM,

Number() or Int()

But can't you check just the date? If it is 01 or 15?

:idiot: you are in the wrong forum!

<{POST_SNAPBACK}>

First off notice the time: Today, 02:21 AM - I wasn't fully awake LOL

I tried Number() and Int() and they didn't work but obviously I did something wrong so I will try it again. Thank you for your response. :D

Sorry about the wrong forum. I noticed that this morning just before writing this reply. ;)

Won't make that mistake again. Can the mod move it to the correct forum? If so, thanks. If not, oh well. :lol:

Yes I can use the date but I wanted to see if I could use a counter so that others can see the file and know where things stand. Currently I am using @Wday as the counter so that it will run once a week.

edit: Another reason is that 1 or 15 could be on a weekend and if they are not at work then the script won't run. If I had a counter of some sort then I can have it set to run every 15th time they log in.

Edited by tazdev
Link to comment
Share on other sites

You can also use FileReadLine(). You'll also have to check out FileOpen()

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
Wend

FileClose($file)

That example is pretty much from the Help File, I just took out the MsgBox() that displays each line, b/c all you need is the last line. So, after the While ... Wend Loop runs, the variable $line will be the last line of your file.

Hope that helps,

Ian

Oh, BTW, I'm sure someone will post a better way to read the last line of a file, but that's the only way I know.

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

Thanks, I had that part already I was just trying to get the last line with the string of "1" to make an int of 1 so I can do 1 + 1 and append to the last line with 2 then the next login it will be 3 etc until it gets to 15 then an if statement that basically says if $var1 = 15 then run the backup else just run the textfile update. :idiot:

Link to comment
Share on other sites

Ok, new plan. Here's what I've got so far. I'll try to finish after lunch.

#include <file.au3>

$file = FileOpen("c:\counter.txt", 0)
$CountLines = _FileCountLines("c:\counter.txt")
$line = FileReadLine($file, $CountLines)

FileClose($file)

$counter = $line + 1
MsgBox(0,"Counter:", $counter)

Hope that helps,

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

Ok, new plan.  Here's what I've got so far.  I'll try to finish after lunch.

#include <file.au3>

$file = FileOpen("c:\counter.txt", 0)
$CountLines = _FileCountLines("c:\counter.txt")
$line = FileReadLine($file, $CountLines)

FileClose($file)

$counter = $line + 1
MsgBox(0,"Counter:", $counter)

Hope that helps,

Ian

<{POST_SNAPBACK}>

Won't $counter be 1 since $line is a string not an integer? That was the problem but if this fixes it then Crap that is just too simple. Man I love AutoIT
Link to comment
Share on other sites

Won't $counter be 1 since $line is a string not an integer? That was the problem but if this fixes it then Crap that is just too simple. Man I love AutoIT

<{POST_SNAPBACK}>

I created a file called counter.txt with:

1

2

3

The msgbox came back with 4

I LOVE AUTOIT

Link to comment
Share on other sites

Ok, here's what I've come up with...

$count = IniRead("c:\counter.ini", "Counter", "Count", "Value Not Found")
If Not $count = "Value Not Found" Then
   $count = $count + 1
   IniWrite("c:\counter.ini", "Counter", "Count", $count)
Else
   MsgBox(0, "Error reading counter.ini", "There was an error reading the counter.ini file, so the counter was not updated")
EndIf

The Counter.ini file should read...

[Counter]Count=10

Of course, Count=10 in the INI file would be whatever number you want to start with. But that seems to work.

Again, I hope this helps,

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

What about this?

$count = IniRead("c:\counter.ini", "Counter", "Count", "0")
$count = $count + 1
If $count = 15 Then
  ;update...
   IniWrite("c:\counter.ini", "Counter", "Count", "0")
Else
   IniWrite("c:\counter.ini", "Counter", "Count", $count)
EndIf
Link to comment
Share on other sites

Thank You

ioliver and SlimShady w00t it is working. I will post it as soon as I incorporate this into it. I am going to use the txt one though because I have some additional info in the txt. This way I have one place to look for the extra stuff I need like username, OS Type, Service Pack level, etc.

Link to comment
Share on other sites

Finished product. It could be cleaner I am sure but I have not used AutoIT much so I just did it as it appeared in my head :idiot: I made it to help swap out Windows NT and 2000 PC's at work when they break. This way we can set the user up close to what they were before the crash.

edit: Oh the Network drive is V:\ cause there was a problem with AD and it settings for each user. @Homeshare was blank. Also the PC Name has a 6 digit inventory tag number for me that is part of the computer's network name so I have it extracted into a string.

; Created to backup profile specific files
; to speed up the transfer of a user from
; one PC to another. Will also provide some
; other info like OS Type and Service Pack
; Version, IP Address, and last time logged in
; The text file will be located on the C:\
; and the V:\ (Home net drive). I use V:\ 
; because @HomeDrive wasn't working properly
; with the AD. Why? Because AD is not set up
; right LOL
; I would like  it in third location that is 
; hidden so we have all of them in one location
; per user/pc name but that seems to be big. 
; 
; Tazdev
; Version 1.01
; 
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;@@@@@@@@@ Variable Creation Area @@@@@@@@@@@@@
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; User Backup Directory on server. Should be
; \\Servername\userid$\userid\computername
; That way if they work on more than one PC we
; can tell the difference of PC's :)


#include <file.au3>

$UserBackup = ("V:"&"\"&@UserName&"\"&@ComputerName&"\")
$TextFileRoot = ("C:\"&@ComputerName&"\"&@ComputerName&".txt")
$TextFileBackUp = ($UserBackup&@ComputerName&".txt")

; Assign var to the folder where the Outlook files
; located on the PC for this user
$Outlook = ("Microsoft\Outlook\")
$UserOutlook = (@AppDataDir&"\Microsoft\Outlook\")

; Var for OS Type Windows 98 or 2000 and the 
; service pack it is on
$OSandPack = (@OSTYPE&" "&@OSServicePack)


; Internet Explorer var1 and 2
$Ql = ("Microsoft\Internet Explorer\Quick Launch\")
$UserQl = (@AppDataDir&"\Microsoft\Internet Explorer\Quick Launch\")
$Favs = ("Favorites\")
$UserFavs = @FavoritesDir&"\"

; Recent Documents
$Recent = ("My Recent\")
$UserRecent = (@UserProfileDir&"\Recent\")

; Business Applications Folder
$PCBusApps = (@DesktopCommonDir&"\Business Applications\")
$BusApps = ("Desktop\Business Applications\")

; User Startup folder
$StartUp = ("Start Menu\Programs\Startup\")
$UserStartUp = (@UserProfileDir&"\Start Menu\Programs\Startup\")

; User My Documents folder
$UserMyDoc = @MyDocumentsDir

; User Desktop folder
$UserDesktop = @DesktopDir

; Get current date mm-dd-yyyy
$CurDate = (@Mon&"-"&@Mday&"-"&@year)

; Get Asset #
$result = StringRight (@ComputerName, 6)

; Get the counter setup
DirCreate("C:\"&@ComputerName)
$filea = FileOpen($TextFileRoot, 1)
; Check if file opened for writing OK
    If $filea = -1 Then
        MsgBox(0, "Error", $filea&" - Unable to open local file.")
        Exit
    EndIf
FileClose($TextFileRoot)
$file = FileOpen($TextFileRoot, 0)
$CountLines = _FileCountLines($TextFileRoot)
$line = FileReadLine($file, $CountLines)
FileClose($file)

$counter = $line + 1
; MsgBox(0, $counter, $counter)
; end routine for counter setup
; Counter should now equal 1 plus the last number
; in the txt file on the C:\


;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;@@@@@@@@@   End of Varible Creation  @@@@@@@@@
;@@@@@@@@@          Area            @@@@@@@@@
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


; Start
CheckDrive()


;
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Error to display when drive is not mapped per
; Active Directory settings. 

Func Errormapping()
    MsgBox(0, "Error", "Your Drive mapping for your V:\ is incorrect or 

does not exists. Please contact the PC Administrator - AutoIT")
    VerifyLocal()
    UpdateTextLocal()
    Exit
EndFunc

;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Verify if the local file is greater than 20
; if so then delete it and make a blank one
; that way if this is perm then the file will 
; never be greater than 20 sessions

Func VerifyLocal()
    If $count > 20 then
        FileDelete($TextFileRoot)
        FileOpen($TextFileRoot, 0)
        FileClose($TextFileRoot)
    EndIf
EndFunc

;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;
;
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Check to see if the homedrive is really there

Func CheckDrive()
If FileExists ("V:\") then
    verifyday()
Else
    Errormapping()
Endif
EndFunc


;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;
;
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Verify that it is the 15th time they have
; logged in.


Func verifyday()
    If $counter > 14 then
        DelTextFiles()
;       CreateTextFiles()
        BackupProfile()
    Else
        UpdateTextAll()
    EndIF
EndFunc

; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;
;
; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Removes the Text Files and recreates them
; See BackupProfile for counter reset

Func DelTextFiles()
    FileDelete($TextFileRoot)
    FileDelete($TextFileBackup)
EndFunc

Func CreateTextFiles()
    FileOpen($TextFileRoot, 0)
    MsgBox(0, $TextFileRoot, $TextFileBackup)
    FileClose($TextFileRoot)
    FileOpen($TextFileBackup, 0)
    FileClose($TextFileBackup)
EndFunc

;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;
;
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; 3 Functions. Last one combines first two
;

Func UpdateTextLocal()

; Make text file for local drive
    DirCreate("C:\"&@ComputerName)
    $filea = FileOpen($TextFileRoot, 1)
; Check if file opened for writing OK
    If $filea = -1 Then
        MsgBox(0, "Error", $filea&" - Unable to open local file.")
        Exit
    EndIf

    FileWrite($filea, "----------------------------" & @CRLF)
    FileWrite($filea, $CurDate & " " & @HOUR & ":" & @MIN & ":" & @SEC & 

@CRLF)
    FileWrite($filea, $result & @CRLF)
    FileWrite($filea, @ComputerName  & " " & @OSType & " " &  

@OSServicePack &  @CRLF)
    FileWrite($filea, @UserName & @CRLF)
    FileWrite($filea, $counter & @CRLF)
    FileClose($filea)
Endfunc

Func UpdateTextBackup()
; Make text file for server drive
    DirCreate($UserBackup)
    $fileb = FileOpen($TextFileBackup, 1)
; Check if file opened for writing OK
    If $fileb = -1 Then
        MsgBox(0, "Error", "Unable to open server file.")
        Exit
    EndIf
    FileWrite($fileb, "----------------------------" & @CRLF)
    FileWrite($fileb, $CurDate & " " & @HOUR & ":" & @MIN & ":" & @SEC & 

@CRLF)
    FileWrite($fileb, $result & @CRLF)
    FileWrite($fileb, @ComputerName  & " " & @OSType & " " &  

@OSServicePack &  @CRLF)
    FileWrite($fileb, @UserName & @CRLF)
    FileWrite($fileb, $counter & @CRLF)
    FileClose($fileb)

EndFunc

Func UpdateTextAll()
    UpdateTextLocal()
    UpdateTextBackup()
EndFunc



;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Backup specific files from user's desktop PC
; and their Profile
; 

Func BackUpProfile()
    $counter = 1
    UpdateTextAll()
; IE Favs
    DirCreate($UserBackup & $Favs)
    DirCopy ($UserFavs, $UserBackup & $Favs, 1)

; Quicklaunch
    DirCreate($UserBackup & $Ql)
    FileCopy ($UserQl&"*.*", $UserBackup & $Ql, 1)
 
; MyDocuments
    DirCreate($UserBackup & "My Documents\")
    DirCopy ($UserMyDoc, $UserBackup & "My Documents\", 1)
 
; 
; UserDesktop
    DirCreate($UserBackup & "Desktop\")
    DirCopy ($UserDesktop, $UserBackup & "Desktop\", 1)

;   MsgBox(0, "Getting ready for",$UserBackup & $BusApps) 
; Business Applications folder
    DirCreate($UserBackup & $BusApps)
    DirCopy ($PCBusApps, $UserBackup & $BusApps, 1) 
;   MsgBox(0, "Should Exist Now",$UserBackup & $BusApps) 
; 
; My Recent Documents
;   DirCreate($UserBackup & $Recent)
;   DirCopy ($UserRecent, $UserBackup & $Recent, 1)

; Outlook
    DirCreate($UserBackup & $Outlook)
    FileCopy($UserOutlook, $UserBackup & $Outlook & "\*.fav", 1)
    FileCopy($UserOutlook, $UserBackup & $Outlook & "\*.pab", 1)

; 
; Startup folder
    DirCreate($UserBackup & $StartUp)
    FileCopy ($UserStartup&"*.*", $UserBackup & $StartUp, 1)
;   MsgBox(0, "Backup","Done boyeeeeeee!") 
EndFunc 
;
;
Edited by tazdev
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...