简体   繁体   中英

Unable to pass command line arguments to a batch file

I've a batch file that is opening a FTP connection to a server and putting a file on a specified location.

Here is how my ftpConnection.bat file look like..

open HOST
FTP_USER_NAME
FTP_USER_PASSWD
cd TO_DIR
lcd TO_FILE_LOCATION
put THE_FILE
quit

and from command prompt if i run it like this ftp -i -s:ftpConnection.bat it works fine.

My requirement is to pass HOST, USER_NAME and PASSWORD as argument

so i tried to use %1 %2 and %3 but this is not working for me.

Passing the argument like this

C:\Users\xxx\Desktop>ftp -i -s:ftpConnection.bat "HOST" "USER_NAME" "PASSWORD" 

also tried without the quotes but result is same, it'S showing

Transfers files to and from a computer running an FTP server service (sometimes called a daemon). Ftp can be used interactively.

FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuf fer] [-b:asyncbuffers] [-w:windowsize] [host]

Followed and tried few document like How to pass multiple parameters in CMD to batch file and Batch file command line arguments

they suggested to use set and i tried it like below but result is same.

set host=%1
set uname=%2
set passwd=%3
open %1
%2
%3

Can anyone suggest me what i am doing wrong or any pointer to achieve this.

Thanks in advance.

Sorry, but you are using a text file with input and commands expected and interpreted by ftp command. This is not a windows batch file and can not use its syntax.

To use the required data as arguments in the call, you need a batch file that will generate the ftp script from the passed arguments

@echo off
  setlocal enableextensions disabledelayedexpansion

  if "%~3"=="" goto :eof

  set "TO_DIR=......"
  set "TO_FILE_LOCATION=......"
  set "THE_FILE=........"

  > ftpScript (
      echo open %~1
      echo %~2
      echo %~3
      echo cd %TO_DIR%
      echo lcd %TO_FILE_LOCATION%
      echo put %THE_FILE%
      echo quit
  )

  ftp -i -s:ftpScript 

Now, you have a batch file (let's call it doFTP.cmd ), that you can call as

doFTP.cmd ftpserver userName pa$$word

The arguments in the command line are stored in the %1 , %2 and %3 variables ( %~1 is first argument without surrounding quotes if present) and used inside the batch file to generate (the block of echo commands are redirected to the ftpScript file) the text file that ftp will handle.

based on your use it is like

set host=%1
set uname=%2
set passwd=%3
open %host%
%username%
%passwd%

you can use the set variable back by enclosing between two %var-name% as i shown above please give it a try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM