Jump to content
Macro Express Forums
mark99k

Close current file, open next in folder (not iterating)

Recommended Posts

Anyone have a way in ME to save & close the active file (a PDF) and automatically open the alphabetically next one in its folder?  I thought Repeat With Folder might be useful but on closer inspection it doesn't look it.  I don't want to iterate through, as each file needs nonrepetitive tasks done to it after opening. I'd just like to be able hit a key when finished to save & close it and open the next one.

Had this same challenge with a huge pile of Word docs recently and a VBA macro to go get the next one saved my sanity. Now with PDFs it's less rosy.

Does ME do any simple file I/O?  If it does, I could collect the PDF names in a text file for ME to read. 

Thanks in advance for any clues.

Share this post


Link to post
Share on other sites
4 hours ago, mark99k said:

Anyone have a way in ME to save & close the active file (a PDF) and automatically open the alphabetically next one in its folder?  I thought Repeat With Folder might be useful but on closer inspection it doesn't look it.  I don't want to iterate through, as each file needs nonrepetitive tasks done to it after opening. I'd just like to be able hit a key when finished to save & close it and open the next one.

Had this same challenge with a huge pile of Word docs recently and a VBA macro to go get the next one saved my sanity. Now with PDFs it's less rosy.

Does ME do any simple file I/O?  If it does, I could collect the PDF names in a text file for ME to read. 

Thanks in advance for any clues.

How about this?

Repeat with Folder YourFolderName (Return Files only)
  Wait for Right Mouse Click
End Repeat

This returns filenames in alphabetical order (as long as the file system is NTFS - order is random under FAT).
In this instance the script waits for a right-click before continuing - you could have it wait for text, or for a window to lose focus, etc.

Share this post


Link to post
Share on other sites

Hi Paul. Yeah, that's pretty much what I tried at first, but (of course) ME doesn't release control after the file is opened. (I should've clarified: the tasks each file needs done to it while open are various unpredictable edits and other non-ME-able things.)  So as I see it, instead of looping, the macro needs to begin executing when I've finished with each file, and terminate when the next file is open.

I was hoping to do something like store an array of the filenames and have the macro (a) remember which file is open, (b) figure out from the array which file is next, and then (c) save+close the former and open the latter. But I seem to be out of my depth with loading & reading arrays in ME. Does this make any sense?

Share this post


Link to post
Share on other sites
7 hours ago, mark99k said:

Hi Paul. Yeah, that's pretty much what I tried at first, but (of course) ME doesn't release control after the file is opened. (I should've clarified: the tasks each file needs done to it while open are various unpredictable edits and other non-ME-able things.)

I don't understand exactly what you mean. While ME is waiting for you to right-click (in my example) in order to continue on to the next file, are you not able to do any processing or editing of the opened file? Or does ME lock it?

Share this post


Link to post
Share on other sites

That's correct. The Acrobat window is viewable but doesn't respond to anything except the wait-for key -- in my case F2, since there's lots of right-clicking required in each file's editing session. (Also, those sessions can last hours and may include closing the file to do other things, so my goal isn't to leave ME suspended in a waiting state, just to have it get the next file when told.)

Share this post


Link to post
Share on other sites

Here is an idea about how to tackle it. You will need two macros.

Macro 1 - gets the list of files and stores them in a file. Run this only once to set up. Something like this. (You will need to change the paths):

// Delete registry counter and files to set things up to process files
Delete Registry Value: HKEY_CURRENT_USER\Software\Insight Software Solutions\Macro Express 4\$Temp\NextFileNumber // Delete the file number, if it exists
Delete File/Files: "e:\FilesToProcess.txt" // Delete output file, if it exists
 
// Get a list of the files to process
Repeat with Folder e:\temp
  Variable Modify String: Append %File% to text file, "e:\FilesToProcess.txt"
End Repeat

Macro 2 - opens the next file from the list. Run each time you are finished manually processing one file. (Again, you will need to change the paths):

// Use a hotkey activation for this macro
 
// Get the NextFileNumber from the registry
Variable Set Integer %NextFileNumber% to 1 // Default in case the registry value does not exist
Read Registry Value "HKEY_CURRENT_USER\Software\Insight Software Solutions\Macro Express 4\$Temp\NextFileNumber" into %NextFileNumber% // Get number of file to use
 
// Get the name of the next file in the list
Text File Begin Process: e:\FilesToProcess.txt
  Break // Only get one file
Text File End Process
 
// If NextFilename is blank then all files have been processed
If Variable %NextFilename% Equals ""
  Text Box Display: Done
  Macro Stop
End If
 
// Increment and save the NextFileNumber to the registry
Variable Modify Integer %NextFileNumber%: Increment
Write Registry Value "%NextFileNumber%" into HKEY_CURRENT_USER\Software\Insight Software Solutions\Macro Express 4\$Temp\NextFileNumber // Get number of file to use
 
 
Variable Set From File path // Get file extension
 
Text Box Display: Opening file
If Variable %Ext% Equals ".pdf" // Process pdf files
  OR
If Variable %Ext% Equals ".txt" // or txt files
  OR
If Variable %Ext% Equals ".doc" // or doc files
  OR
If Variable %Ext% Equals ".docx" // or docx files
  OR
If Variable %Ext% Equals ".jpg" // or jpg files
  OR
If Variable %Ext% Equals ".png" // or png files
  OR
If Variable %Ext% Equals ".ini" // or ini files
  Change Directory/Folder to "e:\temp"
  Program Launch: "%NextFilename%" (Normal)
Parameters: 
Else
  Delay: 1 seconds
End If

Here is code you can copy and paste for the "Get filenames" macro:

<COMMENT Value="----------------------------------------------------------------------------------------------------------------------------------"/>
<COMMENT Value="  Get filenames -"/>
<COMMENT Value="----------------------------------------------------------------------------------------------------------------------------------"/>
<COMMENT/>
<COMMENT Value="Delete registry counter and files to set things up to process files"/>
<DELETE REGISTRY VALUE Value="HKEY_CURRENT_USER\\Software\\Insight Software Solutions\\Macro Express 4\\$Temp\\NextFileNumber" _IGNORE="0x0025" _COMMENT="Delete the file number, if it exists"/>
<DELETE FILE/FILES Path="e:\\FilesToProcess.txt" Progress="FALSE" Recurse="FALSE" Permanent="FALSE" _COMMENT="Delete output file, if it exists"/>
<COMMENT/>
<COMMENT Value="Get a list of the files to process"/>
<REPEAT WITH FOLDER Path="e:\\temp" OnlyFiles="TRUE" Destination="%File%" FullPath="FALSE" ProcSubfolders="FALSE"/>
<VARIABLE MODIFY STRING Option="\x12" Destination="%File%" Filename="e:\\FilesToProcess.txt" Strip="TRUE" NoEmbeddedVars="FALSE"/>
<END REPEAT/>

Here is code you can copy and past for the "Process one file" macro:

<COMMENT Value="----------------------------------------------------------------------------------------------------------------------------------"/>
<COMMENT Value="Process one file - "/>
<COMMENT Value="----------------------------------------------------------------------------------------------------------------------------------"/>
<COMMENT Value="Use a hotkey activation for this macro"/>
<COMMENT/>
<COMMENT Value="Get the NextFileNumber from the registry"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%NextFileNumber%" Value="1" _COMMENT="Default in case the registry value does not exist"/>
<READ REGISTRY VALUE Key="HKEY_CURRENT_USER\\Software\\Insight Software Solutions\\Macro Express 4\\$Temp\\NextFileNumber" Destination="%NextFileNumber%" _COMMENT="Get number of file to use"/>
<COMMENT/>
<COMMENT Value="Get the name of the next file in the list"/>
<TEXT FILE BEGIN PROCESS Filename="e:\\FilesToProcess.txt" Start_Record="%NextFileNumber%" Process_All="TRUE" Records="1" Variable="%NextFilename%"/>
<BREAK _COMMENT="Only get one file"/>
<TEXT FILE END PROCESS/>
<COMMENT/>
<COMMENT Value="If NextFilename is blank then all files have been processed"/>
<IF VARIABLE Variable="%NextFilename%" Condition="\x00" IgnoreCase="FALSE"/>
<TEXT BOX DISPLAY Title="Done" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n{\\colortbl ;\\red0\\green0\\blue255;}\r\n\\viewkind4\\uc1\\pard\\qc\\cf1\\f0\\fs24 All files have been processed.\\cf0\\f1\\fs20 \r\n\\par }\r\n" Left="Center" Top="Center" Width="337" Height="125" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Increment and save the NextFileNumber to the registry"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%NextFileNumber%"/>
<WRITE REGISTRY VALUE Key="HKEY_CURRENT_USER\\Software\\Insight Software Solutions\\Macro Express 4\\$Temp\\NextFileNumber" Destination="%NextFileNumber%" _COMMENT="Get number of file to use"/>
<COMMENT/>
<COMMENT/>
<VARIABLE SET FROM FILE Filename="e:\\temp\\%NextFilename%" Option="\x01" Extension="%Ext%" Expand="FALSE" Flags="\x08" _COMMENT="Get file extension"/>
<COMMENT/>
<TEXT BOX DISPLAY Title="Opening file" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs20 Opening File %NextFilename%\r\n\\par Extension: %Ext%\\f1 \r\n\\par }\r\n" Left="Center" Top="Top" Width="574" Height="97" Monitor="0" OnTop="TRUE" Keep_Focus="FALSE" Mode="\x01" Delay="0"/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".pdf" IgnoreCase="TRUE" _COMMENT="Process pdf files"/>
<OR/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".txt" IgnoreCase="TRUE" _COMMENT="or txt files"/>
<OR/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".doc" IgnoreCase="TRUE" _COMMENT="or doc files"/>
<OR/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".docx" IgnoreCase="TRUE" _COMMENT="or docx files"/>
<OR/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".jpg" IgnoreCase="TRUE" _COMMENT="or jpg files"/>
<OR/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".png" IgnoreCase="TRUE" _COMMENT="or png files"/>
<OR/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".ini" IgnoreCase="TRUE" _COMMENT="or ini files"/>
<CHANGE DIRECTORY/FOLDER Path="e:\\temp"/>
<PROGRAM LAUNCH Path="%NextFilename%" Mode="\x00" Default_Path="TRUE" Wait="1" Get_Console="FALSE"/>
<ELSE/>
<DELAY Flags="\x01" Time="1"/>
<END IF/>

 

Share this post


Link to post
Share on other sites

I didn't review Samrae's post in detail and I don't have a lot of time. But keeping lists sounds like trouble as file change. Here's what I would do. Use repeat with folder to find the next file. Given that you know what file you have open, repeat with file until the currently open file is matched by name and go one more. That's the next file.

Share this post


Link to post
Share on other sites

Hi all, and thanks for the help. I did finally manage to make this work, but I suspect my method may be amateurish. Any suggestions appreciated. I agree with Cory that fixed lists are usually unwise, but in this case the filenames are very static and the list is already generated (see line 2), so that's what I started with. FYI the Text File processing line assigns each filename (not its path) to %FI%.

The only thing I'd like to get rid of is the hardcoding of the folder. Thought I'd be able to extract that from the file's full path but I didn't see equivalents to INSTR or MID$ anywhere. Am I looking in the wrong place? Thanks again.

1: Change Directory/Folder to "C:\5"
2: Text File Begin Process: Z:\MyFiles.txt 
3:    If Variable %FoundIt% Equals "True"
4:	     Launch Program and Activate Window: Program "Acrobat.exe", Parameters "%FI%", Window "Acrobat Pro" 
5:	     Goto:Done
6:    End If
7:    If Window "%FI%" is running
8:	     Variable Set Bool %FoundIt% to "True"
9:	     Text Type (Simulate Keystrokes): <ALT>fs 
10:      Delay: 200 milliseconds
11:      Window Close: Acrobat Pro 
12:   End If
13: Text File End Process
14: :Done

 

 

 

Share this post


Link to post
Share on other sites

I did, and it seemed promising for a moment. But there seems to be no way to detect the path of the open document. (Acrobat doesn't put the path in the title bar.) Am I missing something?

Share this post


Link to post
Share on other sites

Not (that I can see) in a way that's appreciably quicker than editing the code. It's fine -- these are often folders containing dozens, sometimes hundreds of files, so an extra 10 seconds at the beginning won't kill me.

Share this post


Link to post
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

×