Solved

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

Posted on 2014-11-28
16
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
16 Comments
 
LVL 85

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 85

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 85

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 85

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 85

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 85

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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