why is this FOR doing this?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

why is this FOR doing this?

#1 Post by taripo » 20 Dec 2011 16:03

C:\WINDOWS\system32>for /f "tokens=*" %f in ('cd') do @ECHO %f <ENTER>
C:\

C:\WINDOWS\system32>

Why doesn't it display c:\windows\system32 ?

It just displays c:\

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

Re: why is this FOR doing this?

#2 Post by dbenham » 20 Dec 2011 16:21

Interesting. :?

I don't see that behavior in Vista. What version of Windows are you using :?:

Dave Benham

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

Re: why is this FOR doing this?

#3 Post by taripo » 20 Dec 2011 16:28

C:\WINDOWS\system32>ver

Microsoft Windows XP [Version 5.1.2600]

C:\WINDOWS\system32>

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: why is this FOR doing this?

#4 Post by orange_batch » 20 Dec 2011 16:33

Code: Select all

C:\WINDOWS\system32>for /f "tokens=*" %f in ('cd') do @ECHO %f
C:\WINDOWS\system32

C:\WINDOWS\system32>ver

Microsoft Windows XP [Version 5.1.2600]

C:\WINDOWS\system32>

Hmm... try "delims=" instead of "tokens=*"?

Does typing just cd output C:\? I know this may be specific, but there is the %CD% variable...

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

Re: why is this FOR doing this?

#5 Post by taripo » 20 Dec 2011 16:38

Since you mention vista works normally..

I just tried it on another computer, also XP SP2 5.2600 and it worked.

So it's just this one computer here.. where it behaves funny!

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

Re: why is this FOR doing this?

#6 Post by taripo » 20 Dec 2011 16:39

C:\WINDOWS\system32>cd <ENTER>
C:\WINDOWS\system32

C:\WINDOWS\system32>for /f %f in ('cd') do echo %f <ENTER>

C:\WINDOWS\system32>echo C:\
C:\

C:\WINDOWS\system32>for /f "delims=" %f in ('cd') do echo %f <ENTER>

C:\WINDOWS\system32>echo C:\
C:\

C:\WINDOWS\system32>

More

C:\WINDOWS\system32>for /f "usebackq delims=" %f in (`cd`) do echo %f <ENTER>

C:\WINDOWS\system32>echo C:\
C:\

C:\WINDOWS\system32>


C:\WINDOWS\system32>for /f %f in ("%cd%") do echo %f <ENTER>

C:\WINDOWS\system32>echo C:\WINDOWS\system32
C:\WINDOWS\system32

C:\WINDOWS\system32>

So just when CD is put in there as a command, there's the issue.

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

Re: why is this FOR doing this? (solved, kind of)

#7 Post by taripo » 20 Dec 2011 17:06

I've found out why it was doing it.
I remember finding out once that the FOR statement runs any initialisation batch file..'cos I once had some output in such a batch file. And the bat file was referenced in an autorun registry entry for the cmd prompt. Any run of the FOR statement ran it and echoed the stuff that bat file echoed. You probably know about that one..

In this case, I had a batch file with the line cd\

Because I don't like the CMD window going into
c:\documents and settings\user>

But I now see this affects the FOR

What to do then?

I do want the cmd prompt to start at C:\>!
But I don't want the FOR statement behaving badly!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: why is this FOR doing this?

#8 Post by orange_batch » 20 Dec 2011 17:37

I would recommend undoing any registry changes and removing the initialization batch, and instead load command prompt using a script.

Code: Select all

start cmd /k cd /d "C:\"

Personally I put single letter scripts into my user directory, so I just type x or y or z to initialize different settings. This could be a solution for you if UAC gets in the way.

x.bat (or .cmd)

Code: Select all

cd /d "C:\"
cls

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

Re: why is this FOR doing this?

#9 Post by taripo » 20 Dec 2011 18:06

I'm on XP.

I often like to launch cmd via start..run..cmd<ENTER>

I just put an icon in the part of the start menu that shows CMD straight away when START is clicked. Hopefully it'll stay there. I think it's 'pinned' there. And i've put an icon for it in the quick launch tray. So I might use that.

I just tried making a cmd.bat in \windows\system32 that could go to a directory, but start..run..CMD<ENTER> doesn't run the bat.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: why is this FOR doing this?

#10 Post by aGerman » 21 Dec 2011 11:52

taripo wrote:I just tried making a cmd.bat in \windows\system32 that could go to a directory, but start..run..CMD<ENTER> doesn't run the bat.

Save it as cmd_.bat and run CMD_ (or choose whatever name ... )

Regards
aGerman

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

Re: why is this FOR doing this?

#11 Post by taripo » 21 Dec 2011 18:19

thanks, I'm now using cmd1.bat

C:\WINDOWS\system32>type cmd1.bat
@cmd.exe /k cd /d c:\rr
C:\WINDOWS\system32>

It's pretty good, but doing start..run..cmd1 <ENTER> runs 2 cmd.exe processes. I'd rather just have one cmd prompt opening to the directory I want, and one cmd.exe process for that window.

How would I do it with only 1 cmd process created?

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: why is this FOR doing this?

#12 Post by timbertuck » 29 Dec 2011 11:57

i just checked your syntax and it appears correct. im using windows xp.

so if your bat file is just one line like so:
cmd1.bat
@cmd.exe /k cd /d c:\

then running this from the Run box, it only opens up one window (and displays the root)

you could also create a shortcut to cmd.exe on your desktop and tweak the properties of the shortcut (after you create the shortcut, right click on the icon, choose properties, from there you can add to the Target; where options go, and Start in; which you would set to the folder you want opened up, so in your case, C:\). If you did it this way, then your string would be "cmd.exe" without any parameters and your start in would be C:\

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

Re: why is this FOR doing this?

#13 Post by taripo » 29 Dec 2011 13:26

I like being able to load it with start..run so start..run..cmd1<ENTER>

I know it makes one window. I said it makes 2 processes, I want it to make just one. Look in task manager

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: why is this FOR doing this?

#14 Post by timbertuck » 29 Dec 2011 15:57

taripo wrote:I know it makes one window. I said it makes 2 processes, I want it to make just one. Look in task manager


my error, apologies for not reading the post completely. and yes your correct, one window, two process'.

ya learn something every day :)

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

Re: why is this FOR doing this?

#15 Post by taripo » 29 Dec 2011 16:50

so the question remains

Post Reply