The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Passing Multiple Files to a Script

I want to make a simple script to rename multiple files that have been selected in Explorer.  Select some files, right click, choose "Rename Files...", an inputbox pops up, you enter a specific name or pattern, and the script renames the files.  I'm having trouble getting windows to pass more than one file name to the script.  In the registry for context menus I have:

wscript "myscript.vbs" "%1" "%2".."%9"

This is obviously doable, since you can select multiple files and print.  There are many apps that operate on multiple files and run from the context menu.  Is there a way to do this without compiling something?  Why's it different for scripts?
Stephen Sorrell Send private email
Tuesday, July 25, 2006
 
 
I've never dealt with this in Windows, but this is actually a common question in UNIX shell scripting. Hopefully what I'm about to say will give you some insights.

A significant problem in this scenario is that you have a finite number of arguments - if I select 10 files to be renamed, do you have a "%10" parameter to accept the tenth? What happens if I select 3,427 files?

The short answer is to accept one argument which is a space-separated list of files to be renamed and then allow the script itself to loop through the list.

From the way you worded the question (Windows can only accept one argument), it sounds like this was the original intention all along.

I would recommend looking at split() or shift() style functions to see if there's a way to extract one filename at a time from the argument-string.
TheDavid
Tuesday, July 25, 2006
 
 
I had assumed it would give all the files in a long space or comma delimited list.  It seems only to give the first file in the list.  I'm really trying to find out how Windows passes along the other filenames.  %2 and the others are empty.

Perhaps I need a compiled program that does one thing.  Get me the list of files Windows is passing to it.  This is a very frustrating issue, because it seems to be undocumented.
Stephen Sorrell Send private email
Tuesday, July 25, 2006
 
 
I hate to say this, but I think the shell actually uses DDE to handle multiple files. There's not much of a chance that scripting languages can handle that.
Chris Tavares Send private email
Tuesday, July 25, 2006
 
 
Stupid question, but is it possible there's a flag or a switch that basically tells Windows to interpret everything following as a single argument?

For example, in the UNIX world, we'd frame the argument with a pair of quotes. Some of the more robust shells allow you to replace those quotes with a flag (initially so that you could use quotes as part of the argument itself).
TheDavid
Tuesday, July 25, 2006
 
 
Stephen, the online docs for wscript give expamples of what you need.

http://msdn2.microsoft.com/en-us/library/z2b05k8s.aspx

The Arguments property contains the WshArguments object (a collection of arguments). Use a zero-based index to retrieve individual arguments from this collection.

VB:
Set objArgs = WScript.Arguments

For I = 0 to objArgs.Count - 1

  WScript.Echo objArgs(I)

Next

JScript:
objArgs = WScript.Arguments;

for (i = 0; i < objArgs.length; i++)

{

  WScript.Echo(objArgs(i));

}

Let me know if this works for you to get all the file names.
Flow
Saturday, July 29, 2006
 
 
Thanks, but this is what I'm using now.  Only the 0th and 1th element contain anything.  I could call it with myscript.wsf "file1", "file2",.. but that's from the command line.  My intention is to select files in the UI, right click, and select myscript.  Explorer seems to pass only the first filename.

The DDE comment is very interesting.  I never considered that or know much about it.  Thanks.
Stephen Sorrell Send private email
Monday, July 31, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz