Solved

Need proper syntax for converting .vbs script to dos batch file

Posted on 2014-11-28
16
167 Views
Last Modified: 2014-11-30
I'm trying to change the following batch file from this:

if not exist "C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE" (
	set SourceDB=KNOW64.accdb
) else (
	set SourceDB=KNOW.accdb
)

if not exist "%UserProfile%\My Documents\FEO_TRANSFER\DBS" md "%UserProfile%\My Documents\FEO_TRANSFER\DBS"
xcopy \\fkpdfs05\i\SD\FEO_TRANSFER\DBS\%SourceDB% "%UserProfile%\My Documents\FEO_TRANSFER\DBS\" /d /y
start MSACCESS "%UserProfile%\My Documents\FEO_TRANSFER\DBS\%SourceDB%"

Open in new window


and grab a registry value shown in this website:

http://stackoverflow.com/questions/445167/how-can-i-get-the-value-of-a-registry-key-from-within-a-batch-script

Instead of hardcoding the path of the batchfile I want to set the variable whatever is in for value:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.EXE\Path

I tried this:

FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

\MSACCESS.EXE\Path" /v ValueName`) DO (
    set appdir=%%A %%B
    )
ECHO %appdir%
pause

Open in new window


But the error I get is system was unable to find the specified registry key or value.
Please note attachment A.  Attachment B is provided to show that path actually does exist.
I believe currently I don't have the correct syntax - thus please help!
AttachmentA.PNG
AttachmentB.PNG
0
Comment
Question by:stephenlecomptejr
  • 10
  • 6
16 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40470950
Try it like this:
@echo off
setlocal
for /f "tokens=2*" %%a in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.exe" /v Path ^| find /i "Path"') do set AppDir=%%b
echo AppDir: %AppDir%

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471248
Ok that gets me started but how do I use a wildcard to now take the variable AppDir and determine if 64-bit or not?
I'm currently basing that on if the AppDir has (x86) in the variable.

One example if its' C:\Program Files (x86)\Microsoft Office\Office14 then I know it's not 64-bit (as shown in the above vbs script)

So using this:  how would I rearrange the IF statement shown in the .vbs script file within this new batch file syntax?

http://superuser.com/questions/709651/how-to-perform-wildcard-string-comparison-in-batch-file
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471473
I tell you what - I know this was posted late and I really feel like this should be a two-part question.  So let me give you the correct answer and do part 2 for this.  Cause I really appreciate the reply.
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 40471477
Great - going to post a second question about this.  Since this person went out of there way to comment during the holidays - the best I can do is post another question to help his score and grade out.   The only issue I see is a free for all for the next commenter so if someone else responds they earn the points if answer first.
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471497
Sorry oBdA,

I figured it out doing this:

@echo off
setlocal
for /f "tokens=2*" %%a in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.exe" /v Path ^| find /i "Path"') do set AppDir=%%b
AppDir = AppDir + "MSACCESS.exe"
echo AppDir: %AppDir%
pause
find "(x86)" %AppDir% >nul
if %errorlevel% equ 1 echo "not 64-bit"
pause

Open in new window


Thank you sincerely for your help!
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40471503
That will not work. "find" will either accept input from the console, or search for files passed as command line arguments.
You'd need something like this:
echo %AppDir%| find "(x86)"  >nul

Open in new window

Note that this will only work on 64bit Windows. It will not detect a 32bit Office on a 32bit Windows; do you have this situation?
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471548
This was the final code and seems to be working...
Are you saying the following won't work still?

@echo off

setlocal

for /f "tokens=2*" %%a in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.exe" /v Path ^| find /i "Path"') do set AppDir=%%b

AppDir = AppDir + "MSACCESS.exe"

echo AppDir: %AppDir%

find "(x86)" %AppDir% >nul

if %errorlevel% equ 1 (

set SourceDB=KNOW.accdb

) else (

set SourceDB=KNOW64.accdb

)

if not exist "%UserProfile%\My Documents\FEO_TRANSFER\DBS" md "%UserProfile%\My Documents\FEO_TRANSFER\DBS"

xcopy \\fkpdfs05\i\SD\FEO_TRANSFER\DBS\%SourceDB% "%UserProfile%\My Documents\FEO_TRANSFER\DBS\" /d /y

start MSACCESS "%UserProfile%\My Documents\FEO_TRANSFER\DBS\%SourceDB%"

Open in new window

0
 
LVL 83

Expert Comment

by:oBdA
ID: 40471604
This should end up always using KNOW.accdb as SourceDB. Just paste this into a command prompt:
set AppDir=C:\Program Files (x86)\Microsoft Office\Office14\
find "(x86)" %AppDir%

Open in new window

You should see a bunch of error messages.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471698
I'm confused by your last comment:
When you say This should end up always using KNOW.accdb as SourceDB - are you talking about my code or the one you are posting below?

I can't use the last code posted because someone installed Office 2013 on another.  And of course that stated path is for Office 2010 only.  

Don't you see in my batch file my If statement checks for any error and if found then

find "(x86)" %AppDir% >nul

if %errorlevel% equ 1 (

set SourceDB=KNOW.accdb

) else (

set SourceDB=KNOW64.accdb

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471720
So giving your last comment a little more thought....

Are you saying with my code - no matter what an error is thrown and people that actually have C:\Program Files\Microsoft Office\Office14\MSAccess.exe as a valid 64 bit path - will still only open KNOW.accdb instead of KNOW64.accdb?

So with that statement - what is then the fix?  What should I change so there is no error in the find...
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40471830
Your code will always throw an error, so the "if errorlevel equ 1" will always be true.
As mentioned in http:#a40471503, if you want to check a string using "find", you need to pass it through the pipe:
echo %AppDir%| find "(x86)"  >nul
if errorlevel 1 (
	echo Office x64 found.
) else (
	echo Office x86 found.
)

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471921
Ok so I tried your code shown last.  And obviously its picking Office 64 bit when I only have 32-bit.

How do I pass it through the pipe as described?
What syntax do I need then?

[Cause just to emphasize again]

@echo off

setlocal

for /f "tokens=2*" %%a in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.exe" /v Path ^| find /i "Path"') do set AppDir=%%b

AppDir = AppDir + "MSACCESS.exe"

echo %AppDir%| find "(x86)"  >nul
if errorlevel 1 (
	set SourceDB=KNOW.accdb
) else (
	set SourceDB=KNOW64.accdb
)

echo %SourceDB%
pause

Open in new window


Yet this still tells me I have 64-bit and I instead have 32-bit.  So the last suggested one doesn't work as well.

Please note error message I'm getting in this screen shot.

Thanks.
ErrorIget.PNG
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40471940
@echo off
setlocal

for /f "tokens=2*" %%a in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.exe" /v Path ^| find /i "Path"') do set AppDir=%%b

set AppDir=%AppDir%MSACCESS.exe

echo %AppDir%| find "(x86)"  >nul
if errorlevel 1 (
	set SourceDB=KNOW.accdb
) else (
	set SourceDB=KNOW64.accdb
)

echo %SourceDB%
pause

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40471958
ok.  So that last code works without throwing an error.
But the problem is that its trying to select KNOW64.accdb when I it should be showing KNOW.accdb for 32-bit.
Please note attachment to show you what I"m getting and what the registry value path is.

I really do appreciate all the replies on this...
nomoreerrorbutnotrightpath.PNG
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40472480
@echo off
setlocal
for /f "tokens=2*" %%a in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.exe" /v Path ^| find /i "Path"') do set AppDir=%%b
set AppDir=%AppDir%MSACCESS.exe
echo %AppDir%| find "(x86)"  >nul
if errorlevel 1 (
	set SourceDB=KNOW64.accdb
) else (
	set SourceDB=KNOW.accdb
)
echo %SourceDB%
pause

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40472668
oBda - thank you for your help.  Now it's working fantastic.
Thank you again for your patience on this.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now