Solved

# Running FTP command from a batch file

Posted on 2010-08-19
1,479 Views
I did try to call a Batch file from VB.Net using the shell command. This works perfectly & the command from the VB.NET application is as follows:

Shell("C:\daemons\getslicer1.bat",vbNormalFocus).

The getslicer1.bat file is this :

"ftp -s:getslicer1.ftp"

This is ok too.
The ftp file which is invoked by the above bat file is :

open 192.168.0.21
hc
cd C:\STATEX
delete statex_slicer1.txt
rename statex.txt statex_slicer1.txt
get statex_slicer1.txt
quit

Which works too most of the time. When I say most of the time I mean that sometimes when the batch file is called the location where the .ftp file is searched by the .bat file is changing!!!!
So The shell command calls the getslicer1.bat file from C:\daemons and  the getslicer1.ftp file is located in the C:\ directory so it's executed however sometimes if I pause the batch file I can see that the batch command in the command line is looking to a different directory !!!
I can't understand why as nobody and nothing changed anything in NEITHER the FTP or BAT code but yet when it executes it tries to look for the ftp file in a different location.
I think that I should force the batch file to search for the fpt in a certain location.
When I did first run the getslicer1.bat file using the shell command  it looked by default in C:\ so I assumed that this is the default.
However if I click on the bat file inside the :C\daemons where it is located it will show an error (if I pause the code) as it looks for the ftp inside the C:\daemons.
So obviously the location where the batch file will look for the FTP file depends on the method it is invoked.
If invoked by shell from VB.NET looks for the ftp in the C:\ but if run locally it looks for the file in the same directory where it was initiated from.However if called by the SHELL command the location where the bat file tries to run the ftp file is chnaging based on god knows what.

0
Question by:Latzi_Marian
• 3
• 3
• 3
• +1

LVL 51

Assisted Solution

HainKurt earned 100 total points
ID: 33481244
use full path for ftp file and you are good to go

"ftp -s:getslicer1.ftp"
-->
"ftp -s:drive:\full\path\to\getslicer1.ftp"
0

Author Comment

ID: 33481292
Thanx for your answer. I will try this right now however  the question is why the SHELL command will change the directory where the bat file is "looking" for the ftp file?
0

LVL 16

Accepted Solution

t0t0 earned 300 total points
ID: 33481459
It's like this:

You can run a batch file that is located ANYWHERE on your system by:

(1) specifying it's full path along with the filename

(2) by navigating to the directory where the batch file exists, then just refering to it by name

The difference is, the batch file will only see files currently 'Working Directory': where YOU are - not where the batch file is.

When you do a CD command on it's own, DOS returns the current working directory showing you where you are.

NOTE: Normally, because the default prompt is '$p$g', your current working directory is always shown as part of the DOS prompt. You can change this by entering the following command:

PROMPT $g or even: PROMPT %username%$+\$g

In which case, you'll occasionally need to verify where you are by entering just CD on it's own (or even ECHO %cd%).

Whatever CD returns is your current working directory.

No matter where you batch file is located, if it needs to refer to any files, it will ALWAYS ONLY look in the current working directory UNLESS...

Unless you specify the file's full path as part of it's filename (so that the batch file knows where to look for the file).

Therefore, GetSlicer1.bat needs to look like this:

ftp -s:c:\getslicer1.ftp

(if GetSlicer1.ftp is located in the c:\ directory).
0

LVL 16

Expert Comment

ID: 33481465
Sorry, I meant:

The difference is, the batch file will only see files in the current 'Working Directory' (where YOU are - not where the batch file is).
0

LVL 51

Expert Comment

ID: 33481494
where is your executable located? maybe it is getting the running path from operating system, maybe last used command path...
0

Author Comment

ID: 33481869
The executable is in a different directory yet again. It is fixed now as I set the path in the batch file but still don't quite understand what switched the path. Nothing was running on the PC when this happened.
0

LVL 51

Expert Comment

ID: 33481902
if it is fixed now, dont worry be happy ;)
0

LVL 43

Assisted Solution

Steve Knight earned 100 total points
ID: 33482770
BTW if you want it to always pick up from the path where the batch file is stored even if the working directory is something else then you get the directory of the batch file using:

%~dp0

So for instance in your batch file

@echo off
echo The drive was %cd%
cd /d "%~dp0"
echo Now it is the batch file ( %~nx0) directory %cd%

Or just add the path to the script file on the ftp line.
Or use a different method to make the ftp scrript on the fly (the all one one file method from here:)

Steve
0

Author Closing Comment

ID: 33484339
Thank you all guys.it's all tested and working.It's pretty clear what happened and why.Thanx again.
0

LVL 16

Expert Comment

ID: 33488588
Thank you. Glad you got it sorted. Easy as pie and chipes isn't it?
0

## Featured Post

### Suggested Solutions

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Outsource Your Fax Infrastructure to the Cloud (And come out looking like an IT Hero!) Relative to the many demands on today’s IT teams, spending capital, time and resources to maintain physical fax servers and infrastructure is not a high priority.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…