473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Batch file doesn't run through Access

21
Hi everyone,
I see this issue repeated in a lot of forums, but so far no answers have worked for me.

I am trying to get Access to run a .bat file (also works as a .cmd file), using a simple Shell statement, but it isn't working - it seems to rush through it too fast. Starting the file manually outside of Access works perfectly.

The basic code:
Expand|Select|Wrap|Line Numbers
  1. Shell "C:\Documents and Settings\myname\My Documents\CLIENTS - ACTIVE\ftptransfer.cmd", vbNormalFocus
(In case it matters, the bat file logs in to our ftp server and downloads a specific file...)

I've tried adding various waits after the code, but it doesn't seem to make a difference, like:
Expand|Select|Wrap|Line Numbers
  1. Shell "C:\Documents and Settings\myname\My Documents\CLIENTS - ACTIVE\ftptransfer.cmd", vbNormalFocus
  2. Dim Counter
  3. Counter = 0
  4. While Counter < 300000000
  5.     Counter = Counter + 1
  6. Wend
  7. Debug.Print Counter
...and other "wait" type of solutions, but that doesn't seem to be the issue - the cmd window closes well before it starts the counter.

It seems like the code is moving on or ending before the batch file has time to run (it takes about 1 full second). Or is it another issue?
Apr 1 '11 #1
15 5008
Rabbit
12,516 Expert Mod 8TB
Try Shell """C:\Documents and Settings\myname\My Documents\CLIENTS - ACTIVE\ftptransfer.cmd""", vbNormalFocus
Apr 1 '11 #2
CarrieR
21
No, didn't work - the issue isn't in the quotes...
Apr 1 '11 #3
CarrieR
21
Actually, just found what is happening, and not sure why...

It seems like the bat is actually downloading the file to:
C:\Documents and Settings\myname\My Documents\

I have absolutely no idea why, unless all of the spaces in the file path is making things complicated?

I can fix with a line moving the file, but I sure would like to understand why this is happening (only when I run it with Access this happens....)
Apr 1 '11 #4
Rabbit
12,516 Expert Mod 8TB
Well, it doesn't matter that the function ends before the batch file finishes running. The problem is somewhere else. I have no problem with the code on a batch file that I created that takes 15 seconds. I assume you used myname on purpose to disguise your user ID?
Apr 1 '11 #5
ADezii
8,834 Expert 8TB
@CarrieR - try the API approach
Expand|Select|Wrap|Line Numbers
  1. Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
  2. ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
  3. ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Expand|Select|Wrap|Line Numbers
  1. Dim strFile As String
  2. Dim strAction As String
  3. Dim lngErr As Long
  4.  
  5. strFile = "C:\Documents and Settings\myname\My Documents\CLIENTS - ACTIVE\ftptransfer.cmd"
  6. strAction = "OPEN"
  7.  
  8. lngErr = ShellExecute(0, strAction, strFile, "", "", 0)
  9.  
  10. 'Optionally, add code to test lngErr
P.S. - The Shell Function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.
Apr 1 '11 #6
CarrieR
21
The API approach, like the simple shell script, is still putting it in
C:\Documents and Settings\myname\My Documents\
rather than the directory I specify in the bat file.
(and yes, it's not literally folder "myname")

From some more investigoogling, I'm guessing it has to do with the MS Access default settings (Access Options > Popular > Default database folder). Which seems like it's just a quirk to Access - unless anyone has an explanation/solution to it besides the obvious changing of the default settings.

In the meantime, I'm just going to "solve" the issue by moving the file after it puts it in the My Documents folder. And thanks for the reminder, ADezii, I'll make sure there's enough of a pause between actions, or just keep them separate.
Apr 1 '11 #7
NeoPa
32,556 Expert Mod 16PB
I do a lot of this, and the surprising thing for me so far is that the contents of the CMD file has neither been explained nor shown.

So far, I'm guessing with very limited information, but it seems to me you are expecting an FTP downloaded file to arrive in a particular place, but it ends up somewhere else instead. If so, the problem is almost certainly down to the contents of the CMD file (and any FTP file you use too of course). Not an Access issue per se, but I'm happy to have a look at them for you if you post them.

As far as the asynchronous nature of the Shell() call goes, I have a short article on how to get around that (ShellWait() Function) if that is necessary. It seems it probably is from my inference, but much is unclear here so I'm just guessing.

PS. To be clear, I do appreciate that every attempt has been made to give as much info as seemed to be needed. I expect the missing info was simply down to a lack of realisation as to its critical nature. I don't intend to criticise the question, or the OP, but simply to indicate where I suspect the problem(s) lie.
Apr 2 '11 #8
CarrieR
21
The cmd (or bat, either works the same) file hasn't been posted, because running alone, it posts perfectly to the correct spot. It is only if I run it with Access that it posts to the wrong place.

Therefore, it's an Access issue. Yes?
Apr 4 '11 #9
CarrieR
21
Though for completeness' sake, the bat/cmd file I run has this:
Expand|Select|Wrap|Line Numbers
  1. ftp -i -s:"script.txt"
script.txt has this written:
Expand|Select|Wrap|Line Numbers
  1. OPEN [ftp server name]
  2. [user name]
  3. [password]
  4. binary
  5. cd [ftp file location]
  6. MGET [file name]
  7. BYE
(Everything secure is in brackets, of course)

Runs perfectly each time, without fail. I'm not saying it's an Access "issue", just that it seems to go to Access default locations. This is something I've seen hinted at at other responses too.
Apr 4 '11 #10
NeoPa
32,556 Expert Mod 16PB
If I thought that were true I wouldn't have posted, so No. There are many reasons why it may work from one place and not another, some of which are not related directly to the fact that it is invoked from Access. I don't want to go to the trouble of thinking through all the possible reasons and explaining them to you when I could see it a lot more easily if I could see the contents of your files. Another piece of information that would be instructive would be a clear explanation of exactly how you run the files when you run them alone. If from the command line then please indicate the default folder run from. If a shortcut then those details also needs to be passed. While I'm about it, posting the VBA code you use to invoke it from the Access database would also be helpful.

Clearly this isn't such an obvious problem that you've been able to work it out yourself. Please don't expect me to do it without even any information to work with. I can assure you that I have never had problems getting FTP jobs to work correctly when called from Access, so it's not a problem that has no solution. Only the information is still missing.
Apr 4 '11 #11
CarrieR
21
OK, so it's posted. I guess I don't understand the question of how I run the batch file. I literally click it, it runs. The whole thing is posted above.

What information is missing?
Apr 4 '11 #12
NeoPa
32,556 Expert Mod 16PB
CarrieR:
Runs perfectly each time, without fail. I'm not saying it's an Access "issue", just that it seems to go to Access default locations.
You beat me to the punch there Carrie. I suspect you're right about it using the Access default locations, but your files can easily be updated such that this ceases to be an issue.

Forget the other info. I wanted to see this to confirm my suspicions and it has. I just need to know exactly where the file needs to be FTPed to. The extra line in the FTP file (script.txt) will use lcd (Local Change Directory). Always something you should use in FTP files unless you specifically want the destination to be relative to the calling position.
Apr 4 '11 #13
CarrieR
21
I tried adding a lcd statement before and after the one line of my batch file - both cases, it still went to the Access default directory (C:\Documents and Settings\myname\My Documents\)

Expand|Select|Wrap|Line Numbers
  1. lcd "C:\Documents and Settings\myname\My Documents\CLIENTS - ACTIVE\"
  2. ftp -i -s:"C:\Documents and Settings\myname\My Documents\CLIENTS - ACTIVE\script.txt"
Is there something else I need to do to get it to be recognized?
Apr 4 '11 #14
NeoPa
32,556 Expert Mod 16PB
You misread my post it seems. It's the correct command, but it needs to be in your FTP file (not the CMD file). See example below :
Expand|Select|Wrap|Line Numbers
  1. OPEN [ftp server name]
  2. [user name]
  3. [password]
  4. binary
  5. lcd "C:\Documents and Settings\myname\My Documents\CLIENTS - ACTIVE\"
  6. cd [ftp file location]
  7. MGET [file name]
  8. BYE
Apr 4 '11 #15
NeoPa
32,556 Expert Mod 16PB
CarrieR:
I guess I don't understand the question of how I run the batch file. I literally click it, it runs.
This info is no longer necessary, but I thought it might help to explain anyway.

There are many ways to invoke executable files of many descriptions. I can't go into all of them here as there really are too many to cover, but of the more common ones there are :
  1. Double-click a file from an Explorer window (I suspect this is what you're referring to). This will generally set the Default Folder (DF) to the folder the file is found in.
  2. Shortcuts and the older, DOS specific, PIF files can also be executed which generally contain within them what the DF is. These can be executed like any other file.
  3. Shortcuts specifically, can also be added to command type toolbars (such as the Quick Launch Toolbar). These require only a single click to invoke.
  4. Any executable file can also be run from a Command Prompt window. In this case, excepting shortcuts and PIFs, the DF is whatever is set for the Command Prompt at the time. To run something from here simply type the name and any parameters and hit enter.

The DF situation is so important for an old Command-Line command like FTP.EXE, as most of the transfer commands specify only the name(s), while the source and destination folders are as the defaults. The defaults can be changed (CD & LCD), but they start as :
Local Folder - DF from where FTP.EXE was called.
Remote Folder - Specified by the server and dependent on the credentials used.
Apr 4 '11 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bill | last post by:
I need help closing a CMD window when it is executed from Access. 1) The batch file is called from Access. 2) Access closes, 3) the batch runs a copy of the access database (creating a backup)...
26
by: mvdkwong | last post by:
What's the trick to running a batch file from VBA? I'm trying to call it using the Shell function but it's not working for me. If I double-click the batch file or run it from the command line it...
8
by: jvborg | last post by:
I am trying to run a .bat file from access. On the click of a search button (btnSearch) I incorperate data entered into a text box (txtSearch) to print a script into a text file (script.txt). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.