# Running FTP command from a batch file

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.

###### Who is Participating?

Commented:
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

Sr. System AnalystCommented:
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

Systems IntegratorAuthor Commented:
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

Commented:
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

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

Systems IntegratorAuthor Commented:
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

Sr. System AnalystCommented:
if it is fixed now, dont worry be happy ;)
0

IT ConsultancyCommented:
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

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

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