tags:

views:

334

answers:

2

I am using c#.NET 2.0 to execute DOS commands to ftp a file. All works except for 1 thing, in the cmd file I call, it runs a PUT statement. Right now the put statement has a hardcoded local file path. I need to specify a dynamic path. I've tried

put %~dp0\myfile.DTL myfile.dtl

but it says it can't find the file.

Right now the .NET code calls a BAT file which only exist to call the CMD file. Interestingly, the BAT file DOES successfully use a relative path in its call to the CMD file:

ftp.exe -s:%~dp0\oit.cmd 

However, I can't get that relative path to wrok in the cmd file:

open <my host> 
<user name> 
<password> 
put <hardcoded path that needs to be relative path>localfilename remotefilename

I'll bever know where it will exist so I just need to grab whatever local directorey the file is in.

A: 

Use System.IO.Directory.GetCurrentDirectory() to get the current (working) folder.

Alternatively, if you want the path relative to your .EXE file, you can use System.Reflection.Assembly.GetEntryAssembly().CodeBase to get full path to your .EXE file, then use System.IO.Path.GetDirectoryName() to get the directory name of that path.

After that, you can use System.IO.Path.Combine to get absolute path out of relative:

string absPath = Path.Combine( @"c:\working\folder", @"sub\folder\file.ext" );
// absPath == "c:\working\folder\sub\folder\file.ext

// Works with double-dot too:
string absPath2 = Path.Combine( @"c:\working\folder", @"..\up\file.ext" );
// absPath == "c:\working\up\file.ext
Fyodor Soikin
Thank you, but I really would need the DOS code which would receive this.
donde
A: 

Relative is "." (dot).

Can you post the exact situation? What directory are you in, and where is the file?

Be careful with the "~" char ... it has a special meaning for DOS files (8.3 notation)

belisarius
Right now the .NET code calls a BAT file which only exist to call the CMD file. Interestingly, the BAT file DOES successfully use a relative path in its call to the CMD file:ftp.exe -s:%~dp0\oit.cmdHowever, I can't get that relative path to wrok in the cmd file:open <my host><user name><password>put <hardcoded path that needs to be relative path>localfilename remotefilename
donde
Ohhh .. so the % sign indicates that dp0 is a variable in the DOS shell. You have to know the actual value. You can add an ECHO command in the BAT file for that
belisarius
Add the following line to the bat file before the ftpECHO %~dp0%
belisarius
Not needed. "%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name."http://www.computerhope.com/forum/index.php?topic=54333.0
belisarius