0

I am a programmer although I have little to no experience with scripts and batch files.

Using this page as reference, it basically describes what I want to do: search for a file and return the path. Find file and return full path using a batch file

As soon as echo %p% is executed (so that I can see the value that was stored), all that is printed on the screen is "ECHO is off." From what I've read, this is either caused by spaces or because the value is blank.

From what I can tell, everything seems to be working fine.

I am trying to improve a current batch file by making it dynamic to handle path issues for 32 vs 64 bit programs and various Microsoft office versions.

This structure works:

IF EXIST "\\server\folder\file.dbf" (
"C:\Program Files (x86)\Microsoft Office\Office12\winword.exe" "\\server\folder\file.docx" /mDoMailMergeViewAllCG
)

Microsoft word is opened, the proper document is loaded then the macro is executed.

The following doesn't work:

@echo off

set PF=
set p=

rem Determine if system is 32bit or 64 bit.
IF EXIST %ProgramFiles(x86)%(
  SET "PF=%ProgramFiles(x86)%"
) ELSE (
SET "PF=%ProgramFiles%"
)

rem Determine which version of office is being used.
FOR /r "%PF%\Microsoft Office\" %%a in (*.EXE) do (
  IF "%%~nxa"=="WINWORD.EXE" (
     set "p=%%~dpnxa"
  )
)

IF EXIST "\\server\folder\file.dbf" (
   %p% "\\server\folder\file.docx" /mDoMailMergeViewAllCG
)

Microsoft word opens with the proper document although the macro does not get executed.

While researching this topic, I have seen comments about using other methods such as powershell or auto macro. At this time I am not prepared to explore these options.

How can I get the new script to execute the macros?

Community
  • 1
  • 1
CGibson
  • 3
  • 2

1 Answers1

0
@echo off
    setlocal enableextensions disabledelayedexpansion

    if defined ProgramFiles(x86) (
        set "pf=%ProgramFiles(x86)%"
    ) else (
        set "pf=%ProgramFiles%"
    )
    set "pf=%pf%\Microsoft Office"

    set "p="
    for /f "delims=" %%a in ('dir /s /b "%pf%\WinWord.exe"') do set "p=%%~fa"

    if defined p if exist "\\server\folder\file.dbf" (
        "%p%"  "\\server\folder\file.docx" /mDoMailMergeViewAllCG
    )

    endlocal

If your first code works, i see no reason for it not to work. There were some syntax problems in your code (missing space before opening parenthesis, non quoted program call) but the logic seems correct.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thanks for the feedback and the modified code. Your solution seems to work properly. Could you tell me what the difference is between the two for loops? – CGibson May 29 '14 at 14:08
  • @CGibson, In your code you iterate over all the list of all `exe` files under the starting folder, checking if it is `winword`. In my code i directly tell `dir` command to list all the `winword.exe` files under the starting point. In this case, in normal conditions, the code inside the `for` command is only executed once. – MC ND May 29 '14 at 14:12
  • Thanks again. That helps me to understand the processes more. – CGibson May 29 '14 at 14:26