Run Macro on all files within folder

Help with writing and playing macros

Run Macro on all files within folder

Postby phelan » Wed Aug 22, 2007 11:44 pm

I have found a couple of threads on this topic but I have been unable to really modify them to what i need.


I have a macro that i use to clean database files prior to import into SQL.

Untill now i have just loaded the files and run the Macro on "ALL FILES"

however we are now dealing with a few hundred files at a time.

IS there a way to make UE run our macro on all files within a folder?

i would prefer not having to create a UE project to do this.

i was thinking a command line batch file may work.

open UE
run XXX.mac on files *.*

But i have read that this is not posible due to auto macro and wildcards?

i am running UE 11.10a+1

Thanks in advance for any help


ps.

if i do have all the files listed in a project, how do i get a macro to run on entire project?......i think i sorted this part :D

Replace in files and select "Project Files" in "Select Where" option.
User avatar
phelan
Newbie
 
Posts: 9
Joined: Mon Jul 02, 2007 11:00 pm

Re: Run Macro on all files within folder

Postby phelan » Thu Aug 23, 2007 12:07 am

Ok after a bit more reading I have found the Replace in files function quite usefull.

I have used it to create a basic macro that I will now edit with our needed functions, I just needed the initial command to enable the all files and directory name.

How many files can be edited this way in one go.

If I specify to edit *.txt in a certain directory... will it have issues if there are 300 .txt files in there?

Is file size a concern? I have edited single large files before but never in batches like this.


UPDATE:

I have managed to get this working for plain text or regular exp.

Can I do this for HEX.

I have tried using the same macro but using HexOn instead of HexOff, but the expression is not found. If I actually load 1 of the files and run the replace function manually it works well with hex mode.

Can Hex be used with Replace in Files???
User avatar
phelan
Newbie
 
Posts: 9
Joined: Mon Jul 02, 2007 11:00 pm

Re: Run Macro on all files within folder

Postby Mofi » Thu Aug 23, 2007 6:52 am

Replace In Files has no limits. You can run it on thousands of files each with several MBs or GBs.

Replace In Files does not support hexadecimal replaces except you use the Perl regular expression engine and appropriate search and replace string in hexadecimal notation. However, you can replace all bytes except a NULL byte (00) with Replace In Files too. You just have to use the ASCII/ANSI characters for the bytes you want to replace.


Here is a small macro which is able to run a macro on all files specified by you on startup.

It works only for non FTP files. (To be more precise, I have not tested it with FTP files.)

It works only for UltraEdit v10.20 or later and all versions of UEStudio. Without the first line it should work also for UltraEdit v9.10 and later, but then you have to run the Find In Files with an empty search string and with option Results to Edit Window checked manually.

If you use an UltraEdit version prior v12.00 or an UEStudio version prior v5.50 delete the command UnicodeToASCII from the macro code because those versions already create the Find In Files results window as ASCII file and not as Unicode file.

The conversion from Unicode to ASCII means also that the full file names of the project files cannot contain Unicode characters. I have not tested if this macro would also work without conversion from Unicode to ASCII.


Further the macro identifies the results window by the string "Search complete, found". This string exists at bottom of the results window only for English version of UltraEdit and only when Find Summary is still enabled and contains the default string at Advanced - Configuration - Search - Set Find Output Format depending on version of UltraEdit. If a localized version of UltraEdit is used, this search string must be modified to the localized string. With an unchecked Find Summary configuration setting the macro will not work because not able to identify the document window with the results.


The name of the called macro as written in the string "Name of your macro" is case sensitive. So make sure that you enter the macro name correct. Also this macro and your called macro must exist in the same macro file. It is not possible to call a macro stored in another macro file. And it is important to first create the called macro and then create this macro or UltraEdit would remove the line with the PlayMacro on close (compilation) because of the missing macro.


The macro property Continue if a Find with Replace not found or Continue if search string not found must be checked for this macro.

FindInFiles "directory" "file type" ""
Loop
Find MatchCase Up "Search complete, found"
IfFound
ExitLoop
Else
NextWindow
EndIf
EndLoop
DeleteLine
SelectToBottom
IfSel
Delete
EndIf
Top
UnicodeToASCII
Loop
IfEof
ExitLoop
EndIf
StartSelect
Key END
IfSel
Open "^s"
PlayMacro 1 "Name of your macro"
CloseFile Save
EndIf
EndSelect
DeleteLine
EndLoop
CloseFile NoSave
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Run Macro on all files within folder

Postby phelan » Sun Aug 26, 2007 8:59 pm

Thanks Mofi!

thats great :D

A few issues.

when referencing the Macro1, does it need to be in the same macro file as the source macro, or do i put the complete file path to the macro?

if i remove the "filetype" prompt at the start will it still work on all files?? most of the files we need this for don't actually have extensions.

so far i can get it to run and display "Search complete, found '' 0 time(s). (0 files.)" on the screen so i think it is having trouble finding the macro to run or trouble finding the file to search

i made very simple test macro which is just "find "PRD""

I also have a few test files with both *.txt extension and no extension. All files contain PRD in them.


Thanks again for the help, once i get it going, you'll have saved me hours and hours of tedium with this.
User avatar
phelan
Newbie
 
Posts: 9
Joined: Mon Jul 02, 2007 11:00 pm

Re: Run Macro on all files within folder

Postby Mofi » Mon Aug 27, 2007 6:09 am

The macro you play with command PlayMacro must be in the same macro file and the name is case-sensitive. UltraEdit removes automatically the PlayMacro line without a warning if you close the edit macro dialog and the macro with its case-sensitive name does not already exist in the current macro file.

The file type specification is nothing special. If you want all files in the directory found, specify "*".

Note: The directory specification must end with a backslash.

Do not specify any search string for the FindInFiles command. It is required for this macro that the search string exists, but is empty. Only with this empty search string UltraEdit returns just a list of found files with full file name (path and file name). The result is than a simple file list like using DOS command dir /A-D /B.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Run Macro on all files within folder

Postby phelan » Mon Aug 27, 2007 11:04 pm

OK, thanks for that Mofi.

Have done some testing,

I am now getting the file list showing up and all files are displayed and listed. I've got a collection of *.txt files with missing the extension.

What happens next is that the macro running/cancel box is displayed and the cursor blinks rapidly. The called macro doesn't appear to run as no changes are made to the files

Obviously I have an error somewhere in the 2nd half of the macro :?

both macros are saved in the same macro file

Called Macro (step2)

Code: Select all
InsertMode
ColumnModeOff
HexOff
UnixReOff
Find RegExp "PRD"
Replace All "XXX"
SaveAll


Main Macro (step1)

Code: Select all
FindInFiles "D:\Users\macro test\" "*" ""
Loop
Find MatchCase Up "Search complete, found"
IfFound
ExitLoop
Else
NextWindow
EndIf
EndLoop
DeleteLine
Top
Loop
IfEof
ExitLoop
EndIf
StartSelect
Key END
IfSel
Open "^s"
PlayMacro 1 "step2"
CloseFile Save
EndIf
EndSelect
DeleteLine
EndLoop
CloseFile NoSave
User avatar
phelan
Newbie
 
Posts: 9
Joined: Mon Jul 02, 2007 11:00 pm

Re: Run Macro on all files within folder

Postby Mofi » Tue Aug 28, 2007 6:44 am

Showing the Cancel dialog is controlled by the macro property Show Cancel dialog for this macro of the main macro. Uncheck this option at Macro - Delete Macro/Modify Properties for the main macro.

Do not use the SaveAll command. It saves all files. The temporary file with the list of files was not saved before and so has no file name. It should not be saved because it is not needed anymore after macro execution and will be empty after macro execution. The main macro contains already CloseFile Save which saves the current file and closes it. So your submacro has nothing to do except the changes you want to do, but no file save and no file close.

Your search and replace is a simple one. There is no need to use the regular expression option.

I hope, your real submacro does more than just replace all "PRD" by "XXX" in the current file. This could be also done without any macro by using the command Search - Replace In Files.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Run Macro on all files within folder

Postby krthk.s » Wed Jan 23, 2008 6:15 pm

I want to replace the hard coded directory in the FindInFiles command with a dynamic value.

FindInFiles "D:\Users\macro test\" "*" ""

So I tried using the following but neither worked.

1. FindInFiles "^c" "*" ""
In this case the directory path was copied to the clipboard before executing the macro

2. FindInFiles "^s" "*" ""
In this case the directory path was selected text in the current file

Can anyone help?

Thanks
Krthk
User avatar
krthk.s
Newbie
 
Posts: 1
Joined: Wed Jan 23, 2008 12:00 am

Re: Run Macro on all files within folder

Postby Mofi » Thu Jan 24, 2008 8:11 am

^c and ^s can be used only for the search string of the FindInFiles macro command. The initial directory and the file type parameters cannot be made variable. Solution is to use a script where variables are in general possible - see for example Function to get list of files into an edit window. But that requires UltraEdit v13.20 or later.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Run Macro on all files within folder

Postby neill80 » Wed Jul 09, 2008 8:50 am

Hi all,

I'm using currently Ultraedit 12.20b+1. How do I get this to work on the whole folder?

I am trying to run a macro on around 100 files to tidy them up with the following macro..

InsertMode
ColumnModeOff
HexOff
UnixReOff
Find "<!-- Begin Document Specific Declarations -->"
Replace ""
Find "<!-- End Document Specific Declarations -->"
Replace ""
Find RegExp "Multiple
procedures"
Replace All "Multiple procedures"
Find RegExp "single
procedure"
Replace All "single procedure"
Find RegExp "<?MEKON*>"
Replace All ""
Find "><"
Replace All ">^p<"
Loop 10
Find "^p^p"
Replace All "^p"
EndLoop
Loop 10
Find " "
Replace All " "
EndLoop
Find "^p</XREF>"
Replace All "</XREF>"
Find "^p</ENTRY>"
Replace All "</ENTRY>"

Do I add the command to the macro and do I have to run it on command line and if so how??

FindInFiles "D:\Users\macro test\" "*" ""

or

RepInFiles "D:\Users\macro test\" "*" ""

Many thanks for your time.

Regards

Neil
User avatar
neill80
Newbie
 
Posts: 4
Joined: Tue Feb 27, 2007 12:00 am

Re: Run Macro on all files within folder

Postby Mofi » Thu Jul 10, 2008 3:23 am

First, I don't understand why you not run these 10 replaces manually with Replace In Files. Do you need it frequently?

Second, it would be much faster to use the command ReplInFiles in your macro for that job instead of opening one file after the other and run your macro on every file. Here is the modified version of your macro.

InsertMode
ColumnModeOff
HexOff
UnixReOff
ReplInFiles "directory" "file type" "<!-- Begin Document Specific Declarations -->"
""
ReplInFiles "directory" "file type" "<!-- End Document Specific Declarations -->"
""
ReplInFiles "directory" "file type" "Multiple^pprocedures"
"Multiple procedures"
ReplInFiles "directory" "file type" "single^pprocedure"
"single procedure"
ReplInFiles RegExp "directory" "file type" "<?MEKON*>"
""
ReplInFiles "directory" "file type" "><"
">^p<"
ReplInFiles RegExp "directory" "file type" "^p[^r^n]+"
"^p"
ReplInFiles RegExp "directory" "file type" "  +"
" "
ReplInFiles "directory" "file type" "^p</XREF>"
"</XREF>"
ReplInFiles "directory" "file type" "^p</ENTRY>"
"</ENTRY>"

directory is for example C:\Temp\ and file type is for example *.xml

Last, what is your problem with the main macro I have posted? If you have loaded the macro file with your macro, just create a new empty macro, paste my macro code into this new macro (replacing the 4 standard lines), edit the 3 red marked strings to your requirements, save the macro and run it once. A macro file can store more than 1 macro!
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Run Macro on all files within folder

Postby neill80 » Thu Jul 10, 2008 7:51 am

Hi Mofi,

Thanks for your response, I use this macro to tidy up some sgml data on a regular basis.

Nothing wrong with your macro, I just dont understand that language...

2 hours later: Just used your macro, Many Thanks, saved me lots of time.

Kind Regards

Neil
User avatar
neill80
Newbie
 
Posts: 4
Joined: Tue Feb 27, 2007 12:00 am

Re: Run Macro on all files within folder

Postby ftpaccess » Fri Apr 30, 2010 12:44 am

This thread has become a bit old. I read through the posts here and applied the same but it just didn't work for me. I have multiple folders inside a folder. How can i run my macro on all files in those folders since there are a few thousand of them. I have UE version 16.
ftpaccess
Newbie
 
Posts: 7
Joined: Fri Apr 30, 2010 12:38 am

Re: Run Macro on all files within folder

Postby Mofi » Fri Apr 30, 2010 12:52 am

The macro template in my (Mofi) first post should also work with UE v16.00.0.1040. You just have to use in the first line

FindInFiles Recursive "directory" "file type" ""
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Run Macro on all files within folder

Postby ftpaccess » Fri Apr 30, 2010 1:09 am

Thanks Mofi ...it worked!!!
ftpaccess
Newbie
 
Posts: 7
Joined: Fri Apr 30, 2010 12:38 am


Return to Macros