I would like to upload a file to my ftp server if internet is connected
In each run, i prefer:
if (ftp server can be connected){
upload the file "C:\abc.txt" to the ftp server directory "/ABC_DB"
}
Thus, I am not sure how to check ftp connection, is it possible to run like this:
echo abc > C:\abc.txt
???Check the connection here
OPEN your.ftp.server.com
usernameabc
passwordbcd
CD /ABC_DB
PUT "C:\abc.txt"
QUIT
PAUSE
Sorry for asking such silly question, but I am new in batch, hope u can help me =[
Test this: change lines 2,3,4 with your details
#echo off
set "name=your_ftp_user-name"
set "password=your_ftp_password"
set "server=ftp_server_name"
ping %server% |find /i "TTL=" >nul || (echo server offline, aborting&pause&goto :EOF)
set "ftpScript=%temp%\%~nx0.ftp.tmp"
(
echo open %server%
echo %name%
echo %password%
echo bin
echo CD /ABC_DB
echo PUT "C:\abc.txt"
echo quit
) > "%ftpScript%"
ftp -i -s:"%ftpScript%"
del "%ftpScript%"
Related
I use a windows batch file that scans download folder and if it detects label.pdf it sends to local printer. This works fine most of the time but often it detects file before the file completely downloads which causes PDFtoPrinter error. Is there a way to make sure the download process is complete before attempting to detect?
#echo off
cls
:start
IF EXIST "C:\Users\Downloads\label.pdf" (
echo "Found!"
ping 127.0.0.1 -n 1 -w 3000> nul
"C:\Users\Downloads\PDFtoPrinter" label.pdf "4BARCODE 4B-2054A"
ping 127.0.0.1 -n 1 -w 10000> nul
del "C:\Users\Downloads\label.pdf"
) ELSE (
echo "it isn't here!"
)
goto start
You can use the mechanism to check if a file is locked for exclusive access by another application.
#echo off
cls
:start
IF EXIST "C:\Users\Downloads\label.pdf" (
REM check if file is locked
2>nul (>>"C:\Users\Downloads\label.pdf" (call )) && ("C:\Users\Downloads\PDFtoPrinter" label.pdf "4BARCODE 4B-2054A") || (echo file is locked)
) ELSE (
echo "it isn't here!"
)
Timeout /T 10
goto start
So I am trying to write a small batch program to copy files over ftp to another device
The problem is the devices I am copying to are all different servers, but the information i'm copying stays the same.
How can I write this so when I open the batch program, i specify the IP address of the device, and the batch will connect to the server automatically and copy the directories or files i need copied.
Currently it will allow me to input the IP, connect to the server and open a specific file, but every time it tries to connect to copy files it says invalid directory or incorrect server.
:Log
set /p PDTFTP= Enter PDT IP Address:
start "ftp://admin:2p0d0t7#%PDTFTP%/pub/IPSM/fds/log/PDTApplicationLog.txt"
:DB
set /p PDTFTP= Enter PDT IP Address:
xcopy "C:\test.txt" "ftp://admin:2p0d0t7#%PDTFTP%/pub/IPSM/fds/"
Is there a way to do this when the ftp server will be different almost every time its used
You could do it like this:
to download the file:
#echo off
set /p ip=IP:
echo username> temp.txt
echo password>> temp.txt
echo get fileToGet>> temp.txt
echo quit>> temp.txt
ftp -s:temp.txt %ip%
del temp.txt
to upload the file:
#echo off
set /p ip=IP:
echo username> temp.txt
echo password>> temp.txt
echo put fileToUpload>> temp.txt
echo quit>> temp.txt
ftp -s:temp.txt %ip%
del temp.txt
I'm writing a batch script for a Windows 7 machine. The goal of the script is to move files from the directory C:\directory\source_dir\ to an ftp server ftpserver.domain.com.
Not all the files should be uploaded to the ftp server, so I'm using regular expression.
File Structure:
C:\directory\source_dir\TF_directory1
C:\directory\source_dir\TF_directory1\file1.txt
C:\directory\source_dir\TF_directory1\file2.txt
C:\directory\source_dir\TF_directory1\sub_dir\file_A.txt
C:\directory\source_dir\Ignore_directory\not_important.txt
C:\directory\source_dir\TF_123.CAM555.abc
C:\directory\source_dir\TF_123.CAM123.zyx
C:\directory\source_dir\TF_987.CAM555.abc
C:\directory\source_dir\wrong_file.txt
From the above structure TF_directory1 and everything inside should be uploaded. As should the files TF_123.CAM555.abc, TF_123.CAM123.zyx and TF_987.CAM555.abc.
Here's my problem:
The ftp put command returns an error on the directory
connected to ftpserver.domain.com
220 Welcome to the ftp server
ftp> user USERNAME
331 Please specify the password
---> PASS PASSWORD
230 Login successful.
ftp>
ftp> cd new_files
250 Directory successfully changed.
---> CWD new_files
ftp> put C:\directory\source_dir\"TF_123.CAM555.abc"
---> PORT 10,X,X,X,4,240
200 PORT command successful. Consider using PASV.
---> STOR TF_123.CAM555.abc
150 Ok to send data.
226 File receive OK.
ftp> put C:\directory\source_dir\TF_123.CAM123.zyx"
---> PORT 10,X,X,X,4,240
200 PORT command successful. Consider using PASV.
---> STOR TF_123.CAM555.abc
150 Ok to send data.
226 File receive OK.
ftp> put C:\directory\source_dir\TF_987.CAM555.abc"
---> PORT 10,X,X,X,4,240
200 PORT command successful. Consider using PASV.
---> STOR TF_123.CAM555.abc
150 Ok to send data.
226 File receive OK.
ftp> put C:\directory\source_dir\"TF_directory1"
**Error opening local file C:\directory\source_dir\TF_directory1.**
ftp> quit
---> QUIT
221 Goodbye.
Script:
set base_dir=C:\directory\
set log_dir=%base_dir%source_dir\
set log_file=%base_dir%log_file.txt
::Function to check if the ftpinfo exists. If not, create it.
:createFTPinfoFile
echo ########################## entering function :createFTPinfoFile
if not exist %base_dir%ftpinfo.dat (
echo %timestamp% -- Creating ftpinfo.dat file at location %base_dir% >> %log_file%
echo user USERNAME> %base_dir%ftpinfo.dat
echo PASSWORD>> %base_dir%ftpinfo.dat
echo %timestamp% -- Created ftpinfo.dat >> %log_file%
) ELSE (
echo %timestamp% -- %base_dir%ftpinfo.dat was not properly removed - Removing the file >> %log_file%
del %base_dir%ftpinfo.dat
echo %timestamp% -- Creating ftpinfo.dat file at location %base_dir% >> %log_file%
echo user USERNAME> %base_dir%ftpinfo.dat
echo PASSWORD >> %base_dir%ftpinfo.dat
echo %timestamp% -- Created ftpinfo.dat >> %log_file%
)
echo ############################ finished :createFTPinfoFile
EXIT /B 0
:addFilesToFTPinfo
echo ############################ entering function :addFilesToFTPinfo
set num=0
echo %timestamp% -- Starting to add files from %log_dir% to ftpinfo.dat >> %log_file%
for /f "delims=" %%i in ('forfiles /p %log_dir% /m "TF_*.CAM*.*" /d -0 -c "cmd /c echo put %log_dir%#file >> %base_dir%ftpinfo.dat & echo 1" ^| find /c /v ""') do set /a num=%%i-1
echo %timestamp% -- Starting to add folders from %log_dir% to fptinfo.dat >> %log_file%
for /f "delims=" %%i in ('forfiles /p %log_dir% /m "TF_*" /d -0 /c "cmd /c if #isdir==TRUE echo put %log_dir%#file >> %base_dir%ftpinfo.dat & echo 1" ^| find /c /v ""') do set /a num=%num%+%%i-1
echo %timestamp% -- added everything to ftpinfo.dat >> %log_file%
echo ############################ finished :addFilesToFTPinfo
EXIT /B 0
REM::This function creates the connection to the ftp server using the information from ftpinfo.dat
:ftpUploadFiles
echo ########################## entering function :ftpUploadFiles
if exist %base_dir%ftpinfo.dat (
echo cd new_files >> %base_dir%ftpinfo.dat
CALL :addFilesToFTPinfo
echo quit >> %base_dir%ftpinfo.dat
echo %timestamp% -- Connecting to FTP server to upload files >> %log_file%
ftp -n -s:%base_dir%ftpinfo.dat ftpserver.domain.com
)
echo ########################## finished :ftpUploadFiles
EXIT /B 0
Does anyone know a better way to do this?
The Windows command-line ftp.exe client does not support recursive operations.
If you want to transfer folders, you have three options:
Do all the hard work in the batch file, generating ftp upload commands for all files and folders. While doeable, this is pretty difficult to implement.
Use an ad-hoc solution for your specific folder(s), like the answer by #SamDenty shows.
Easiest is to use a 3rd party command-line FTP client. Most 3rd party FTP clients do support recursive operations.
For example with WinSCP FTP client, you can use a script like:
open ftp://username:password#ftp.example.com/
put TF_directory1
put TF_123.CAM555.abc
put TF_123.CAM123.zyx
put TF_987.CAM555.abc
exit
And run the script (ftp.txt) from a batch file like:
winscp.com /script=ftp.txt
See the guide for converting Windows FTP script to WinSCP script.
(I'm the author of WinSCP)
You are using put to transfer a folder, but put doesn't support transferring folders, only files as stated by:
**Error opening local file C:\directory\source_dir\TF_directory1**
Instead of using put, try using:
mkdir TF_directory1
cd TF_directory1
mput C:\directory\source_dir\TF_directory1\*
Which would:
Make a directory on the FTP server named TF_directory1
CD into the directory
Copy all files from the TF_directory1 and place them in the new FTP folder
Reference -
Superuser
Good day all.
I've done a batch script that connects to an FTP and download a file on the same location of the .bat file. everything works fine, now, is there a way to "ask for file overwrite permissions"? I mean, if the user already have a file named in the same way, is it possible to prevent overwrite and ask him what to do? the code actually is :
#echo off
echo user MYUSERNAME> ftpcmd.dat
echo MYPASSWORD>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo cd /www.website.com/>>ftpcmd.dat
echo get afile.txt>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat ftp.website.com
del ftpcmd.dat
You can use if exist "afile.txt" in advance and use choice to let the user decide what to do:
#echo off
if exist "afile.txt" (
choice /C YN /M "Overwrite 'afile.txt'? "
) else (
> nul ver & rem (clear `Errorlevel`)
)
if not ErrorLevel 2 (
> "ftpcmd.dat" (
echo user MYUSERNAME
echo MYPASSWORD
echo bin
echo cd /www.website.com/
echo get afile.txt
echo quit
)
ftp -n -s:ftpcmd.dat ftp.website.com
del "ftpcmd.dat"
)
However, since there is no -i switch in the ftp command line and the interactive prompt mode should be on as per default, I assume the ftp command would prompt the user anyway...
Corrective Update: The "interactive mode" means that the mget and mput will ask user to confirm transfer of each matching file, not matter if it exists or not. See technet.microsoft.com/en-us/library/bb490670.aspx. There are no overwrite confirmations in ftp.exe. [Martin Prikryl]
Use the if exist command to test, if the local file exists already before the transfer.
If it exists, ask an user for confirmation.
#echo off
if not exist afile.txt goto download
:ask
echo Overwrite?
set INPUT=
set /P INPUT=Y/N: %=%
If /I "%INPUT%"=="Y" goto download
If /I "%INPUT%"=="N" exit
echo Incorrect input
goto ask
:download
...
ftp -n -s:ftpcmd.dat ftp.website.com
Though for a long term solution, you better use PowerShell, instead of hacking this in a batch file.
unsuccessful trying to connect directly into subfolder of ftp.
it showing
unknown host ftp://ftp2.xxx.com/test/949010. need help please.thanks
SET "Server=ftp://ftp2.xxx.com/test/949010"
SET "UserName=xxx"
SET "Password=x:nj*~A+"
SET "Commands=%TEMP%SendToFTP_commands.txt"
ECHO %UserName%> %Commands%
ECHO %Password%>> %Commands%
ECHO binary >> %Commands%
ECHO put "C:\Users\Desktop\Processed\*" >> %Commands%
REM Close the FTP connection.
ECHO close >> %Commands%
ECHO bye >> %Commands%
REM Perform the FTP.
FTP -d -i -s:%Commands% %Server%
ECHO.
ECHO.
pause
REM Clean up.
IF EXIST %Commands% DEL %Commands%
You should use mput *.* (multiple put), if you have many files to upload
Here are a list of commands you can use.
And of course change this line :
SET "Commands=%TEMP%SendToFTP_commands.txt"
to this :
SET "Commands=%TEMP%\SendToFTP_commands.txt"
I include below to the subfolder
ECHO %UserName%> %Commands%
ECHO %Password%>> %Commands%
REM FTP subfolder of in/
ECHO cd in/%%X>> %Commands%
ECHO binary >> %Commands%