Process and rename all the files with a specific extension

A current use of WinTask is to process all the files with a specific extension within one directory, and then to save the new files under another extension. The script below lists all the files with a .DOC extension in a directory, loads each of them in Worpad, makes some changes in each of them, and saves under the same filename but with the new extension .RTF.

You can find the corresponding script file under Script_Examples, sub-directory of scripts

 

'Declaration of the arrays needed by DirTree function for retrieving all the files

'with extension .DOC in the specified directory

dim longname$(500)

dim shortname$(500)

dim pathname$(500)

dim att$(500)

 

'path where your files are

DIREC$="C:\docs\"

 

'In this example, we use wordpad to process each individual file with extension .DOC

'It can be any application, for instance Photoshop.

shell("wordpad")

 

'nb gives the number of *.doc files present in DIREC$

nb=dirtree(direc$+"*.doc",longname$(),shortname$(),pathname$(),att$(),"")

 

if nb=0 then msgbox("no file to process !") end endif

 

'now the loop which makes the repetitive actions for each file

i=0

repeat

'In this example, the tasks to process on each file are
'Open the file, then format it, then save under rtf extension
UseWindow("WORDPAD.EXE|WordPadClass|Document - WordPad",1)

ChooseMenu(Normal,"&File|&Open... Ctrl+O")

 

UseWindow("WORDPAD.EXE|#32770|Open",1)

WriteEdit("1",direc$+longname$(i))

Click(Button,"&Open")

 

pause 2

'here what you want to do within the opened file.

'Use Recording mode to generate automatically the actions in your script

UseWindow("WORDPAD.EXE|WordPadClass|",1)

ChooseMenu(Normal,"&Edit|Select A&ll Ctrl+A")

 

UseWindow("WORDPAD.EXE|AfxControlBar42|Standard",1)

ChooseItem(ToolBar, "Formatting", "|32799", single, left )

 

UseWindow("WORDPAD.EXE|WordPadClass|",1)

ChooseMenu(Normal,"&File|Save &As...")

 

'here we change the name of the current file in .RTF

'We extract the filename without the extension in leftpart$
'including the ., and in WriteEdit function where we save under the new filename,
'we concatenate with rtf extension.
pos=instr(longname$(i),".")

leftpart$=left$(longname$(i),pos)

UseWindow("WORDPAD.EXE|#32770|Save As",1)

WriteEdit("1",leftpart$+"rtf")

Click(Button,"&Save")

 

pause 1

i=i+1

until i=nb

 

'at the end, close the application

KillApp("wordpad.exe",1)