binary FTP transfer of .txt file -- Unix to Windows - shell

I am trying to ftp a text file from a Unix machine to a Windows machine. In the process, the data is getting mangled. I want to try transfering the file in binary instead of ASCII - hopefully that will make the data pass thru without getting corrupted.
How can I modify the following script to make the .txt file transfer as a binary file, instead of ASCII? I am not familiar with the syntax.
#echo off
setlocal
set uname=john
set passw=password
set hostname=johncomputer
set filespec=SampleSPEC
echo %uname%> name.ftp
echo %passw%>> name.ftp
echo cd CRMD>> name.ftp
echo get %filespec%>> name.ftp
echo bye>> name.ftp
ftp -s:name.ftp %hostname%
if errorlevel 1 pause
endlocal

Try: echo TYPE I >> name.ftp before the line that adds the get command.

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

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

Downloading file from server over FTP

I need to download a file that is placed in a folder called abc_20140221_123456 in server1 over ftp to my local directory. The problem is that the last six characters of the folder name are not fixed. For example today the folder may be called abc_20140221_123456 and tomorrow it might be called abc_20140221_234567. I am having problems in writing an automation batch script to do the same.
Here's the script I am working on:
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
echo open server1>>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
echo lcd E:\>>file.tmp
:: Not sure how to cd to abc_20140221_* from here
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I know that I can loop through directories using for like this:
for /d %%d in (' %path%/*%folderName%* ') do (
echo get filename.txt>>file.tmp
echo y>>file.tmp
)
But "for" doesn't work inside ftp>.
Any help is highly appreciated. Thanks.
I am assuming that for a given date there is only one abc_YYYYMMDD_nnnnnn directory.
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
set root=E:
echo open server1>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
for /D %%D in (%root%\%foldername%??????) do set target=%%D
echo lcd %target%>>file.tmp
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I set root to E:\ based on your original script - you can change it to whatever the actual parent directory is. I also changed
echo open server1>>file.tmp
to
echo open server1>file.tmp
so it creates a new temp file. Otherwise, it will keep appending to file.tmp each time the script runs. If that is what you want, or if you are generating a unique script file name each time and were just using file.tmp as a placeholder, ignore that change and use >> as before.
If there is more than one folder for a given day, I believe this script will use whichever one comes up last when the wildcards are expanded. I did not test this out, however.

To search for a file at FTP

I want to find if a file exists at FTP using if-exist filename -else statement using FTP batch script which is as follows:
ftp.txt open ftp.mysite.com
ftp.txt username
ftp.txt password
ftp.txt if exist filename (echo file exists) else (echo file doesn't exist)
ftp.txt quit
ftp -s:ftp.txt
the if-exist line above does not work.
Is there any other way to search?
Don't do the logic in the FTP script.
Call the ftp.txt script from a batch file. Within your ftp.txt script, just do a GET on your file. If the file is there, it'll be downloaded to the local directory. Otherwise, it won't. After calling the FTP script, check the file's existence in your local directory using standard DOS batch commands, i.e.:
#echo off
:FETCHFILE
ftp -s:ftp.txt
IF EXIST filetocheckfor.txt (
REM the file was there, so do something
) ELSE
echo Trying again...
REM ping "sleep" would go here
GOTO FETCHFILE
)
If you want to build a delay into your retries, perform a "sleep" by pinging a bogus IP address, as described in this post:
http://www.dullsharpness.com/2010/06/14/elapsed-timer-using-pure-ms-dos/

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