[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2014-11-28
16
Medium Priority
?
195 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 86

Accepted Solution

by:
oBdA earned 2000 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 86

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 86

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
 
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 86

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 86

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 86

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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

607 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