Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

876 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