Unable to pass command line arguments to a batch file - windows

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

Related

Rename a file on the remote FTP host to the output of a command (Windows)

I'm connecting to an FTP server of my website using cmd.exe (on Windows 7). However, when I want to rename a file on the remote host, I'm able to only rename it to a static text (first word, if there's multiple words separated by space)
What I want to do is renaming the file to the output of a command (dynamic). For example the output of the command date is
The current date is: Sat 06/18/2016
I want the name of the file to be the result of the date command.
Thanks in advance!
Start by collecting the command output to a variable.
Then, use the variable to generate the ftp.exe script on the fly.
FOR /F "tokens=* USEBACKQ" %%F IN (`date /t`) DO (
SET OUTPUT=%%F
)
echo open ftp.example.com>ftp.txt
echo user>>ftp.txt
echo password>>ftp.txt
echo rename current_name %OUTPUT%>>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt

Please explain following outputs in Windows command prompt

I tried to customize windows command prompt with the following batch file.
#echo off
cls
:cmd
set /p "cmd=%cd%>"
%cmd%
goto cmd
So, when I open the batch file, it just takes my command into cmd variable and executes it and again prompts for a new command.
But the following command echo %cd% outputs only %cd%
Then I enabled delayedexpansion and used echo !cd! and got the desired output.
I think, because of the delayed expansion, cmd variable now holds echo c:\Users\Sourav\Desktop (am I correct?)
But I got confused when I tried to open the command prompt (not the batch file) and tried the following commands.
I thought, I will get c:\Users\Sourav\Desktop but I got !cd!. This contradicts my understanding of how echo !cd! is working in first case.
Why am I getting different output in the second case?
Can anyone suggest any improvement to the batch file, so that I can get desired output just using echo %cd% in first case?
you need another level of parsing. You can use call to do so:
#echo off
cls
:cmd
set /p "cmd=%cd%>>"
call %cmd%
goto cmd

Single input variable for windows send to .bat

I want to get a framemd5 via ffmpeg by right clicking a file, selecting "send to", then selecting my.bat file.
Using this allows me to make framemd5s for every file in a directory:
for %%a in ("*.*") do C:\Users\bla\Downloads\avanti-092\Avanti-ffmpeg-GUI-092\ffmpeg\ffmpeg-20150513-git-51f6455-win64-static\ffmpeg-20150513-git-51f6455-win64-static\bin\ffmpeg.exe -i "%%a" -f framemd5 "%%~na.framemd5"pause
I want to just run the bat file on a single file, the one I right clicked on to begin with. I do not know which variable to use.
A Send-To command (the batch file in your case) receives the selected file(s) as command line argument(s), which can be accessed in a batch file with %1, %2, %3 and so on.
%0 is the (path to the) batch file itself. Type call /? to get more information.
For applying that to a single file use %1. This syntax supports ~ modifiers like for variables. For instance, %~1 removes surrounding double-quotes, %~n1 extracts the file name only.
For your batch file, you actually don't need the for command (which executed one iteration only).
The following should therefore work:
C:\Users\bla\Downloads\avanti-092\Avanti-ffmpeg-GUI-092\ffmpeg\ffmpeg-20150513-git-51f6455-win64-static\ffmpeg-20150513-git-51f6455-win64-static\bin\ffmpeg.exe -i "%~1" -f framemd5 "%~n1.framemd5" & pause

Command Prompt: Execute Commands from Any Text File (Not Having ".bat" or ".cmd" Extensions)

I can't find any way to do, for example, the following:
cmd.exe /C "script.txt"
In other words, I need Command Prompt to (try) to execute file with any extension (not necessarily .bat or .cmd) if it contains valid batch script code. I'm looking for behavior similar to Unix shells:
./script.txt
While on Unix the shebang (#!/bin/sh) is responsible for understanding that the file is actually a script, it seems like on Windows .bat or .cmd extensions play the same role, indicating a batch script file for Command Prompt.
Is it possible to avoid that and force Command Prompt to interpret a file with any name?
NOTE: Please, no answers like:
Give your file .bat or .cmd extension.
That's not what the question is about.
This depends on the complexity of the NON-Batch file. If the NON-Batch file does not use these facilities:
Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
Execution of GOTO command.
Execution of CALL :NAME command (internal subroutine).
Execution of SETLOCAL/ENDLOCAL commands.
then you may execute any file as a "Batch file" via this trick:
cmd < anyFile.ext
Further details at this post
you first need an "installation" script :
#echo off
rem :: A files with .TEST extension will be able to execute batch code but is not perfect as the %0 argument is lost
rem :: "installing" a caller.
if not exist "c:\caller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\caller.bat
rem :: associating file extension
assoc .test=batps
ftype batps=c:\caller "%%1" %*
then try a simple .test file:
#echo off
for /l (1;1;10) do (
echo testing .TEST extension
)
In fact ASSOC and FTYPE both have immediate effect so you can start a .test file right after "installation". With direct editing of the registry eventually you can get more control -> How to create file extension that behaves as .cmd/.bat? . Check also drop handlers -> http://msdn.microsoft.com/en-us/library/windows/desktop/cc144165%28v=vs.85%29.aspx

DOS script tp FTP from a remote server and copy files to local folder

I need to write a single dos script which will first FTP from a remote server to my local directory and then will copy these files to another folder within the local machine. The reason behind the second step is that the destination folder path is based on whether the SYSTEM Processor of 32-bit or 64-bit.
I have two separate scripts which are working fine now. But when I am putting them together in a single script, it is failing with the ftp command prompt. Here is the script which I am using -
#echo off
:MAIN
SETLOCAL ENABLEDELAYEDEXPANSION
set winver=%PROCESSOR_ARCHITECTURE%
rmdir c:\batch\temp
mkdir c:\batch\temp
goto :ftpbegin
:ftpbegin
#ftp -i -s:"%~f0"&GOTO:EOF
open x.y.z.a
<userid>
<password>
!:-----FTP commands here -----
lcd C:\batch\temp
cd CGServices\Uploads
binary
mget "*.psl"
mget "*.PST"
mget "*.psy"
get abc.ini
get def.pmd
disconnect
bye
:eof
exit /b
:findwin
if %winver% ==x86 (goto :copywin32) else (goto :copywin64)
:copywin32
echo "inside copywin32"
<do the copy files here>
:copywin64
<do copy files here>
exit /b 0
However, the ftp script seems to cause a break while executing the second part of my program since it is calling the program in a loop in ftp prompt. So, none of the dos commands are translated on FTP prompt.
Any help on how to achieve this in a single script for this is highly appreciated.
thanks,
Sanders
The FTP command AFAIK cannot process the commands from the command line without reading the ftp commands from an external file. I usually download a windows version of wget, and it works well for us, perhaps you would like to have a look at that.
Here is the manual for wget http://www.editcorp.com/Personal/Lars_Appel/wget/v1/wget_7.html
for example
wget ftp://<user>:<password>#server.com/upload/file.ext
After invoking ftp command you tell your script to jump to ':EOF'. You can not use :EOF label for jumping inside a batch file. Goto :EOF basically means "jump to the end of the file", which is generally the same as calling exit /b. You can also call goto :EOF from inside a subroutine. In this case it mean "this is the end of the subroutine". For more info, see goto /? and call /?
Because in ftp.exe you are using your .bat file as a ftp command script, all the lines before open x.y.z.a will give you invalid command messages, so it would be good to minimize the number of lines before FTP command block.
Your script should look like this:
#echo off
goto main
:ftpbegin
#ftp -i -s:"%~f0" & goto ftpend
open x.y.z.a
<userid>
<password>
!:-----FTP commands here -----
lcd C:\batch\temp
cd CGServices\Uploads
binary
mget "*.psl"
mget "*.PST"
mget "*.psy"
get abc.ini
get def.pmd
disconnect
bye
:main
SETLOCAL ENABLEDELAYEDEXPANSION
set winver=%PROCESSOR_ARCHITECTURE%
rmdir c:\batch\temp
mkdir c:\batch\temp
goto ftpbegin
:ftpend
if not "%winver%"=="x86" goto copywin64
REM did not jump, so this is a 32-bit system
echo "inside copywin32"
<do copy files here>
goto finish
:copywin64
echo "inside copywin64"
<do copy files here>
:finish
endlocal

Resources