Create a backup copy of files

Help with writing and playing macros

Create a backup copy of files

Postby V@no » Thu Feb 24, 2005 12:32 am

Hi!
On the old forum someone posted a code for a batch (.bat) file which would save current opened file in the same directory of the file, but with a different filename:
filename.ext.<year>-<month>-<day>_<hour>-<min>-<sec>.bak
This little script was extremly helpfull, all I had to do is press CTRL+SPACE (my chose) and it would create a backup copy of the file...it was working for me as a manual CVS server...:)
Anyway, I've lost that script, and the old forum is gone...does someone has such script or maybe someone could make a new one?
Or maybe there is already something like this? maybe even better? 8O

Thank you!
User avatar
V@no
Basic User
Basic User
 
Posts: 20
Joined: Thu Jul 22, 2004 11:00 pm

Re: Create a backup copy of files

Postby Bego » Thu Feb 24, 2005 9:57 am

Hi V@no,

I have a macro that does the job for me (similarily)
IT HAS TO HAVE the property "search again, if nothing found !"


macro: savecopy
Dont be surprised: its long, but easy :D
- remember filename and date/time in line 1
- mark the file
- copy file and file information to a new tab
- replace the : in the filename ! (german date property here)
any better suggestions for regexp replace nicely seen :!:
- save the file with time information at the end of filename
- kill tab and go back to the old file
- remove Line 1

No guarantee ! Enjoy.
Bego

InsertMode
ColumnModeOff
HexOff
UnixReOff
GotoLine 1
Key HOME
"
"
Key UP ARROW
CopyFilePath
Paste
TimeDate
SelectAll
StartSelect
Copy
NewFile
Paste
GotoLine 1
Key HOME
StartSelect
Key END
Find RegExp Select "00:^([0-9][0-9]^)"
Replace All "00_^1"
Find RegExp Select "01:^([0-9][0-9]^)"
Replace All "01_^1"
Find RegExp Select "02:^([0-9][0-9]^)"
Replace All "02_^1"
Find RegExp Select "03:^([0-9][0-9]^)"
Replace All "03_^1"
Find RegExp Select "04:^([0-9][0-9]^)"
Replace All "04_^1"
Find RegExp Select "05:^([0-9][0-9]^)"
Replace All "05_^1"
Find RegExp Select "06:^([0-9][0-9]^)"
Replace All "06_^1"
Find RegExp Select "07:^([0-9][0-9]^)"
Replace All "07_^1"
Find RegExp Select "08:^([0-9][0-9]^)"
Replace All "08_^1"
Find RegExp Select "09:^([0-9][0-9]^)"
Replace All "09_^1"
Find RegExp Select "10:^([0-9][0-9]^)"
Replace All "10_^1"
Find RegExp Select "11:^([0-9][0-9]^)"
Replace All "11_^1"
Find RegExp Select "12:^([0-9][0-9]^)"
Replace All "12_^1"
Find RegExp Select "13:^([0-9][0-9]^)"
Replace All "13_^1"
Find RegExp Select "14:^([0-9][0-9]^)"
Replace All "14_^1"
Find RegExp Select "15:^([0-9][0-9]^)"
Replace All "15_^1"
Find RegExp Select "16:^([0-9][0-9]^)"
Replace All "16_^1"
Find RegExp Select "17:^([0-9][0-9]^)"
Replace All "17_^1"
Find RegExp Select "18:^([0-9][0-9]^)"
Replace All "18_^1"
Find RegExp Select "19:^([0-9][0-9]^)"
Replace All "19_^1"
Find RegExp Select "20:^([0-9][0-9]^)"
Replace All "20_^1"
Find RegExp Select "21:^([0-9][0-9]^)"
Replace All "21_^1"
Find RegExp Select "22:^([0-9][0-9]^)"
Replace All "22_^1"
Find RegExp Select "23:^([0-9][0-9]^)"
Replace All "23_^1"
Copy
EndSelect
DeleteLine
SaveAs "^c"
CloseFile NoSave
GotoLine 1
DeleteLine
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Create a backup copy of files

Postby palou » Thu Feb 24, 2005 10:45 am

Hi Bego,

I don't have tried but a

Code: Select all
Find RegExp Select "^([0-9][0-9]^):^([0-9][0-9]^)"
Replace All "^1_^2"


Should do the job.

Another remark: Are you sure that your Find/Replace should not be:

Code: Select all
Find RegExp "^([0-9][0-9]^):^([0-9][0-9]^)"
Replace All SelectText "^1_^2"


I quite sure the "Find RegExp Select" will select text from the current
cursor position until the found expression, but in the WHOLE file not
only in the selection.

Regards,
Alain
User avatar
palou
Basic User
Basic User
 
Posts: 46
Joined: Fri Dec 17, 2004 12:00 am
Location: Geneva / Switzerland

Re: Create a backup copy of files

Postby Bego » Thu Feb 24, 2005 12:47 pm

Hi Palou

I tried out your suggestions and they work well :D

So here for everybody the new macro (forget the upper one)



InsertMode
ColumnModeOff
HexOff
UnixReOff
GotoLine 1
Key HOME
"
"
Key UP ARROW
CopyFilePath
Paste
TimeDate
SelectAll
StartSelect
Copy
NewFile
Paste
GotoLine 1
Key HOME
StartSelect
Key END
Find RegExp Select "^([0-9][0-9]^):^([0-9][0-9]^)"
Replace All SelectText "^1_^2"
Copy
EndSelect
DeleteLine
SaveAs "^c"
CloseFile NoSave
GotoLine 1
DeleteLine



see also topic
http://www.ultraedit.com/index.php?name ... opic&t=530
for "replace regexp" for current selection only


Hmmm, someone there who has a nice way to put the "old" extension also to the end of the backup-filename ???

Thanx, Bego :roll:
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Create a backup copy of files

Postby V@no » Thu Feb 24, 2005 6:30 pm

Hi guys, thank you very much!
There are few issues though:
1) Neather of the code worked for me...the first code was opening a new tab, copy the content of the original file into that tab and then showed an error popup "File error", after clicking "Ok" it closes the new tab and and shows another error message "Can not save file". After clicking "Ok", it clear the original file and no undo could be done to restore it 8O
Perhaps I did something wrong...(just for reference I went to Macro menu -> Edit macro -> chosed "backup" macro, in the left part I paste the code -> hit modify -> unchecked all checkboxes -> set the hotkey -> hit "ok" -> on the question "update macro" clicked "yes" -> close macro window.
There couldnt be permissions issue, because if I use normal save it saves the file.

2) this one is very important for me - it "destroys" the clipboard content.

3) if I understood correctly, after executing the macro it will put cursor on first line, is this correct? - not convinient on large files.
User avatar
V@no
Basic User
Basic User
 
Posts: 20
Joined: Thu Jul 22, 2004 11:00 pm

Re: Create a backup copy of files

Postby palou » Fri Feb 25, 2005 2:43 am

Hi V@no,

Could you please post your macro code (Menu Macro->
Edit macro... Select your backup macro in the drop down
menu and, in the edit, select all text copy it in the clipboard
with Ctrl-C and paste it in your post between a "code" "/code"
tag) The last time I get a "File error" was the quote missing after
the SaveAs command.

Perhaps this macro will be better for you if the file is large:
Code: Select all
Save
Clipboard 9
CopyFilePath
NewFile
Paste
TimeDate
".old"
Key HOME
StartSelect
Find RegExp Select "^([0-9][0-9]^):^([0-9][0-9]^)"
Replace All SelectText "^1_^2"
Key END
Cut
CloseFile NoSave
SaveAs "^c"


At this point your file is saved with the new name and still open
in Ue, the one with original name is closed. If it's anoying for you
I can modify the macro to reopen the original file.

For Bego, I have added the ".old" extension to file name ;)

Let me know.
Alain
User avatar
palou
Basic User
Basic User
 
Posts: 46
Joined: Fri Dec 17, 2004 12:00 am
Location: Geneva / Switzerland

Re: Create a backup copy of files

Postby Bego » Fri Feb 25, 2005 2:59 am

You can be helped !

Now I tested it with american date format and got the same error...
The reason for your file error is the american format: mm/dd

So I take the more robust approach and replace all / and : with _
Afterwards I restore the : of e.g. C:
--> Only works with Dos-filenames, not ftp....

Also your Clipboard-content will be maintained now.

The line 1 problem I will look later ....

So far, Bego :wink:

InsertMode
ColumnModeOff
HexOff
UnixReOff
Clipboard 9
GotoLine 1
Key HOME
"
"
Key UP ARROW
CopyFilePath
Paste
TimeDate
SelectAll
StartSelect
Copy
NewFile
Paste
GotoLine 1
Key HOME
StartSelect
Key END
Find Select ":"
Replace All SelectText "_"
Find Select "/"
Replace All SelectText "_"
Find Select " "
Replace All SelectText "_"
EndSelect
Key HOME
Key RIGHT ARROW
Key DEL
":"
Key HOME
StartSelect
Key END
Copy
DeleteLine
SaveAs "^c"
CloseFile NoSave
GotoLine 1
DeleteLine
Clipboard 0
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Create a backup copy of files

Postby palou » Fri Feb 25, 2005 3:51 am

Ok guys,

This is a macro which do the job (at least on my PC) ;)

Code: Select all
Save
IfSel
Key LEFT ARROW
Key RIGHT ARROW
EndIf
"~@#"
Clipboard 8
CopyFilePath
Clipboard 9
SelectAll
Copy
NewFile
Paste
Find Up "~@#"
Delete
Top
"
"
Top
Clipboard 8
Paste
TimeDate
".old"
Key HOME
StartSelect
Key END
Find RegExp " ^([0-9][0-9]^):"
Replace All SelectText "_^1_"
Cut
Delete
SaveAs "^c"
CloseFile NoSave
Top
Find "~@#"
Delete
Clipboard 0


Explanation:
    Start by saving the current file (to not loose the last modif)
    Then test if some text is selected and in that case unselect it
    Insert a tag which shall be unique in the file (for me ~@#)
    Copy file path in clipboard 8
    Copy whole file in clipboard 9
    Create a new temp file
    Paste clipboard 9 in it
    Remove the tag (to not have it in the backup file)
    Add a line at the top and paste file path from clipboard 8
    Add time stamp and new extension
    Replace space and colon by underscore around the time
    Cut new file name in clipboard 8
    Remove added line (now it is empty so Delete is enough)
    Save as new name and close temp file (which return to original file)
    Find tag and delete it to reposition in the original file
    Switch to Windows clipboard (clipboard 0)

It's a bit long but quite complete (in my opinion the date has not the
right format which should be yyyy.mm.dd so alphabetic sort do a
chronologic sort too, but this will complicate a bit the macro).

Regards,
Alain
User avatar
palou
Basic User
Basic User
 
Posts: 46
Joined: Fri Dec 17, 2004 12:00 am
Location: Geneva / Switzerland

Re: Create a backup copy of files

Postby palou » Fri Feb 25, 2005 4:10 am

Oupss, I was writting my post and miss the Bego's response.
So my solution don't resolve the american's date format. Sorry.

Bego,
Did you not have problem with "/" of the file path?

The solution can be like this:
    First insert the time stamp
    Do the corrections
    Go home
    Paste file name
    Copy/Cut the whole line to the clipboard


Regards,
Alain
User avatar
palou
Basic User
Basic User
 
Posts: 46
Joined: Fri Dec 17, 2004 12:00 am
Location: Geneva / Switzerland

Re: Create a backup copy of files

Postby Bego » Fri Feb 25, 2005 4:16 am

Hi Palou and all guys !

You're makro looks more straight forward, but crashes on US-regional-settings.
So I took it and added my more radical replace-logic:

So I think we are all happy now :D

cu, Bego

Save
IfSel
Key LEFT ARROW
Key RIGHT ARROW
EndIf
"~@#"
Clipboard 8
CopyFilePath
Clipboard 9
SelectAll
Copy
NewFile
Paste
Find Up "~@#"
Delete
Top
"
"
Top
Clipboard 8
Paste
TimeDate
".old"
Key HOME
StartSelect
Key END
Find Select ":"
Replace All SelectText "_"
Find Select "/"
Replace All SelectText "_"
Find Select " "
Replace All SelectText "_"
EndSelect
Key HOME
Key RIGHT ARROW
Key DEL
":"
Key HOME
StartSelect
Key END
Cut
Delete
SaveAs "^c"
CloseFile NoSave
Top
Find "~@#"
Delete
Clipboard 0
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Create a backup copy of files

Postby V@no » Fri Feb 25, 2005 6:55 pm

Thanks guys, gonna test the code in a minute.
But still I found the DOS batch version is much simplier, faster and more convinient for me, it had absolutely no side effects as the macro version does... :(

For example the macro adds some characters (unique string, as I understand its to bring the cursor back to the same position after its done) and after macro finished the job, and do "Undo" it will show that string - that could mess u up, if u did some changes in the file and then u remmebered that u forgot to make a backup of the original code, so u do "undo" untill u reach the code u wanted to make backup of, but after the macro u wont be able do "Redo" to bring back your recent changes, because the macro adds that string...

Another issue - is the time format. The US format is MM - DD - YYYY (in my oppinion is so dumb, as dumb as their distance and weight mesurments, no wonder in school they must have calculators :roll: . no offence though)
I'm pretty sertain there is a way instead of just replace all slashes and columns with the underline, shufle the format and make it YYYY - MM - DD
this way the files will be sorted by alphabet AND by date in the same time .
Can this be done?

Thanks again!
User avatar
V@no
Basic User
Basic User
 
Posts: 20
Joined: Thu Jul 22, 2004 11:00 pm

Re: Create a backup copy of files

Postby Bego » Sat Feb 26, 2005 5:35 am

Hi V@no,

Well, I thing the biggest limitation in ultraedit is macro language. It has no variables etc. so you have to live with many workarounds...
One of these is the @#~ or whatever string to reposition text....
Maybe a create bookmark with a name would cover this issue ...
You would run into even more undo trouble when reformatting the date. I would be possible though. Maybe an "stop undo"-command would fix this to
--> Backups surely can be done by outside-os-batches much easier.

Anyway I agree in your anger on american date format :-)

Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Create a backup copy of files

Postby V@no » Sun Feb 27, 2005 4:48 pm

After pulling my old books for DOS :? I could manage recreate the batch file, in fact now its even smaller and contains only two lines!!! :D

Just in case someone instersted here is what needs to be done:

Step 1
Create a new file ultraeditbackup.bat (name it as u wish, just make sure the extension is .bat) with this code inside:
Code: Select all
SET FLT=%time: =0%
COPY %1 "%1.%date:~10,4%-%date:~4,2%-%date:~7,2%_%FLT:~0,2%-%FLT:~3,2%-%FLT:~6,2%_%FLT:~9,2%.bak"

Save it somewhere on disk and remmember the path to it.

Step 2
- In UltraEdit go to: Advanced -> Tools Configuration
- In the "Command line" browse to your ultraeditbackup.bat file and append at the end of line %f so the "Command line" should looks like:
XX:/<path to your file>/ultraeditbackup.bat %f

- Enter "Menu Name" (enter any name u want)
- Click "Insert"
- If u have more then one tool, count on wich possition is this one in the list, u'll need it to setup "Hotkey"


That's about it.

P.S. whould be nice have some input if it works or not on non-english with none-american time format.
User avatar
V@no
Basic User
Basic User
 
Posts: 20
Joined: Thu Jul 22, 2004 11:00 pm

Re: Create a backup copy of files

Postby Manni » Mon Feb 28, 2005 3:06 am

V@no wrote:P.S. whould be nice have some input if it works or not on non-english with none-american time format.



Sure. First of all: This seems to be a very elegant solution.

Unfortunately, it doesn't work for me on W2k/German. It will turn c:\temp\backup.bat into
C:\temp\backup.bat.005-8.-2._ 8-44-47_61.bak


005 must be a reference to 2005. Is '8' all that's left of '28'? The worst thing is that is has a space before the time (which could easily be fixed by quoting the target file in the batch file).

I thought that my knowledge of batch files was pretty solid, but I've never seen what you are doing here. How exactly is this working?

Seems you pull substrings out of date and time. Hmm. %date% looks like this on my system: Mo 28.02.2005
%time% turns to 9:00:18,43.

Have you thought about simply using
copy %1 %1.%~t1
?


Manni
User avatar
Manni
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Jul 15, 2004 11:00 pm
Location: Europe

Re: Create a backup copy of files

Postby V@no » Mon Feb 28, 2005 3:47 am

Manni wrote:Unfortunately, it doesn't work for me on W2k/German. It will turn c:\temp\backup.bat into
C:\temp\backup.bat.005-8.-2._ 8-44-47_61.bak
1) Thank you for the input ;)
2) I just realised that it doesnt work from 1am to 9am because time format does not add "0" before hours (3:01:34)
So, because of that the code should be two lines long :)
Code: Select all
SET FLT=%time: =0%
COPY %1 "%1.%date:~10,4%-%date:~4,2%-%date:~7,2%_%FLT:~0,2%-%FLT:~3,2%-%FLT:~6,2%_%FLT:~9,2%.bak"

3) for every other date/time format u'll need:
- know exactly what is the format of date and time on your system.
u can find it out by opening DOS promt and type:
Code: Select all
echo %DATE%
echo %TIME%

- knowing the structure, u can easily correct the script for your format.
%date:~10,4%
the number 10 is offset from left to right and 4 is number of letter u want to "crop"


There is one thing though...if on your system hour, minutes or seconds not using zeros for number 1 to 9 (01, 02, 09) then this method wont work for u...
But anyway. if it still fails for u, please post the EXACT date/time string from your system.
User avatar
V@no
Basic User
Basic User
 
Posts: 20
Joined: Thu Jul 22, 2004 11:00 pm

Next

Return to Macros