Read File from batch program

Microsoft Windows xp professional w/sp2
January 5, 2010 at 01:53:52
Specs: Windows XP
Hello All,

I am searching an exe file using DOS and writing it to a temp file. Command for Searching is

dir /s /b winword.exe > temp.bat

The output of this searching i require in a temp Variable for String Manipulation.

I dont know how to do it. Can any one plz help
me.... How to get the output of this into a temp variable.

See More: Read File from batch program

Report •

January 5, 2010 at 03:33:50
First, I would advise you not to call the temporary file temp.bat because it's not a batch file. Use .tmp instead.

If the output is just one line, you can do this:

set /p winwordpath=<temp.tmp

However, if the only reason you create temp.tmp is to read it straight back in, you don't have to create a temporary file at all:

for /f "delims=" %%a in ('dir /s /b winword.exe') do set winwordpath=%%a

Report •

January 5, 2010 at 04:03:48
By the way, the following is probably faster, as it doesn't search through a directory tree. There is one caveat: it only works on machines where Word has been run at least once.

What sort of string manipulation do you want to do on the variable? If you want to extract the directory path, just use the second example code. If you want the full pathname of winword.exe, use the first example.

rem Put winword.exe pathname into variable winwordpathname
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe" /ve ^| find "REG_"') do (
   set winwordpathname=%%b

rem Put the directory where winword.exe resides into winword path
for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe" /v Path ^| find "REG_"') do (
   set winwordpath=%%b

Report •

January 5, 2010 at 04:07:06
I still don't know where you're going with this but you may be interested to know that if winword is installed [I suppose it is.] you don't need it in the path to run it. You can just:

start - run - winword

This works not because it's in the path but because it's in the registry.

Helping others achieve escape felicity


Report •

Related Solutions

January 5, 2010 at 04:36:03
Thanks you so much friends.. for the help...

I just did it using this

for /f "tokens=1 delims= usebackq" %%a in (temp1) do set
echo %checkdata%

Thanks a lot..

Report •

January 5, 2010 at 05:47:37
@M2: good point, and you can also run Word from the command line (no need to use the mouse) just by typing "start Winword".

So, to the OP, as M2 said, you haven't told us what you want this variable for, but maybe you don't need it at all.

Report •

January 5, 2010 at 07:40:07
Hello Friedns..!

Actully i have an appliaction that calls the winword.exe (Its the exe file of word). By now i have to set its winword path in PATH variable manually. I am trying to make a small batch file that can serch the winword.exe and can add it into PATH automatically.
when i do 'dir /s /b winword.exe' it gives me the complete path that conatins address upto winword.exe . so i am putting that into a temp variable so that i can remove the exe filne name from last.

I will definately try the M2 way tomorrow. Thats a good one.

Thanks once again Friends..

Report •

January 5, 2010 at 22:44:05
Having another program use winword seems odd, but nevermind.

If you just need to add the path to winword you can do it manually, just ONCE. Unless, of course, it moves around while you're not looking. In that case this is way too wierd for me.

To find it with a bat:

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d c:\winword.exe') do (
set myvar=%%~DPa

echo !myvar!

Helping others achieve escape felicity


Report •

Ask Question