Jump to content

Create a Backup of a text file with the help of autoit


remin
 Share

Recommended Posts

In my text editor I created a script (text editor script language) to backup files like this:

p.e. mytextfile.txt -->

bkp_Filename without extension_Date_Time.extension

bkp_mytextfile_2014-12-19_10-23.txt

I was wondering if I could create a script in autoit to do this globally p.e. in word 2007, wordpad or whatever other text editor.

Maybe it is not so difficult to construct the backup name of the file but I don't have any idea how to capture the current filename of the document I'm currently editing.

Is there a way in autoit to know the current filename of a document?

Remin

ps: I wish everybody a happy new year!

Link to comment
Share on other sites

_FilelistToArray will give you all the filenames in a current directory specified.

Happy new year!

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Is there a way in autoit to know the current filename of a document?

Given the number of different text editors and the difficulty of identifying which file(s) are currently opened within each text editor, I don't think so.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hey Breathe, what's up !?1?

 

Howdy kylosmas ^_^

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

I've created a small script to create a backup of the current word file:

#include <Word.au3>

HotKeySet("!5",  "_BkpWordDoc")

Func _BkpWordDoc()
   Global $oWord = _Word_Create()
   Global $oDoc = _Word_DocGet($oWord, 1)
   Local $myDocNam = StringRegExpReplace($oDoc.Name, ".doc*", "")
   Local $filetime = FileGetTime($oDoc.FullName, 0, 0)
   ;backup using actual time (bkp_filename_date_time.filextension)
   ; FileCopy($oDoc.FullName, "d:\bkp_" & $myDocNam & "_" & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "-" & @MIN & ".doc", 1)
   ;backup using modified time (bkp_filename_date_time.filextension)
   FileCopy($oDoc.FullName, "d:\bkp_" & $myDocNam & "_" & $filetime[0] & "-" & $filetime[1] & "-" & $filetime[2] & "_" & $filetime[3] & "-" & $filetime[4] & ".doc", 1)
EndFunc

Maybe someone is interested in it.

Wish I could find a way to do this in all files p.e. .au3/txt/php/ini/py/doc*/xls* etc :)

Edited by remin
Link to comment
Share on other sites

Updated version:

- backup also of .docx files.

#include <Word.au3> 

HotKeySet("!5",  "_BkpWordDoc")

Func _BkpWordDoc()
   Global $oWord = _Word_Create()
   Global $oDoc = _Word_DocGet($oWord, 1)

   Local $myDocName = StringRegExp($oDoc.Name, ".*(?=\.[a-zA-Z0-9]*$)", 1)
   Local $myDocExt  = StringRegExp($oDoc.Name, "\.[a-zA-Z0-9]*$", 1)
   Local $filetime  = FileGetTime($oDoc.FullName, 0, 0)

   ;backup using actual time
   ; FileCopy($oDoc.FullName, "d:\bkp_" & $myDocName[0] & "_" & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "-" & @MIN & $myDocExt[0], 1)
   ;backup using modified time
   FileCopy($oDoc.FullName, "d:\bkp_" & $myDocName[0] & "_" & $filetime[0] & "-" & $filetime[1] & "-" & $filetime[2] & "_" & $filetime[3] & "-" & $filetime[4] & $myDocExt[0], 1)
EndFunc
Link to comment
Share on other sites

Wish I could find a way to do this in all files p.e. .au3/txt/php/ini/py/doc*/xls* etc :)

Why are you just not using the file path with the FileCopy command?

Without too much trouble, you can do a backup copy of any file, even with an incremental file name, which is easily coded.

Don't know why you are even using Word to do any of this.

FileCopy allows you to rename, plus you can use date and time macros to be part of that renaming.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Why are you just not using the file path with the FileCopy command? ....

FileCopy allows you to rename, plus you can use date and time macros to be part of that renaming.

 

That would be nice but can you please tell me how to find the path and the current filename of the file I'm editing?

Using FIleCopy I have to go to Windows Explorer, copy the filename and insert it in a input box invoked from the  backup_function, isn't it?

Link to comment
Share on other sites

  • 2 weeks later...

#include <Word.au3> 

HotKeySet("!5",  "_BkpWordDoc")

Func _BkpWordDoc()
   Global $oWord = _Word_Create()
   Global $oDoc = _Word_DocGet($oWord, 1)

   Local $myDocName = StringRegExp($oDoc.Name, ".*(?=\.[a-zA-Z0-9]*$)", 1)
   Local $myDocExt  = StringRegExp($oDoc.Name, "\.[a-zA-Z0-9]*$", 1)
   Local $filetime  = FileGetTime($oDoc.FullName, 0, 0)

   ;backup using modified time
   FileCopy($oDoc.FullName, "d:\bkp_" & $myDocName[0] & "_" & $filetime[0] & "-" & $filetime[1] & "-" & $filetime[2] & "_" & $filetime[3] & "-" & $filetime[4] & $myDocExt[0], 1)
EndFunc

Once in a while I receive this error:

Local SmyDocName = StringRegExp($oDoc.Name, ".*(?=.[a-zA-Z0-9]*$)", 1)
Local SmyDocName = StringRegExp(SoDoc^ ERROR
Error: Variable must be of type "Object"
 
Does anybody know why?
Edited by remin
Link to comment
Share on other sites

That would be nice but can you please tell me how to find the path and the current filename of the file I'm editing?

Using FIleCopy I have to go to Windows Explorer, copy the filename and insert it in a input box invoked from the  backup_function, isn't it?

Ok, didn't realize you were talking about a file you had open in Word and were working on.

Word has backup/save option.

But you are probably like me and don't like them.

In that case, you should be using Word Macros (VBA) to do what you want. No need for AutoIt at all. That's what I do. Use a button or menu item to run your VBA macro.

If you really need to, you can even use AutoIt to run that VBA macro ... see the Word UDF.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

remin,

If your purpose is to do real time (or close to it) backups then check out >this thread.  However, if the data currency window is larger (you define what "larger" means in your context) then any incremental/differential scheme should work. 

Good Luck,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Show the code where $oDoc is created...and re-read post #5...

 

Hi Kylomas,

Please see post#12

ps: Agree that there are many different editors. I'm sure I need more than one backup script in the future  :)

Edited by remin
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...