Script: MultiFileOpen.cmd

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: Hacker, petermad, Stefan2, white

Post Reply
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Script: MultiFileOpen.cmd

Post by *Balderstrom »

I originally wrote this while trying to fix a discrepancy between how MetaPad would save to the proper folder when launched from the ButtonBar when the Cursor was on a Directory or [..] or a BinaryFile and NotePad2 would not. It's evolved a little since then.

I have since found that TC does not do proper quoting of passed parameters (arguments) when they don't contain spaces. It is especially bad when a filename contains an ampersand "&".

NOTE: Since cmd.exe is launched, the path is known, thus %P%S is not needed, only %S.

Code: Select all

::
::MultiFileOpen.cmd v2.01
::
::Overview: 	Launched via a Total Commander Button:
::					Command: cmd /c MultiFileOpen.cmd "C:\Path\to\Your\Program.exe"
::					Parameters: %S
::Currently:	Launched via AutoHotkey, from TC Button:
::					Command: %AHK%\TC_QuoteArgs.ahk "MultiFileOpen2.cmd" "%AppEditText%\NotePad2\notepad2.exe"
::					Parameters: %S
::
::Purpose(1)*	Dont pass %S Args to Program when Cursor is on [..] or Folder.
::
::Purpose(2)	When MultipleFiles are selected, Pass QUOTED "%S" arguments 1 at a time.
::
::Purpose(3)	If used with NotePad2,in your NotePad2.ini, underneath [Settings2]
::					add: DefaultDirectory=%EditorDefaultDIR%
::
::				If used with TedNotePad, in your TedNPad.ini, underneath [Setts]
::					add: DefPath=%EditorDefaultDIR%
::
::Purpose(4)	If cursor is on a Folder, Start an empty instance of Program from that DIR.
::
::
::	*Prevents the annoyance of things like NotePad2 showing the
::	 OpenFileDialog instead of just Starting (with no file).
::
::	History:
::		  Previous version 1.xx was launched via a TotalCommander Button, but I found that 
::		TC does not properly quote files. This is highly problematic when characters such 
::		as &!% are encountered in file names that don't have spaces. The ampersand, especially,
::		completely breaks most scripts when it is encountered and NOT quoted. 
::
::		Thus I have had to use AutoHotKey to properly quote the files before passing back 
::		into the MultiFileOpen.cmd script.
:: 
::
::	NOTES:
::		%AHK% and %AppEditText% are both SET in the RunTC.cmd I use to launch Total Commander.
::		--> SET AHKDir=%ProgramFiles%\AutoHotKey
:: 		--> SET AHK="%AHKDir%\AutoHotkey.exe" "%AHKDir%\Scripts"
::		
::		AppEditText has a convoluted SET, as it's path contains a non-standard character.
::		and CMD.exe doesn't like those characters in a batch file. Nor have I been able to 
::		get CMD.exe to launch/run a UTF or unicode .cmd file.
::
::

Code: Select all

@ECHO OFF
CLS
SET xProgram="%~1"
IF "%~2"=="" CALL:_OPEN_FROM_FOLDER "%CD%"&GOTO:END
:_MAIN_OLOOP
SHIFT
SET isDir=-%~a1-
SET isDir=%isDir:~1,1%
IF -%isDir%-==-d- CALL:_OPEN_FROM_FOLDER "%~1"&GOTO:_NEXT
START "" %xProgram% "%~1"
:_NEXT
IF "%~2" NEQ "" GOTO:_MAIN_OLOOP
GOTO:END

:_OPEN_FROM_FOLDER
PUSHD "%~1"
SET "EditorDefaultDIR=%CD%"
START "" %xProgram%
POPD
GOTO:EOF

:END
SET xProgram=
SET isDir=
Optional AHK PreLauncher:

Code: Select all

;;
;; TC_QuoteArgs.ahk
;;
	#SingleInstance, Force 
	#NoEnv
SetBatchLInes, -1

fileList := "", fileName := ""
Loop, %0%
{
	if( A_Index < 3 )
		continue
	fileName := %A_Index%
	fileList = %fileList% `"%fileName%`"
}
Run, %comspec% /s /c ""%1%" "%2%" %fileList%",, Min
return
History
2.01: Rewrite. Cleaner. Optional AHK preLauncher to fix Total Commander's broken argument quoting mechanism.

1.85: *Fixed broken variable expansion inside IF ( )
....... Fixed Notepad2.exe comparison by uppercasing filename.

(*) Knew variable expansion did not occur within FOR loops when ENABLEDELAYEDEXPANSION was off , but was unaware that also occured within any brace(d) code.

1.70: Automatic detection of NotePad2.exe; change to SETLOCAL
1.50: Apparently FART.exe is not completely unicode compliant, though
....... It is more functional than gsar. Rewrite of NotePad2's DefaultDir.


1.11: FIX: Quirk with :~0,1 path expansion, caused by v1.05.

1.10: FIX: Moved "::" Label comments outside of ( ) code blocks.
......: as noted here, IRT: "The system cannot find the drive specified."
Didn't prevent functionality, but did cause a system <beep> :-)

1.05: Removed ENABLEDELAYEDEXPANSION, and any conflict with Files named with "!".
OLD Version 1.85:Download MultiFileOpen v1.85
Last edited by Balderstrom on 2010-08-10, 08:32 UTC, edited 10 times in total.
User avatar
nsp
Power Member
Power Member
Posts: 1917
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Re: Script: MultiFileOpen.cmd

Post by *nsp »

Balderstrom wrote:I've noticed a number of Programs don't really understand how TC passes input to them for opening multiple files at once. And I imagine there is probably an AHK script or others around to handle this.
... ... ...
Packer, tcbl, batch script, ahk, .... a bunch of solution are possible for multiple file handling you can see in this forum, the wiki, or on plugin sites...

Even TC made some progress with %S and %L parameters ( now a bunche of application handle either multiple file on command line (like notepad2, pspad, notepad++,...) or filelist (like irfanview,...)

Comments:
Using %S for file selection is not the best way to handle file list (command line length is limited) and %L is more flexible IMO for this purpose (unicode filelist is even now possible with TC 7.5) for evey specific call (file placement) you will have to write a new script...

Notepad2 can be launched with multiple file from TC with a button using the folowing :

Code: Select all

command:notepad2.exe
parameters: /r + %P%S
TC will quote %P%S automatically..
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

Passing Notepad2 %P%S when the cursor is on a Folder or [..] causes it's OpenFileDialog, instead of just giving you an editor instance ready to edit. And if you cancel the OpenFile Dialog then NotePad2 wont remember where you opened it from. It will want to save the new file to either the last location it saved a file to, or the DefaultFolder you set in its ini.

I've clearly noted those issues in the post :P
And TotalCommander has a Bug with %P%S when the cursor is on [..] it doesn't encapsulate the a space-containing Path with quotes.

And theres hardly anything wrong with using %S, when the cmd script launches it knows what its current directory is via %CD%.
%CD% + the passed in %S = TC's %P%S.
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Re: Script: MultiFileOpen.cmd

Post by *Balderstrom »

nsp wrote: Comments:
Using %S for file selection is not the best way to handle file list (command line length is limited) and %L is more flexible IMO for this purpose (unicode filelist is even now possible with TC 7.5) for evey specific call (file placement) you will have to write a new script...
IF you are concerned about command-line length, then %S is overall better than %P%S, it doesn't hand off the whole DRIVE:\LONG\PATH\FILE.foo
It hands off solely the Directory or FileName the Cursor is on. Thus your CommandLength is Shorter with %S than %P%S.

And it doesn't need to be rewritten for every specific call, since it hands the program the PATH+FILE one at a time.

I'll just not post things next time :-)
User avatar
nsp
Power Member
Power Member
Posts: 1917
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Re: Script: MultiFileOpen.cmd

Post by *nsp »

%S is wrong if you use branch view or result from search (which i use all the time). About folder is it up to you to choose what you want to edit and [..] it is a small anoyance i've nevers seen.
Balderstrom wrote:I'll just not post things next time :-)
Next time do not read response... :twisted:
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

I used to primarily use MetaPad as my quick-n-dirty editor. But have been running into issues with its spotty unicode support of late.

MetaPad had some quirks I imagine that enabled it to open new instances (empty/new file) when you opened it with the cursor on [..] or a folder. It might complain about "access violation" about attempting to edit a folder, but you would still wind up with an empty file that would saveTo the folder you opened it in.
When on the [..] it (metapad) didn't complain at all.

On the other hand NotePad2 will not do this. Thus you need some kind of intermediary to launching it, or use 2 separate buttons. 1 to edit existing files, and one to launch an empty editor instance. Since in TC the cursor is *ALWAYS* on a folder or file or [..].

In both those cases, NotePad2 when handed %P%S (as I've mentioned) will give you the OpenFileDialog for that folder. and if you cancel the dialog to get to an emptyFile to edit when you try to save your new document it wont save where you opened it, it will go to DefaultFolder location or wherever windows last saved a file.

The script works flawlessly with cm_DirBranch
Tested with cm_Dirbranch opening 5+ files in 5 different subDirs.
Tested opening files that are 6+ directory levels deep from where DirBranch was invoked.
When save-as|save is invoked files will go to the proper location (where they were opened from).
Using chp.exe cmd.exe /c mentioned, will get rid of the brief CMD prompt popup display that using cmd.exe /c or cmdow.exe shows.

I primarily use AHK for my MX518 Mouse to further expand the functionality depending on which program I'm using. I've also found AHK to be nearly as tempermental as CMD batch scripts to getting them to the point where they actually work.
User avatar
nsp
Power Member
Power Member
Posts: 1917
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Post by *nsp »

Balderstrom wrote: The script works flawlessly with cm_DirBranch
Tested with cm_Dirbranch opening 5+ files in 5 different subDirs.
Tested opening files that are 6+ directory levels deep from where DirBranch was invoked.
When save-as|save is invoked files will go to the proper location (where they were opened from).
.....
Using chp.exe cmd.exe /c mentioned, will get rid of the brief CMD prompt popup display that using cmd.exe /c or cmdow.exe shows.
Your script works perfectly with dirbranch and feed to listbox .... i just not started in the same folder when i tried it ... :oops:
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

Various fixes. Should be compatible with any legal file/pathname now. a "&" quirk snuck in during 1.0 to 1.11. Also FART.exe doesn't seem to be completely unicode compliant, so went with an alternative for setting NotePad2's DefaultDirectory.
*BLINK* TC9 Added WM_COPYDATA and WM_USER queries for scripting.
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

Update to 1.70 Final.
Download removed, copy/paste the code if you have any use for it.
User avatar
Boofo
Power Member
Power Member
Posts: 1431
Joined: 2003-02-11, 00:29 UTC
Location: Des Moines, IA (USA)
Contact:

Post by *Boofo »

2Balderstrom

Why did you remove it? It would be nice to see this for EditPlus, too.
chmod a+x /bin/laden -- Allows anyone the permission to execute /bin/laden

How do I un-overwrite all my data?

User of Total Commander
#60471 Single user license
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

My login for Hotmail where I was hosting the download on SkyDrive isn't working currently. And the download was just a file called MultiFileOpen.cmd with the contents of the First "Quote" and the "Code" Body, which are both right there in the post...

This should work for any program, initially I referenced "Editors" in the code, as thats all I used it for initially. Really it could handle anything, even something that needs parameters, just include parameters after the .exe and have it all contained in one quoted string, i.e.
"C:\Some\Program.exe x f"

And if EditPlus has a setting to only allow one instance, then sending it files one at a time, should just open up more tabs. And this would work with no change at all.
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

v1.85
*Fixed broken variable expansion inside IF %OpenedAtLeastOne%==0 (
Uppercased Notepad2.exe for == comparison.Filename check would fail if it didn't match case exactly.

(*) Knew variable expansion did not occur within FOR loops when ENABLEDELAYEDEXPANSION was off , but was unaware that also occured within any brace(d) code.

Download replaced.
*BLINK* TC9 Added WM_COPYDATA and WM_USER queries for scripting.
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

Updated to Version 2.01, Download only for old version, ATM.
*BLINK* TC9 Added WM_COPYDATA and WM_USER queries for scripting.
Post Reply