简体   繁体   English

无法将命令行参数传递给批处理文件

[英]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. 我有一个批处理文件,正在打开与服务器的FTP连接并将文件放在指定的位置。

Here is how my ftpConnection.bat file look like.. 这是我的ftpConnection.bat文件的样子。

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. 并且从命令提示符下,如果我像ftp -i -s:ftpConnection.bat这样运行它,它将正常工作。

My requirement is to pass HOST, USER_NAME and PASSWORD as argument 我的要求是传递HOST,USER_NAME和PASSWORD作为参数

so i tried to use %1 %2 and %3 but this is not working for me. 因此我尝试使用%1 %2 and %3但这对我不起作用。

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服务器服务的计算机(有时称为守护程序)之间传输文件。 Ftp can be used interactively. Ftp可以交互使用。

FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuf fer] [-b:asyncbuffers] [-w:windowsize] [host] FTP [-v] [-d] [-i] [-n] [-g] [-s:文件名] [-a] [-A] [-x:发送缓冲区] [-r:recvbuf文件] [- b:asyncbuffers] [-w:windowsize] [主机]

Followed and tried few document like How to pass multiple parameters in CMD to batch file and Batch file command line arguments 遵循并尝试了一些文档,例如 如何将CMD中的多个参数传递给批处理文件 批处理文件命令行参数

they suggested to use set and i tried it like below but result is same. 他们建议使用set ,我尝试如下所示,但结果相同。

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. 抱歉,但是您正在使用一个文本文件,该文件带有ftp命令期望和解释的输入和命令。 This is not a windows batch file and can not use its syntax. 这不是Windows批处理文件,不能使用其语法。

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 要将所需的数据用作调用中的参数,您需要一个批处理文件,该文件将从传递的参数中生成ftp脚本。

@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 ),您可以将其称为

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. 命令行中的参数存储在%1%2%3变量中( %~1是第一个参数,如果没有引号,则使用引号引起来),并在批处理文件中用于生成( echo命令块重定向到ftpScript文件),ftp将处理的文本文件。

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 您可以通过在上面显示的两个%var-name%之间括起来来使用set变量,请尝试一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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