For command initiates autorun bat file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

For command initiates autorun bat file

#1 Post by taripo » 01 Aug 2011 13:58

I made a bat file, it says echo sss ttt and runs at the start, and i'd like it running at the start

this key refers to it
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

But that FOR command is executing it in some way! and executing echo sss

Why does it say echo sss and not echo sss ttt?
How can I suppress the running of cmd or that initialization bat file with the FOR?

The FOR statement should really just display "echo g", not "echo sss" or "echo sss ttt".


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
sss ttt
C:\Documents and Settings\Administrator>cd\

C:\>type \blah\startfile.bat <ENTER>
@echo sss ttt

C:\>

C:\>for /f %f in ('echo g ^| findstr "g"') do echo %f <ENTER>

C:\>echo sss
sss

C:\>echo g
g

C:\>

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: For command initiates autorun bat file

#2 Post by dbenham » 01 Aug 2011 14:33

Your FOR /F statement is executing your AUTORUN script because FOR /F must instantiate another CMD.EXE in order to execute the command in the IN ('command') clause.

Your AUTORUN script outputs sss ttt, and your FOR /F only preserves the 1st token which is sss. BUT it doesn't make sense that sss passes your FINDSTR portion of the command. I'm wondering if perhaps your example is not precisely what is happening on your machine, and in attempt to paraphrase the situation you've got the scenario slightly wrong.

I'm not aware of a way to disable AUTORUN when using FOR /F %A IN ('COMMAND').

Perhaps you could modify your AUTORUN script so that the main body only executes once within a session:

Code: Select all

@echo off
if defined AUTORUN_HAS_ALREADY_BEEN_RUN exit /b
echo sss ttt
set AUTORUN_HAS_ALREADY_BEEN_RUN=1
When your FOR statement instantiates a new CMD.EXE, it inherits the existing environment, so the AUTORUN script should exit prior to doing any harm. I haven't tested this, but it seems like it should work.

Dave Benham

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: For command initiates autorun bat file

#3 Post by taripo » 01 Aug 2011 14:36

what is "if defined"?

I don't see it listed in "help if"

is it recorded anywhere in documentation?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: For command initiates autorun bat file

#4 Post by dbenham » 01 Aug 2011 14:59

It is a standard command extension that should be available to you in XP. Normally command extensions are enabled by default.

Here is the relavent documentation:
If Command Extensions are enabled IF changes as follows:

IF [/I] string1 compare-op string2 command
IF CMDEXTVERSION number command
IF DEFINED variable command
...
The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment variable
is defined.


If your "DOS" session is using COMMAND.COM instead of CMD.EXE then I guess the command extensions are not available, but I'm not sure. I don't have access to XP at the moment.

You can simply change the test as follows:

Code: Select all

if not "%AUTORUN_HAS_ALREADY_BEEN_RUN%"=="" exit /b


Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: For command initiates autorun bat file

#5 Post by dbenham » 02 Aug 2011 05:30

I checked my XP @home

COMMAND.COM on XP supports IF DEFINED, and there doesn't appear to be a way to disable command extensions with COMMAND.COM.

Based on the header display when you start your command shell, you are running CMD.EXE anyway.

Your HELP IF results should include the description of IF DEFINED.

Dave Benham

Post Reply