We help IT Professionals succeed at work.

Batch file execution

martynlath
martynlath asked
on
295 Views
Last Modified: 2010-04-13
i have been running a program in VB that zips a bunch of files from different locations and ftp's the resulting file to multiple ftp sites. I have the program execute a batch file which runs several txt files within the ftp protocal.

Recently win 2k was updated on the network, now the dos window will only stay alive for less then a second before it is destroyed. From what i can tell, none of the commands are run anymore (tested this using a simple net send to my own machine)

The app works fine still on XP, any ideas???

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
How are you calling the batch file?
Precede it with
 CMD /C
if not already...

To test it though- precede with CMD /K (window stays open)...

Author

Commented:
Shell (App.Path & "\ftp.bat")

Author

Commented:
could you be more clear on where to put CMD /K or /C please?



CERTIFIED EXPERT
Top Expert 2007

Commented:
Try shell ("CMD /K " & App.Path & "\ftp.bat")
This should at least leave the window open so you can determine where it's failing...
Can you post ftp.bat here or is it lengthy?

Author

Commented:
batch file example:

cd\
ftp -s:"C:\PROJECT-WORK\PROGRAMS\COMBINED-ZIP-FTP\myscript.txt"
ftp -s:"C:\PROJECT-WORK\PROGRAMS\COMBINED-ZIP-FTP\myscript2.txt"
ftp -s:"C:\PROJECT-WORK\PROGRAMS\COMBINED-ZIP-FTP\myscript3.txt"
exit

example of txt file:

open 69.93.83.218
anonymous
me@you.com
binary
cd pub
cd work
put "C:\Documents and Settings\Profile 1\Desktop\dailyupdate.zip"
bye

Author

Commented:
the problem appears to be that the batch file is being called from a directory with a space in the name. Any ideas how to stop make the whole path into a string in vb.

Dim t As String
t = """"
Shell ("CMD /K " & t & App.Path & t & "\ftp.bat")

This does not appear to work and i am unsure of another solution

Author

Commented:
the reason the file batch file above worked is because there is no spaces in the file name. when testing it on the win 2k machine it became evident.
CERTIFIED EXPERT
Top Expert 2007

Commented:
A couple of different ways to do it - but probably the easiest would be

Shell ("CMD /C " & chr(34) & app.path & "\ftp.bat" & chr(34))
This will enclose the string in quotes...

Author

Commented:
that did not seem to improve the issuse.

it still stops at "C:/Program"

its trying to find the program files directory
CERTIFIED EXPERT
Top Expert 2007

Commented:
Strange - that works for me...
Could you paste that line here - also, what does App.path resolve to?
CERTIFIED EXPERT
Top Expert 2007

Commented:
This code should generate the LFN for you...
'[BEGIN CODE]
Option Explicit

Const MAX_PATH = 206

Private Declare Function GetModuleHandle Lib "kernel32" _
    Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
   
Private Declare Function GetModuleFileName Lib "kernel32" _
    Alias "GetModuleFileNameA" (ByVal hModule As Long, _
    ByVal lpFileName As String, ByVal nSize As Long) As Long

Private Declare Function GetWindowsDirectory Lib "kernel32" _
    Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long
   
Private Declare Function GetSystemDirectory Lib "kernel32" _
    Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long

Private Declare Function GetShortPathName Lib "kernel32" _
    Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long


Private Sub Form_Load()
 Dim myHandle As Long, myResult As Long, strLFNPath As String
 strLFNPath = Space$(MAX_PATH)
 myHandle = GetModuleHandle(App.EXEName)
 myResult = GetModuleFileName(myHandle, strLFNPath, MAX_PATH)
 MsgBox strLFNPath
End Sub
'[END CODE]

Author

Commented:
app.path = C:\program files\temp files
lfn = C:\program files\temp files\vbzip.exe
bat file = C:\program files\temp files\ftp.bat
 

Author

Commented:
although it says thats the error, ive noticed that the prompt points at the correct directory. so if we can carrage return the file name part. maybe that will improve the situation?
CERTIFIED EXPERT
Top Expert 2007

Commented:
vbcrlf will do that...but I'm not certain I follow what you meant there...
The shell prompt from cmd /k brings you to the right path?

Author

Commented:
yes. although it suggests the error is in the path. though it wont run the file name. but if you type it in manually after it runs the batch file
CERTIFIED EXPERT
Top Expert 2007

Commented:
martynlath,
Try specifying the full path for ftp in your batch file...
Something like

%systemroot%\system32\ftp -s "C:\PROJECT-WORK\PROGRAMS\COMBINED-ZIP-FTP\myscript.txt"

Author

Commented:
good news got it working. though CMD /C doesnt run. Any idea why?

i ran it without the app.path. as it is already in the directory by default.
easy fix for that issuse :-)

/c doesnt appear to run at all though. not even in system processes.

nearly there..
CERTIFIED EXPERT
Top Expert 2007

Commented:
CMD /C doesn't run?  Have you tried it just from a command line?
Do you get an error?  Probably irrelevant now though.
When you say nearly there... what's left?

Author

Commented:
correction. it needs the app path. it will only run first time otherwise. then the path is incorrect afterwards
CERTIFIED EXPERT
Top Expert 2007

Commented:
So, it's still not working?  I'm a bit confused now... :(

Can you step through your code and try a debug.? of the following line:

debug.? "CMD /C " & chr(34) & app.path & "\ftp.bat" & chr(34)

Thanx.

Author

Commented:
the APP  works correctly the first time round. but from then on the folder path cannot be changed. keeps saying internal or external command not recognised. i even tried spliting the commands up over 3 batch files

Dim d, e, f As String
d = Left$(App.Path, 2)
e = Right$(App.Path, (Len(App.Path) - 3))
f = Left$(e, (Len(e) - 8))

'8 = length of "\ftp.bat"

Shell ("CMD /K" & d & "|" & "EXIT" & "|")
Shell ("CMD /K" & "cd/" & "|" & "EXIT" & "|")
Shell ("CMD /K" & "cd/" & f & "|" & "EXIT" & "|")
Shell ("CMD /K " & d & "/" & f & "ftp.bat")

 

i need a piece of code that will get me to that file given any senario, including the prompt starting on a different drive letter. I believe my close, but not quite sure what im doing wrong.

Commented:
Instead of cmd /c in scripting use %COMSPEC% /C
This is a environmental varialble for your cmd.exe file. Better to use this when the script will
be run on multiple OS types.
What is the exact path to the files you want to access?
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hmm - I'd hate to see this one trashed after all the time put into it...
martynlath - any update?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.