odd behavior with cmd echo writing to txt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

odd behavior with cmd echo writing to txt

#1 Post by julesverne » 19 Nov 2013 01:32

Hi all,
Long time time reader, first time poster. I recently wrote a batch program utilizing sendto parameters (%*), that would end with the batch program bringing up the working windows folder back to the forefront. I'm not interested in using autohotkey to do it, although I have to admit I recently discovered that gem from this website, and it's great!

Utilizing a recent discovery of having .bat create a temp vbs script by echoing out the commands to temp vbs script and then running it and closing it, I had an idea to do the same thing with bringing the working window to the forefront. However, it is exhibiting very odd behavior when echoing out the text to the vbs script. I have a feeling it has to do with delayedexpansion.

I have included some comments where I'm seeing some of this odd behaviour.

Code: Select all

REM echo out the first line of the vbs script
echo Set objShell = CreateObject("WScript.Shell") > WINDOWFOCUS.vbs

SETLOCAL enableExtensions enableDelayedExpansion

REM Get the filepath from %* parameter

FOR /F "delims=" %%i in (%*) do (
  REM for some reason this will break if the next line doesn't include the oddball 3rd double quote
  SET "filepath=%%~dpi""
  REM This removes backslash.. -1 doesn't work have to use -2 which seems odd
  SET "filepath=!filepath:~0,-2!"
  REM this seems unnecessary to assign varpath as a variable but !filepath! won't work outside the loop.
  SET varpath=!filepath!
  CALL :breakingloop
)

:breakingloop
REM http://stackoverflow.com/questions/774175/how-can-i-open-a-message-box-in-a-windows-batch-file
REM this line doesn't get completely echoed out to .vbs file
echo objShell.AppActivate("!varpath!")>> WINDOWFOCUS.vbs
call WINDOWFOCUS.vbs
pause
del WINDOWFOCUS.vbs

exit



so the final WINDOWFOCUS.vbs should look like this:
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate("C:\examplefolder")

If I create this vbs file myself, it runs as it should when I run it. It brings the folder into focus.

Any help would be greatly appreciated.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: odd behavior with cmd echo writing to txt

#2 Post by foxidrive » 19 Nov 2013 01:47

This uses a better method to get rid of a trailing slash, and the () need to be escaped in the echo command when within a loop.



Code: Select all

@echo off
REM echo out the first line of the vbs script
echo Set objShell = CreateObject("WScript.Shell") > WINDOWFOCUS.vbs

REM Get the filepath from %* parameter

FOR %%a in (%*) do (
   for %%b in ("%%~a\.") do (
     REM http://stackoverflow.com/questions/774175/how-can-i-open-a-message-box-in-a-windows-batch-file
     echo objShell.AppActivate^("%%~fb"^)>> WINDOWFOCUS.vbs
     call WINDOWFOCUS.vbs
     pause
    del WINDOWFOCUS.vbs
)
)
exit

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: odd behavior with cmd echo writing to txt

#3 Post by julesverne » 19 Nov 2013 02:50

Your method definitely looks and works a lot better than mine. I was wondering if I needed to escape the paranthesis. The vbs script is finally getting written correctly except for the variable. In order for the window to come to the front the syntax needs to be just the folder path without the trailing slash. The way the script is written it includes the filename. Below in REM you can see that I changed the modifiers to fix this, however, doing this brings back the trailing slash. What do you suggest?

Code: Select all

FOR %%a in (%*) do (
   for %%b in ("%%~a\.") do (
     REM http://stackoverflow.com/questions/774175/how-can-i-open-a-message-box-in-a-windows-batch-file
     REM I changed the variable in the next line to give me the drive letter and path by using the modifiers dp
     REM however this brings back the trailing slash
     echo objShell.AppActivate^("%%~dpb"^)>> WINDOWFOCUS.vbs
     call WINDOWFOCUS.vbs
     del WINDOWFOCUS.vbs
     REM I've added the goto breakingloop label back in because one of the great things about sendto is you can select multiple files
     REM to run commands on, but only need one folderpath, so I don't want to run this loop through all files.
   goto :breakingloop
)
)
:breakingloop
exit

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: odd behavior with cmd echo writing to txt

#4 Post by foxidrive » 19 Nov 2013 03:22

You didn't say that you were dropping files on it...

This will take the path from the last "path\filename" on the command line.
I'm not sure that calling the vbs script is what you need to do...

Code: Select all

@echo off
REM echo out the first line of the vbs script
echo Set objShell = CreateObject("WScript.Shell") > WINDOWFOCUS.vbs

FOR %%a in (%*) do for %%b in ("%%~dpa\.") do set "p=%%~dpnxb"
     REM http://stackoverflow.com/questions/774175/how-can-i-open-a-message-box-in-a-windows-batch-file
     echo objShell.AppActivate("%p%")>> WINDOWFOCUS.vbs
     call WINDOWFOCUS.vbs
     pause
     del WINDOWFOCUS.vbs

exit

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: odd behavior with cmd echo writing to txt

#5 Post by julesverne » 19 Nov 2013 04:57

Thanks so much foxidrive. Sorry.. my bad for not making clear about dropping files. That part of the batch happens in a different section of the script. I only included the part here that I was having trouble with so it wouldn't be a whole mess of code to analyze. Anyway, your script worked however one thing I remembered from reading is that if a window is minimized that vbs script won't do anything. So, I added another line and works as perfectly as i could get it. The only thing it doesn't do is, if the window was completely closed, to bring up another explorer window to that path would be cool. I know how to do it in the bat program, but not in the vbs script in the form of an if statement, which would make the most sense to have the vbs script determine if the window exists. Although.. I suppose I could try using wmic to determine if it's still there. Anyway, here's the code I have.

Code: Select all

REM echo out the first line of the vbs script
echo Set objShell = CreateObject("WScript.Shell") > WINDOWFOCUS.vbs

REM Get the filepath from %* parameter

REM echo out the first line of the vbs script
echo Set objShell = CreateObject("WScript.Shell") > WINDOWFOCUS.vbs

FOR %%a in (%*) do for %%b in ("%%~dpa\.") do set "p=%%~dpnxb"
     REM http://stackoverflow.com/questions/774175/how-can-i-open-a-message-box-in-a-windows-batch-file
     echo objShell.AppActivate("%p%")>> WINDOWFOCUS.vbs
     REM http://www.youtube.com/watch?v=615NpV_RmcA
     REM line below restores active window if minimized %% is added to escape the first percent sign.
     echo objShell.SendKeys "(%% ) (R)" 'restore active window >> WINDOWFOCUS.vbs
     call WINDOWFOCUS.vbs
     del WINDOWFOCUS.vbs
   

exit

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: odd behavior with cmd echo writing to txt

#6 Post by Squashman » 19 Nov 2013 07:29

Maybe there is an easier solution. Well this works for me at least on Windows 7.

The path I am testing with already has the folder opened and minimized.

When I put this into my batch file it brings the folder to focus. It will also open a new window if it already isn't open.

Code: Select all

start H:\path_to_my_minimized\folder

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: odd behavior with cmd echo writing to txt

#7 Post by julesverne » 19 Nov 2013 13:45

hah! that's perfect squashman! haha. I love simple. I guess at least I learned a few things in the process though. Thanks to you and foxidrive for the help.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: odd behavior with cmd echo writing to txt

#8 Post by Ed Dyreen » 19 Nov 2013 19:21

lol

Post Reply