dbTalk Databases Forums  

VBA - batch file

comp.databases.ms-access comp.databases.ms-access


Discuss VBA - batch file in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
bsn
 
Posts: n/a

Default VBA - batch file - 05-14-2011 , 10:00 AM






Hi NG

I try to start a batch file which uses an .txt file for scripting...
The txt file includes FTP username and password, and a put statement...

Code:
stAppName = CurrentProject.Path & "\backupfile.bat"
Debug.Print stAppName
MsgBox stAppName
Call Shell(stAppName, 1)
/Code

stAppName = C:\Documents and Settings\Bjarne\Desktop\backupfile.bat

The batch file should start an .txt file(backupfile.txt)
The .txt file lies on the Desktop to...

When i click on the batch file on the Desktop it all works well...
Its only when i try starting script from VBA the error occurs...

When I try to start the batch file from VBA, i get an error saying :
"Error opening scriptfile backupfile.txt."

Anybody have a hint...???

Greetings
Bjarne
"Every day new surprises"

Reply With Quote
  #2  
Old   
Vacuum Sealed
 
Posts: n/a

Default Re: VBA - batch file - 05-14-2011 , 09:31 PM






Hi Bjarne

I fairly certain you have to wrap this line:

stAppName = C:\Documents and Settings\Bjarne\Desktop\backupfile.bat

stAppName = "C:\Documents and Settings\Bjarne\Desktop\backupfile.bat"

HTH
Mick

Reply With Quote
  #3  
Old   
bsn
 
Posts: n/a

Default Re: VBA - batch file - 05-15-2011 , 04:33 AM



"Vacuum Sealed" <mclind1 (AT) bigpond (DOT) com> skrev i en meddelelse
news:jWGzp.4248$aH5.1130 (AT) viwinnwfe02 (DOT) internal.bigpond.com...
Quote:
Hi Bjarne

I fairly certain you have to wrap this line:

stAppName = C:\Documents and Settings\Bjarne\Desktop\backupfile.bat

stAppName = "C:\Documents and Settings\Bjarne\Desktop\backupfile.bat"
Thx for answer...
I tried this, but with same result...
Strange problem, when i click on the .bat file on the Desktop it all works
fine...
Bjarne

Reply With Quote
  #4  
Old   
John Spencer
 
Posts: n/a

Default Re: VBA - batch file - 05-15-2011 , 08:57 AM



Just to confirm.
You did something like the following to wrap the string in quotes.

stAppName = Chr(34) & CurrentProject.Path & "\backupfile.bat" & Chr(34)

Call Shell(stAppName, 1)


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

On 5/15/2011 5:33 AM, bsn wrote:
Quote:
"Vacuum Sealed"<mclind1 (AT) bigpond (DOT) com> skrev i en meddelelse
news:jWGzp.4248$aH5.1130 (AT) viwinnwfe02 (DOT) internal.bigpond.com...
Hi Bjarne

I fairly certain you have to wrap this line:

stAppName = C:\Documents and Settings\Bjarne\Desktop\backupfile.bat

stAppName = "C:\Documents and Settings\Bjarne\Desktop\backupfile.bat"

Thx for answer...
I tried this, but with same result...
Strange problem, when i click on the .bat file on the Desktop it all works
fine...
Bjarne



Reply With Quote
  #5  
Old   
bsn
 
Posts: n/a

Default Re: VBA - batch file - 05-15-2011 , 10:17 AM



Thx for answer...
Ur suggestion are not working - same result...
No I did this way, with same result as u write about ...
"""" & CurrentProject.Path & "\bufile.bat" & """"

As u see i have shorten the filename to under 8 chars to...
another hint i found on the internet...
but its still not working...8-(
Bjarne



"John Spencer" <JSPENCER (AT) Hilltop (DOT) umbc> skrev i en meddelelse
news:iqom3d$3bm$1 (AT) dont-email (DOT) me...
Quote:
Just to confirm.
You did something like the following to wrap the string in quotes.

stAppName = Chr(34) & CurrentProject.Path & "\backupfile.bat" & Chr(34)

Call Shell(stAppName, 1)


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

On 5/15/2011 5:33 AM, bsn wrote:
"Vacuum Sealed"<mclind1 (AT) bigpond (DOT) com> skrev i en meddelelse
news:jWGzp.4248$aH5.1130 (AT) viwinnwfe02 (DOT) internal.bigpond.com...
Hi Bjarne

I fairly certain you have to wrap this line:

stAppName = C:\Documents and Settings\Bjarne\Desktop\backupfile.bat

stAppName = "C:\Documents and Settings\Bjarne\Desktop\backupfile.bat"

Thx for answer...
I tried this, but with same result...
Strange problem, when i click on the .bat file on the Desktop it all
works
fine...
Bjarne




Reply With Quote
  #6  
Old   
Douglas J Steele
 
Posts: n/a

Default Re: VBA - batch file - 05-15-2011 , 10:24 AM



Actually, I believe you need to use the command line interpreter in order to
run batch files.

Try:

stAppName = Chr$(34) & Environ(%comspec%) & Chr$(34) & " " & _
Chr$(34) & CurrentProject.Path & "\backupfile.bat" & Chr$(34)
Call Shell(stAppName, 1)

Because Chr$(34) puts double quotes around the filename, there's no need to
worry about it being less than 8 characters.

"bsn" wrote in message
news:4dcfeec5$0$56780$edfadb0f (AT) dtext02 (DOT) news.tele.dk...

Thx for answer...
Ur suggestion are not working - same result...
No I did this way, with same result as u write about ...
"""" & CurrentProject.Path & "\bufile.bat" & """"

As u see i have shorten the filename to under 8 chars to...
another hint i found on the internet...
but its still not working...8-(
Bjarne



"John Spencer" <JSPENCER (AT) Hilltop (DOT) umbc> skrev i en meddelelse
news:iqom3d$3bm$1 (AT) dont-email (DOT) me...
Quote:
Just to confirm.
You did something like the following to wrap the string in quotes.

stAppName = Chr(34) & CurrentProject.Path & "\backupfile.bat" & Chr(34)

Call Shell(stAppName, 1)


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

On 5/15/2011 5:33 AM, bsn wrote:
"Vacuum Sealed"<mclind1 (AT) bigpond (DOT) com> skrev i en meddelelse
news:jWGzp.4248$aH5.1130 (AT) viwinnwfe02 (DOT) internal.bigpond.com...
Hi Bjarne

I fairly certain you have to wrap this line:

stAppName = C:\Documents and Settings\Bjarne\Desktop\backupfile.bat

stAppName = "C:\Documents and Settings\Bjarne\Desktop\backupfile.bat"

Thx for answer...
I tried this, but with same result...
Strange problem, when i click on the .bat file on the Desktop it all
works
fine...
Bjarne




Reply With Quote
  #7  
Old   
bsn
 
Posts: n/a

Default Re: VBA - batch file - 05-15-2011 , 12:37 PM



Thx for answer...
Unfortunately its still not working...
Ur suggestion gives me a dos window with a prompt who says "C:\Documents"...
Bjarne


"Douglas J Steele" <NOSPAM_djsteele (AT) NOSPAM_gmail (DOT) com>
Quote:
Actually, I believe you need to use the command line interpreter in order
to run batch files.

Try:

stAppName = Chr$(34) & Environ(%comspec%) & Chr$(34) & " " & _
Chr$(34) & CurrentProject.Path & "\backupfile.bat" & Chr$(34)
Call Shell(stAppName, 1)

Because Chr$(34) puts double quotes around the filename, there's no need
to worry about it being less than 8 characters.

"bsn" wrote in message
news:4dcfeec5$0$56780$edfadb0f (AT) dtext02 (DOT) news.tele.dk...

Thx for answer...
Ur suggestion are not working - same result...
No I did this way, with same result as u write about ...
"""" & CurrentProject.Path & "\bufile.bat" & """"

As u see i have shorten the filename to under 8 chars to...
another hint i found on the internet...
but its still not working...8-(
Bjarne



"John Spencer" <JSPENCER (AT) Hilltop (DOT) umbc> skrev i en meddelelse
news:iqom3d$3bm$1 (AT) dont-email (DOT) me...
Just to confirm.
You did something like the following to wrap the string in quotes.

stAppName = Chr(34) & CurrentProject.Path & "\backupfile.bat" & Chr(34)

Call Shell(stAppName, 1)


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

On 5/15/2011 5:33 AM, bsn wrote:
"Vacuum Sealed"<mclind1 (AT) bigpond (DOT) com> skrev i en meddelelse
news:jWGzp.4248$aH5.1130 (AT) viwinnwfe02 (DOT) internal.bigpond.com...
Hi Bjarne

I fairly certain you have to wrap this line:

stAppName = C:\Documents and Settings\Bjarne\Desktop\backupfile.bat

stAppName = "C:\Documents and Settings\Bjarne\Desktop\backupfile.bat"

Thx for answer...
I tried this, but with same result...
Strange problem, when i click on the .bat file on the Desktop it all
works
fine...
Bjarne





Reply With Quote
  #8  
Old   
Benny Andersen
 
Posts: n/a

Default Re: VBA - batch file - 05-15-2011 , 03:09 PM



try with:
cmd /k
or
...Environ(%comspec%)& " /k" ...


Yoy will have to close the window manually - or else finalize the
command with
"&pause&exit"

I believe it's also possible to use a 'silent' version:
cmd /c

but then the second parameter yo the shell call shall be
vbMinimizedFocus (2)

Reply With Quote
  #9  
Old   
bsn
 
Posts: n/a

Default Re: VBA - batch file - 05-16-2011 , 12:30 AM



Thx for answer...
....Environ(%comspec%) & " /k"... gives an error :
"File not found"
Bjarne

"Benny Andersen" <a.mail.user (AT) gmail (DOT) com> skrev i en meddelelse
news:4dd032e8$0$307$14726298 (AT) news (DOT) sunsite.dk...
Quote:
try with:
cmd /k
or
...Environ(%comspec%)& " /k" ...


Yoy will have to close the window manually - or else finalize the command
with
"&pause&exit"

I believe it's also possible to use a 'silent' version:
cmd /c

but then the second parameter yo the shell call shall be vbMinimizedFocus
(2)

Reply With Quote
  #10  
Old   
Phil
 
Posts: n/a

Default Re: VBA - batch file - 05-16-2011 , 04:11 AM



On 16/05/2011 06:30:31, "bsn" wrote:
Quote:
Thx for answer...
...Environ(%comspec%) & " /k"... gives an error :
"File not found"
Bjarne

"Benny Andersen" <a.mail.user (AT) gmail (DOT) com> skrev i en meddelelse
news:4dd032e8$0$307$14726298 (AT) news (DOT) sunsite.dk...
try with:
cmd /k
or
...Environ(%comspec%)& " /k" ...


Yoy will have to close the window manually - or else finalize the command
with
"&pause&exit"

I believe it's also possible to use a 'silent' version:
cmd /c

but then the second parameter yo the shell call shall be vbMinimizedFocus
(2)


Try ShellWait(CurrentProject.Path &""\backupfile.bat", true)

The bit up to the "Public Function ShellWait" needs to go in the
declarations. You probably don't need the word "PtrSafe"

Phil

'This code was originally written by Terry Kreft.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Terry Kreft
Private Const STARTF_USESHOWWINDOW& = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare PtrSafe Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long

Public Function ShellWait(PathName As String, Optional WindowStyle As Long)
As Integer 'ShellWait ("C:\Program Files\ZipGenius 6\ZG.Exe -add " &
"""C:\Documents and Settings\Phil\My Documents\Access\MDB\WFYC\WFYCTab2007 02
September 2008 09 49.zip""" & " C5 +" & """C:\Documents and Settings\Phil\My
Documents\Access\MDB\WFYC \WFYCTab2007.mdb""")

Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
Dim ret As Long

' Initialize the STARTUPINFO structure:
With Start
.cb = Len(Start)
If Not IsMissing(WindowStyle) Then
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = WindowStyle
End If
End With
' Start the shelled application:
ret& = CreateProcessA(0&, PathName, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, Start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)

ShellWait = ret ' 1 for OK, 0 for failes

End Function

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.