User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and playing macros
19 posts Page 1 of 2
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 extremely helpful, 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!
Hi V@no,

I have a macro that does the job for me (similarly).
IT HAS TO HAVE the property Continue if search string not found enabled.

Macro: savecopy
Don't 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
Macro code removed, see below for a better one.

No guarantee! Enjoy.
Bego
Hi Bego,

I don't have tried, but following should do the job.

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

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
Never forget: "Above the clouds, The sky is blue and the sun shine"
Hi Palou

I tried out your suggestions and they work well :D

So here for everybody the new macro.

Code: Select all
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

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

Thanks, Bego :roll:
Hi guys, thank you very much!
There are few issues though:

  1. Neither 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 -> selected "backup" macro, in the left part I pasted the code -> hit modify -> unchecked all check boxes -> set the hotkey -> hit "ok" -> on the question "update macro" clicked "yes" -> close macro window.)
    There couldn't 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 convinced on large files.
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 annoying 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
Never forget: "Above the clouds, The sky is blue and the sun shine"
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 local file names, not FTP.

Also your clipboard content will be maintained now.

The line 1 problem I will look later.

So far, Bego :wink:

Code: Select all
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
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 modification).
  • 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
Never forget: "Above the clouds, The sky is blue and the sun shine"
Ops, I was writing 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
Never forget: "Above the clouds, The sky is blue and the sun shine"
Hi Palou and all guys !

You're macro 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

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 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
Thanks guys, gonna test the code in a minute.
But still I found the Windows command line batch version is much easier, faster and more convinced 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 you up, if you did some changes in the file and then you remembered that you forgot to make a backup of the original code, so you do "undo" until you reach the code you wanted to make backup of, but after the macro you 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 opinion is so dumb, as dumb as their distance and weight measurements, no wonder in school they must have calculators :roll: . no offence though)
I'm pretty certain there is a way instead of just replace all slashes and columns with the underline, shuffle 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!
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, too.
--> Backups surely can be done by outside-os-batches much easier.

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

Bego
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 interested here is what needs to be done:

Step 1
Create a new file ultraeditbackup.bat (name it as you 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 remember 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 look like: X:\<path to your file>\ultraeditbackup.bat "%f"
  • Enter "Menu Name" (enter any name you want).
  • Click "Insert".
  • If you have more then one tool, count on which position is this one in the list, you'll need it to setup "Hotkey".
That's about it.

P.S. Would be nice have some input if it works or not on non English with non American time format.
V@no wrote: P.S. Would be nice have some input if it works or not on non English with non 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. Hm. %date% looks like this on my system: Mo 28.02.2005
%time% turns to 9:00:18,43.

Have you thought about simply using

Code: Select all
copy %1 "%~1.%~t1"

Manni
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 realized that it doesn't work from 1 am to 9 am 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 you'll need:

    • Know exactly what is the format of date and time on your system.
      You can find it out by opening DOS prompt and type:

      Code: Select all
      echo %DATE%
      echo %TIME%

    • Knowing the structure, you 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 you 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 won't work for you.

But anyway, if it still fails for you, please post the EXACT date/time string from your system.
19 posts Page 1 of 2