Link to home
Start Free TrialLog in
Avatar of sunhux
sunhux

asked on

Windows batch script to read in current dir, 1st parameter, cd to where Adobe XI pro is, launch acrobat.exe, reading in current dir & filename (1st parameter)

I'd like to be able to launch Adobe Acrobat Pro from its directory
& then read a pdf file as its parameter but I don't like to search
for the 2000+ pdf files (amongst the dozens of folders) from
within WinExplorer as I like do a 'dir' search (to me this is
quicker than Win Explorer) for a  substring of the filename:
eg: dir *nessus*db*.pdf

I need a Windows  batch (.bat) script that will do the following:
a) take note of the current directory
b) read in the first 1st parameter (which is the filename) given
    to the batch script
c) script to "cd C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat"
d) script than launches:
     acrobat.exe  dir_noted_in_point_a\1st_parameter
Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

@sunhux

Base version 0.2
@ECHO OFF
REM BASE VERSION 0.0
SETLOCAL
REM TYPE
REM HELP SETLOCAL
REM at cmd.exe
REM Begins localization of environment changes in a batch file.  Environment changes made after SETLOCAL has been issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings.  When the end
REM c) script to "cd C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat"
REM We can set PATH environment variable
SET PATH=C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat;%PATH%
REM I am including C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader since AcroRd32.exe present at my host
SET PATH=C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader;%PATH%
REM Hence we can call acrobat.exe anywhere inside this script
REM IF required you can CLEAR THE SCREEN
REM CLS
REM ECHO DISPLAY CURRENT DIRECTORY NAME FOLLOWED BY BATCH FILE NAME
SET BATCHNAME=%0%
REM IF using CLS we can use following line.
REM ECHO %CD%^> %BATCHNAME% %* 
REM IF No parameter passed GOTO USAGE and display usage
IF "%1%" == "" GOTO USAGE
REM IF %1% IS A FILE AND %1% not a directory assigne SRCFILE to %1% first parameter
IF EXIST %1% AND NOT EXIST %1%\NUL (
	REM b) read in the first 1st parameter (which is the filename) given to the batch script
	SRCFILE=%1%
) ELSE IF EXIST "%1%\NUL" IF NOT EXIST "%2%" (
	ECHO File Not Found %2%
	GOTO FINISH
)
:GETSRC
REM Get user input for source directory
IF "%2%" == "" (
	REM IF second parameter is not passed
	REM get the user input for source directory
	REM a) take note of the current directory
	SET /P SRCDIR=Enter the value of source directory: 
	REM IF user given Enter without giving any input obtain valid user input using GOTO GETSRC
	IF "y" == "%SRCDIR%" GOTO GETSRC
	IF "n" == "%SRCDIR%" GOTO GETSRC
) ELSE (
	REM IF first parameter is a directory assign SRCDIR variable to directory name
	REM a) take note of the current directory
	SET SRCDIR=%1%
	IF EXIST %SRCDIR%\NUL (
		ECHO AcroRd32.exe %SRCDIR%\%2%
		START /B AcroRd32.exe %SRCDIR%\%2%
		GOTO FINISH
	) ELSE IF NOT EXIST "%SRCDIR%" (
		REM IF %1% is not a file and not a directory
		ECHO The system cannot find the  directory passed: ^= "%SRCDIR%" 
		GOTO FINISH
	)
)
REM ECHO %SRCDIR% SRCDIR
IF EXIST %SRCDIR%\NUL (
	REM if %SRCDIR% is present CD to that drive using /D and related directory %SRCDIR%
	ECHO AcroRd32.exe %SRCDIR%\%1%
	START /B AcroRd32.exe %SRCDIR%\%1%
	GOTO FINISH
) ELSE (
	ECHO The system cannot find the path specified. ^= "%SRCDIR%" 
	GOTO FINISH
)
GOTO FINISH
:USAGE
ECHO Usage:
ECHO %0% SAMPLE_FILE.pdf
ECHO OR
ECHO %0% SOURCE_DIRECTORY SAMPLE_FILE.pdf
GOTO FINISH
:FINISH
ENDLOCAL

Open in new window

I am re-writing the code again -> performing related testing

Current test case result:
C:\Users\murugesandins\experts_exchange>.\29177769.cmd . sample_file.pdf
AcroRd32.exe .\sample_file.pdf

C:\Users\murugesandins\experts_exchange>.\29177769.cmd sample_file.pdf
Enter the value of source directory: .
AcroRd32.exe .\sample_file.pdf

C:\Users\murugesandins\experts_exchange>

Open in new window

Avatar of sunhux
sunhux

ASKER

So I should just use the 2nd script with 8 lines or
should I replace certain portion of 1st script with
the 2nd??  Unclear on how/which script to use
@sunhux

Do not use old scripts.
Use the latest script which is available here
Let us know the related output you are obtaining / any exception you have seen ?
Current test case result: => I have pasted my output here.
Use the script pasted above under
Base version 0.2

@ECHO OFF
REM BASE VERSION 0.0
SET
...
:FINISH
ENDLOCAL
Avatar of sunhux

ASKER

Thanks.

Can you make the script auto detects the directory it's in
currently rather than prompt for the directory from user?
Something like below, extract from %CD% to use it:
C:\mydir>set cud=%CD%
C:\j>echo %cud%
C:\mydir

Another issue is:
this script executes  Acrobat.exe reader rather than
acrobat.exe (the XI Pro version) ie the dir\binary should be as below:
C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\acrobat.exe"
@sunhux

@ECHO OFF
REM BASE VERSION 0.5
SETLOCAL EnableDelayedExpansion
REM TYPE
REM HELP SETLOCAL
REM at cmd.exe
REM Begins localization of environment changes in a batch file.  Environment changes made after SETLOCAL has been issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings.  When the end
REM c) script to "cd C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat"
REM We can set PATH environment variable
SET PATH=C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat;%PATH%
REM I am including C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader since AcroRd32.exe present at my host
SET PATH=C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader;%PATH%
REM Hence we can call acrobat.exe anywhere inside this script
REM IF required you can CLEAR THE SCREEN
REM CLS
REM ECHO DISPLAY CURRENT DIRECTORY NAME FOLLOWED BY BATCH FILE NAME
SET BATCHNAME=%0%
REM IF using CLS we can use following line.
REM ECHO %CD%^> %BATCHNAME% %* 
REM IF No parameter passed GOTO USAGE and display usage
IF "%1%" == "" GOTO USAGE
REM IF %1% IS A FILE AND %1% not a directory assigne SRCFILE to %1% first parameter
IF EXIST %1% AND NOT EXIST %1%\NUL (
	REM b) read in the first 1st parameter (which is the filename) given to the batch script
	SRCFILE=%1%
) ELSE IF EXIST "%1%\NUL" IF NOT EXIST "%2%" (
	ECHO File Not Found %2%
	GOTO FINISH
)
:GETSRC
REM Get user input for source directory
IF "%2%" == "" (
	REM IF second parameter is not passed
	REM get the user input for source directory
	REM a) take note of the current directory
	SET "PARAM1=."
	REM IF user given Enter without giving any input obtain valid user input using GOTO GETSRC
	IF "y" == "%PARAM1%" GOTO GETSRC
	IF "n" == "%PARAM1%" GOTO GETSRC
) ELSE (
	REM IF first parameter is a directory assign PARAM1 variable to directory name
	REM a) take note of the current directory
	SET "PARAM1=%~1"
	IF EXIST %PARAM1%\NUL (
		ECHO AcroRd32.exe %PARAM1%\%2%
		START /B AcroRd32.exe %PARAM1%\%2%
		GOTO FINISH
	) ELSE IF NOT EXIST "%PARAM1%" (
		REM IF %1% is not a file and not a directory
		ECHO The system cannot find the  directory passed: ^= "%PARAM1%" 
		GOTO FINISH
	)
)
IF EXIST %PARAM1%\NUL IF NOT "%2%" == "" (
	REM if %PARAM1% is present CD to that drive using /D and related directory %PARAM1%
	ECHO AcroRd32.exe %PARAM1%\%2%
	START /B AcroRd32.exe %PARAM1%\%2%
	GOTO FINISH
) ELSE IF "%PARAM1%" == "." IF NOT "%1%" == "" (
	REM if %PARAM1% is present CD to that drive using /D and related directory %PARAM1%
	ECHO AcroRd32.exe %PARAM1%\%1%
	START /B AcroRd32.exe %PARAM1%\%1%
	GOTO FINISH
) ELSE (
	IF EXIST %PARAM1%\NUL IF "%2%" == "" (
		ECHO PASS FILENAME to SECOND PARAMETER
	) ELSE (
		ECHO The system cannot find the path specified. ^= "%PARAM1%" 
		GOTO FINISH
	)
)
GOTO FINISH
:USAGE
ECHO Usage:
ECHO %0% SAMPLE_FILE.pdf
ECHO OR
ECHO %0% SOURCE_DIRECTORY SAMPLE_FILE.pdf
GOTO FINISH
:FINISH
ENDLOCAL

Open in new window


Let use know if any modification required or not ?
I have tested the same at my host.
Avatar of sunhux

ASKER

Run into an issue:  if the PDF filename does not have a space (or special
character in it), this script (which I name it as ac.bat) works fine.
However, if there's space in the filename, ran into issues below:

C:\dir>ac QMS_POL_005 PC Purchase Policy v1.8.pdf
Acrobat.exe \PC

C:\dir>ac "QMS_POL_005 PC Purchase Policy v1.8.pdf"
PC was unexpected at this time.
@sunhux

Instead of using "QMS_POL_005 PC Purchase Policy v1.8.pdf"
use
ac QMS_PO~1.PDF
Reason to obtain windows format file name:

DIR /o:D /a /x *.pdf

Use the short name only when file name having space
C:\Users\murugesandins\experts_exchange> DIR /o:D /a /x *.pdf
 Volume in drive F is BackLearnWork
 Volume Serial Number is CC14-05B4

 Directory of C:\Users\murugesandins\experts_exchange

Tue 31-Dec-2019  02:28 PM            76,973 SAMPLE~1.PDF sample_file.pdf
Tue 31-Dec-2019  02:28 PM            76,973 QMS_PO~1.PDF QMS_POL_005 PC Purchase
 Policy v1.8.pdf
Mon 06-Apr-2020  08:58 AM                 0 V18~1.PDF    v1.8.pdf
               3 File(s)        153,946 bytes
               0 Dir(s)  205,683,175,424 bytes free

C:\Users\murugesandins\experts_exchange>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux

ASKER

Excellent guys.  The last script is real neat.

Can I request for 2 more scripts:

1. for MSWord where winword.exe is found in C:\Program Files\Microsoft Office\root\Office16
    So I'll just need to run the script (call it wd.bat)  as
       wd filename (which can be .doc or .docx)
    & it'll launch winword.exe reading in the content of the Word file

2. for MSWord where excel.exe is found in C:\Program Files\Microsoft Office\root\Office16
    So I'll just need to run the script (call it xc.bat) as
       xc filename (which can be .xls or .xlsx)
    & it'll launch winword.exe reading in the content of the Word file

Do cater for filenames with space/special characters in them, thanks very much.

My shot at the xc (ie Excel script) will be something like:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

rem  a) take note of the current directory
set cud=%CD%

rem  b) read in the first 1st parameter (which is the filename) given
set fn=%1
if [%fn%] equ [] echo *** Missing filename & goto :eof
set fn=%fn:"=%

pushd

rem  c) script to "cd C:\Program Files\Microsoft Office\root\Office16"
cd "C:\Program Files\Microsoft Office\root\Office16"

rem  d) script than launches excel.exe  dir_noted_in_point_a\1st_parameter
excel.exe  "%cud%\%fn%"

popd
Kind of curious about the need you are trying to solve?  If Acrobat, Word and Excel are correctly installed, you can open a file in those from the command line by simply entering the name of the file.  The extension will tell Windows the type of the file, and it will send the file to the assigned application for that file type, and open it.  So at a command prompt you should be able to just type:

"QMS_POL_005 PC Purchase Policy v1.8.pdf"

and the file will be opened.  The same os true for Word and Excel.


»bp
@sunhux

I cannot proceed since I am from Linux oriented.
Hence pressed helpful comment to NVIT (of course I'm not having any batch requirements)
Thought of informing you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux

ASKER

Bill

>If Acrobat, Word and Excel are correctly installed, you can open a file in those from the command line by simply entering the name of the file
Ever since we migrated from a Win2008 AD to a Win2019 AD, we can't launch it anymore, thus I'm asking for the scripts.
Reckon it has to do with certain GPO setting (which we don't know which one).

Refer to another EE thread on the issue:
https://www.experts-exchange.com/questions/29177098/cant-launch-file1-doc-file2-pdf-file3-txt-from-cmd-command-prompt-after-moving-to-new-Win2019-hardened-AD-servers.html

Didn't get to figure out what's the cause;  it happens on all PCs in the domain, not just mine.
@sunhux,

I noticed in your prior thread, that you had checked the associations for those file types.  In that comment it looked like you might be missing some parms from the ftype commands.  Below notice the command line parms that are passed to the programs, especially %1.  That's how the name of the file actually gets passed to the application.

It may just be that you didn't paste in all the info, but thought I would mention it just in case.  Below is how they look on my system (Win10, Office 2019) here.
User generated image
»bp
@sunhux

>> Ever since we migrated from a Win2008 AD to a Win2019 AD, we can't launch it anymore, thus I'm asking for the scripts.
EITHER
a)
Open cmd.exe for the user you need winword.exe to open.
b)
Inside that cmd.exe execute
C:\Windows\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables
OR logon using required user
a,b) windows r => C:\Windows\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables
c)
click edit prefix PATH value using
C:\Program Files (x86)\Microsoft Office\Office14;
OR
prefix PATH value using:
C:\Program Files (x86)\Microsoft Office\Office14;C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader;
I am having
C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE
C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe
exit from cmd.ee
here after we can execute related exe applications using
windows r => or inside cmd.exe without using full path.
@sunhux

Reason for using PATH environment at all operating systems:

Assume that we are having hai.exe at following directories:
C:\Users\murugesandins\hai.exe
C:\Users\murugesandins\testing\hai.exe
PATH environment variable having
C:\Users\murugesandins;C:\Users\murugesandins\testing;%PATH%
if we use windows r => hai.exe => OK
it Windows will execute hai.exe from C:\Users\murugesandins\hai.exe

Same thing applicable at Linux/AIX/SunOS/HP-UX/UNIX OS
separator being colon instead of semicolon

sample PATH at Linux
$ echo $PATH
/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/java/jdk1.6.0_31/bin:/bin:/u01/app/oracle/product/11.2.0/xe/bin:/usr/java/jdk1.6.0_27/bin:.

Open in new window



sample PATH at windows:
C:\Users\murugesandins>ECHO %PATH%
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\PROGRA~2\MozillaFirefox;C:\PROGRA~2\TextPad7;C:\PROGRA~2\MICROS~1\Office14;C:\PROGRA~2\INTERN~1;C:\PROGRA~2\Google\Chrome\APPLIC~1;C:\Users\murugesan;C:\Users\murugesandins\home\murugesan;C:\Program Files (x86)\VMware\VMware Player;;C:\TURBOC3\TurboCPP;C:\PROGRA~2\Java\jdk1.6.0_10\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0:.

C:\Users\murugesandins>

Open in new window


You can see that I am using .
including current directory at PATH environment variable.
Search all directory if not found in any of them search current directory also
First off, I really think you need to focus on the root cause of this problem, rather than a band-aid workaround.  If the file associations have been lost for PDF and Office files (and perhaps others?) then that's a pretty significant issue.  I'd want to try and figure out when it started, what caused it, did a GPO change get made, were updates rolled out, etc.  You really need to try and track that down and repair it, in my opinion.

Okay, here's the work around approach you could use, or at least a version of how I would implement that.  This is a BAT file that you can use to open defined file extensions using the program paths you assign.  I assigned the ones I have here, I think my PDF location is different than yours, but the Office ones may/should match if you are on 2019.

Save as a BAT file called "open.bat" (unless you already have a program or bat file named "open", then use a different name) and make sure the folder you place it in is in the PATH so that it will be available from any drive or folder.  You can probably find an existing folder that's in the PATH to place it in, if not then place it in a folder and then add that folder to the PATH.  No need to add any of the programs that will be used to open the specified file types to the PATH, that's not needed and just bloats the PATH variable.

This is a single BAT file that can be used to open multiple different types of files.  It gets the extension from the desired file to open (parm on command line) and matches it to a "table" of supported extensions that map to an EXE file to launch for that file type.  It can be expanded to handle additional file types and programs, just make sure you understand the format of the entries in the table before changing / adding to it.

Let me know what questions or problems you have.

@echo off
setlocal

rem Make sure a filename was specified
if "%1" EQU "" (
    echo No filename specified.
    echo USAGE: %~n0 [filename-to-open]
    exit /b
)

rem Make sure file specified exists
if not exist "%~1" (
    echo File "%~1" not found.
    echo USAGE: %~n0 [filename-to-open]
    exit /b
)

rem Define program to launch for supported extensions
set map="C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe=.pdf,.ppp"
set map=%map%,"C:\Program Files\Microsoft Office\root\Office16\winword.exe=.doc,.docx"
set map=%map%,"C:\Program Files\Microsoft Office\root\Office16\excel.exe=.xls,.xlsx"

rem Get extension from requested file, clear program exe variable
set "ext=%~x1"
set "exe="

rem Loop through all mapping table entries
for %%A in (%map%) do (
    rem Break apart each entry on the equal sign
    for /f "tokens=1-2 delims==" %%B in ("%%~A") do (
        rem Look at each of the extensions this entry supports
        for %%D in (%%~C) do (
            rem If it matches the input file extension, then we found the program to launch for this file
            if /i "%%~D" EQU "%ext%" (
                set "exe=%%~B"
                goto :ExitLoop
            )
        )
    )
)

:ExitLoop
rem If we didn't find the files extension in the supported table, report error
if not defined exe (
    echo Unknown file type.
    echo USAGE: %~n0 [filename-to-open]
    exit /b
)

rem Double check that the program file from the mapping table actually exists
if not exist "%exe%" (
    echo Program file "%exe%" not found.
    echo USAGE: %~n0 [filename-to-open]
    exit /b
)

rem Launch program for this file
start "" "%exe%" "%~1"

Open in new window


»bp
Avatar of sunhux

ASKER

>First off, I really think you need to focus on the root cause of this problem, rather than a band-aid workaround.  If the
> file associations have been lost for PDF and Office files (and perhaps others?) then that's a pretty significant issue.
It's lost for Pdf, Office files, .txt, .html or .htm  : I wud like to identify the cause too but got nowhere despite trying
out what expert had suggested.  Launching .exe  works fine though

Thanks for the universal script which I place in a folder that's in PATH & calls it xc.bat;
 it has certain syntax error when opening files with space in the filename but works
fine for files that are without spaces in it:

C:\j>xc "2016_16 JP TerminalAgmt n LeaseMgt Audit 2 Dec 16.pdf"
JP was unexpected at this time.

C:\j>xc 2016_16 JP TerminalAgmt n LeaseMgt Audit 2 Dec 16.pdf
File "2016_16" not found.
USAGE: xc [filename-to-open]

C:\j>xc "2016_16 JP TerminalAgmt n LeaseMgt Audit 2 Dec 16.docx"
JP was unexpected at this time.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you look at my comment #a43061299 above?  It looked like you were missing some items in your ftype  settings.  Also, keep in mind that you must start the command window in Admin mode to change these settings.

Also, what happens if you just enter the name of an existing file at the command prompt, like a PDF file, or Excel file?  Do you get an error?  Does anything happen?


»bp
Avatar of sunhux

ASKER

The last script Bill gave  worked wonderfully.


C:\QMS\02 Procedures\Archived>assoc .docx
.docx=Word.Document.12

C:\QMS\02 Procedures\Archived>assoc .pdf
.pdf=Acrobat.Document.11

C:\QMS\02 Procedures\Archived>
C:\QMS\02 Procedures\Archived>ftype Word.Document.12
Word.Document.12="C:\Program Files\Microsoft Office\Root\Office16\WINWORD.EXE" /n "%1" /o "%u"

C:\QMS\02 Procedures\Archived>ftype Acrobat.Document.11
Acrobat.Document.11="C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe" "%1"

C:\QMS\02 Procedures\Archived>ftype Excel.Sheet.12
Excel.Sheet.12="C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE" "%1"
Also, what happens if you just enter the name of an existing file at the command prompt, like a PDF file, or Excel file?
Do you get an error?  Does anything happen?
Avatar of sunhux

ASKER

>what happens if you just enter the name of an existing file at the command prompt,
> like a PDF file, or Excel file?  Do you get an error?  Does anything happen?

No error/messages appear, just nothing happens, it simply get back to the
command prompt as shown in attached.

There's no Event Viewer logs (for Security, Apps, Setup, System) with timings
that coincide with entering the filenames of these PDF/MSOffice  on the PC.
Not sure if AD/DC servers have any logs as I don't have access to them.
It happens to all PCs/laptops that in company's domain (whether the PCs
are brought home or in office joining the domain)
LaunchpdfdocNoResp.jpg
Very strange...
Avatar of sunhux

ASKER

As it coincide with us moving from a Win2008 AD servers to an almost
fully-hardened Win2019 AD server (we are now required to have 15
character length password with password history of 24 plus many
other GPO policies),  I reckon it's one of those policies but I don't know
where to begin to search in the lengthy gpresult.

Just one last bit:
I've amended DirPgm in NVIT's last script (just that it'll echo out the
parameter 1 ie filename to be edited) & Bill's "set map ..." for PDF
to run Acrobat XI Pro (not Acrobat Reader)

Will close this thread in 2 days' time if there's nothing further
Avatar of sunhux

ASKER

Thanks very much
Welcome.

»bp
@sunhux

NUL =>  redirect output to NUL (like /dev/null like Empty recycle bin at all operating systems)

ECHO MOST WELCOME > NUL