PC Review


Reply
Thread Tools Rate Thread

Batch files from any file name?

 
 
David Trimboli
Guest
Posts: n/a
 
      27th Jan 2007
This may seem an odd question.

Is there any way to run a batch script from anything other than a .bat
or .cmd file?

In Unix, I can create a batch file of any name, then run, for instance,
sh <filename>
to execute the batch.

In cmd.exe, the same doesn't work.

I'm not trying to do anything in particular; I was just wondering if
there is a way to do this that I hadn't thought of.

The reason this occurs to me is that I like to mimic Unix's habit of
using .* files for configuration, so my cmd autorun file is ".cmd", my
mail signature is ".signature", my macro file is ".doskey", and so on.
However, since I prefer not to view file extensions for known file
types, ".cmd" is displayed in Windows Explorer as a file without a name,
but which runs as a batch file. Thus, I wondered if there was a way to
create a ".autorun" file or some such which could be run directly as a
batch file.

(Sure, I could create a macro or something that secretly renames the
file with a .cmd, runs the batch, then renames the file back again, but
that's cheating!)

--
David
Stardate 7073.7
 
Reply With Quote
 
 
 
 
Mark Blain
Guest
Posts: n/a
 
      28th Jan 2007
David Trimboli <(E-Mail Removed)> wrote (in part):

> Is there any way to run a batch script from anything other than a .bat
> or .cmd file?
>
> In Unix, I can create a batch file of any name, then run, for instance,
> sh <filename>
> to execute the batch.
>
> In cmd.exe, the same doesn't work.


Not a true script, no. Cmd.exe distinguishes scripts solely by file
extension. There is definitely no feature to recognize scripts by
"executable" permissions in the file system or by the famous unix
hashbang (#!/bin/sh)

You can execute individual commands in "yourfile" one-by-one using
redirection or "for /f", but that trick is of limited usefulness.

(Create a file containing a couple of harmless dir commands.)

for /f %i in (yourfile) do %i
or
cmd <yourfile
or
type yourfile | cmd

To run scripts as you describe try a Windows port of a unix shell,
such as those available in Cygwin, Mingw/MSYS or MS Windows
Services For Unix.
http://www.cygwin.com
http://www.mingw.org/
http://en.wikipedia.org/wiki/Microso...vices_for_UNIX

 
Reply With Quote
 
David Trimboli
Guest
Posts: n/a
 
      28th Jan 2007
Mark Blain wrote:
> David Trimboli <(E-Mail Removed)> wrote (in part):
>
>> Is there any way to run a batch script from anything other than a .bat
>> or .cmd file?

>
> Not a true script, no. Cmd.exe distinguishes scripts solely by file
> extension. There is definitely no feature to recognize scripts by
> "executable" permissions in the file system or by the famous unix
> hashbang (#!/bin/sh)


Thanks, Mark. That's what I thought.

> You can execute individual commands in "yourfile" one-by-one using
> redirection or "for /f", but that trick is of limited usefulness.


Neat idea! I hadn't thought of that. No, it's not what I meant, but it's
creative.

--
David
Stardate 7074.7
 
Reply With Quote
 
Herb Martin
Guest
Posts: n/a
 
      28th Jan 2007

"David Trimboli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mark Blain wrote:
>> David Trimboli <(E-Mail Removed)> wrote (in part):
>>
>>> Is there any way to run a batch script from anything other than a .bat
>>> or .cmd file?

>>
>> Not a true script, no. Cmd.exe distinguishes scripts solely by file
>> extension. There is definitely no feature to recognize scripts by
>> "executable" permissions in the file system or by the famous unix
>> hashbang (#!/bin/sh)

>
> Thanks, Mark. That's what I thought.
>
>> You can execute individual commands in "yourfile" one-by-one using
>> redirection or "for /f", but that trick is of limited usefulness.

>
> Neat idea! I hadn't thought of that. No, it's not what I meant, but it's
> creative.


One issue with Cygwin is that for all your current batch files with
extensions, you must type the ?##%$* extension or they don't
work from the CygWin session. (Mybat.cmd is necessary there
if the file has an extension.)

--
Herb Martin, MCSE, MVP
http://www.LearnQuick.Com
(phone on web site)


 
Reply With Quote
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      28th Jan 2007

"David Trimboli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mark Blain wrote:
> > David Trimboli <(E-Mail Removed)> wrote (in part):
> >
> >> Is there any way to run a batch script from anything other than a .bat
> >> or .cmd file?

> >
> > Not a true script, no. Cmd.exe distinguishes scripts solely by file
> > extension. There is definitely no feature to recognize scripts by
> > "executable" permissions in the file system or by the famous unix
> > hashbang (#!/bin/sh)

>
> Thanks, Mark. That's what I thought.
>
> > You can execute individual commands in "yourfile" one-by-one using
> > redirection or "for /f", but that trick is of limited usefulness.

>
> Neat idea! I hadn't thought of that. No, it's not what I meant, but it's
> creative.


Yes, way cool. But the for/f trick is severely limited, considering:

- the problems that will result from the external script containing any
special characters that need special treatment;
- references to batch parameters;
- compound statements occupying more than one line;

Way simpler to use the .cmd extension in the first place.

/Al


 
Reply With Quote
 
David Trimboli
Guest
Posts: n/a
 
      29th Jan 2007
Al Dunbar [MS-MVP] wrote:
> Way simpler to use the .cmd extension in the first place.


Quite true. I just find it odd that batch files, which are simply text
files containing command lines, absolutely must have .cmd or .bat to work.

Perhaps this is a security feature, to prevent running batch commands
from arbitrary (and thus undetectable) text files?

--
David
Stardate 7079.1
 
Reply With Quote
 
Herb Martin
Guest
Posts: n/a
 
      29th Jan 2007

"David Trimboli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Al Dunbar [MS-MVP] wrote:
>> Way simpler to use the .cmd extension in the first place.

>
> Quite true. I just find it odd that batch files, which are simply text
> files containing command lines, absolutely must have .cmd or .bat to work.
>
> Perhaps this is a security feature, to prevent running batch commands from
> arbitrary (and thus undetectable) text files?


Could be but it probably just an historical artifact of the original
command processor code -- and no one has ever seen a reason
for changing it, or when they thought of changing it then decided
security was better this way.

It wan't until fairly recently that the list of file extensions that are
"executable" could be extended, and I have personally tried to
extend it by the <nul> extension with no success.

--
Herb Martin, MCSE, MVP
http://www.LearnQuick.Com
(phone on web site)


 
Reply With Quote
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      30th Jan 2007

"Herb Martin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "David Trimboli" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Al Dunbar [MS-MVP] wrote:
> >> Way simpler to use the .cmd extension in the first place.

> >
> > Quite true. I just find it odd that batch files, which are simply text
> > files containing command lines, absolutely must have .cmd or .bat to

work.

Batch files do not "work" on their own, but are processed by the O/S. And I
have mostly always found it a waste of time to argue with the O/S on these
things.

Do executable files work with extensions other than the few obvious ones?
vbscript files?

> > Perhaps this is a security feature, to prevent running batch commands

from
> > arbitrary (and thus undetectable) text files?


Whether or not by design, that certainly simplifies things for software that
is designed to quarantine files of certain types...

> Could be but it probably just an historical artifact of the original
> command processor code -- and no one has ever seen a reason
> for changing it, or when they thought of changing it then decided
> security was better this way.


Could be. I remember using a minicomputer O/S long ago that had a batch file
equivalent. But it also had a built-in command that allowed one to run a
batch file regardless of the type of the file. Could be done here with CALL,
except that that works for .exe's as well...

/Al


 
Reply With Quote
 
Michael Bednarek
Guest
Posts: n/a
 
      30th Jan 2007
On Mon, 29 Jan 2007 23:13:15 -0700, Al Dunbar [MS-MVP] wrote in
microsoft.public.win2000.cmdprompt.admin:

>"Herb Martin" <news@...> wrote in message
>news:(E-Mail Removed)...
>>
>> "David Trimboli" <trimboli@...> wrote in message
>> news:(E-Mail Removed)...
>> > Al Dunbar [MS-MVP] wrote:
>> >> Way simpler to use the .cmd extension in the first place.
>> >
>> > Quite true. I just find it odd that batch files, which are simply text
>> > files containing command lines, absolutely must have .cmd or .bat to work.

>
>Batch files do not "work" on their own, but are processed by the O/S. And I
>have mostly always found it a waste of time to argue with the O/S on these
>things.


Not strictly true; batch files are processed by a batch file processor,
which is usually also a command line processor (CLI). A popular sample
CLI is CMD.EXE; there are others.

>Do executable files work with extensions other than the few obvious ones?
>vbscript files?

[snip]

Yes: rename NOTEPAD.EXE to anything.any and:
start anything.any
will run it.

Also:
cscript //E:vbscript fu.bar
will work as expected. See:
<http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_divn.mspx?mfr=true>.

OTOH, there seems to be indeed no method to make CMD.EXE or COMMAND.COM
(or 4NT for that matter) to process a file with a non-standard
extension.

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"
 
Reply With Quote
 
David Trimboli
Guest
Posts: n/a
 
      30th Jan 2007
Michael Bednarek wrote:
> On Mon, 29 Jan 2007 23:13:15 -0700, Al Dunbar [MS-MVP] wrote in
> microsoft.public.win2000.cmdprompt.admin:
>> Do executable files work with extensions other than the few obvious ones?
>> vbscript files?

> [snip]
>
> Yes: rename NOTEPAD.EXE to anything.any and:
> start anything.any
> will run it.
>
> Also:
> cscript //E:vbscript fu.bar
> will work as expected. See:
> <http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_divn.mspx?mfr=true>.
>
> OTOH, there seems to be indeed no method to make CMD.EXE or COMMAND.COM
> (or 4NT for that matter) to process a file with a non-standard
> extension.
>


Thanks for the notes, Michael. The bit about renaming notepad.exe was
particularly interesting.

--
David
Stardate 7081.7
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling multiple batch files from within a batch file yawnmoth Windows XP General 3 26th May 2008 06:47 PM
batch file to generat file names and content from .txt files? gerryR Microsoft Windows 2000 CMD Promt 5 31st Aug 2006 09:38 PM
DOS: How to create a batch file that will move file based on files =?Utf-8?B?QnJhbmRvbg==?= Windows XP General 2 25th Aug 2006 11:50 PM
How to run batch files from bat file? brett Windows XP General 3 12th Jul 2006 01:29 AM
file splitter (also creates a batch file to re-connect the split files) *ProteanThread* Freeware 0 11th Jan 2005 04:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:21 PM.