Upload files to FTP, using .BAT, only once - batch-file

I have automated file (.doc) uploads to an FTP server through BATCH files. I then run the BATCH file through the task scheduler every few minutes.
The below two batch files do the work for me:
upload.bat :-
open ftp.servername.com
username
password
cd FOLDER_NAME
binary
put D:\TEST\*.doc
bye
the above .bat file is called by the below .bat file,
startupload.bat :-
ftp -i -s:upload.bat
Now, the client removes files from the FTP once they are uploaded.
So, with above batch files, files are getting repeatedly uploaded.
Hence, my requirement is that each .doc file should be uploaded only once,
(OR,
may be, once a file is successfully uploaded to an FTP, it is shifted to another folder.)
Please help.
Thank you.

You can echo the directions into a text file then use -s?
Like the attached image
Then copy them to another folder and delete the originals?
#echo off
echo open serveraddress >ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo cd FOLDER_NAME>>ftp.txt
echo binary >>ftp.txt
echo "LCD D:\TEST\"
echo mput *.doc>>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt
del /f /q ftp.txt
copy "D:\TEST\*.doc" "C:\otherFolderPath\"
del /f /q "D:\TEST\*.doc"
::NOTES:
:: you can use ">>" to put the output of a command into a text file.
:: you can use ">" to put the output of a command into a text file. ">" Will clear a file if it exists, and will create a new file if it does not.
Note: If you want it to wait some time and then run again you can add this to the end of the script: Timeout /t 60 (That will wait 60 seconds, or until a user presses a key to continue)

Related

CMD/FTP to create folder using to today date & connect ftp download into created folder

I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B

bzip2 - Bzipping all files inside folders (Windows)

I have a bzipping tool on my computer, but it only bzips files that are inside the "compress" directory. How would I make it so files inside all directories inside the compress directory are zipped?
Example
compress/image.png goes to compress/image.png.bz2
however
compress/folder/image.png stays as compress/folder/image.png
My batch file is as follows:
#echo off
title bzip
echo bzip
echo All files within /compress will be compressed as a .bz2
echo.
echo Compressing file(s)...
bzip2.exe -z compress/*.*
echo.
echo Compression Completed!
pause
I hope somebody can help me!
Edit:
When running the process with directories inside the compress directory, it says "permission denied".
Use for /r compress %%i in (*) do bzip2.exe "%%i" in your batch file instead of the call to bzip2.exe directly. bzip2 almost certainly doesn't know how to recurse through subfolders -- standard wildcard globbing libs on Windows generally don't.
Run for /? from a Command Prompt to see more about the syntax of the for command. If you want to test the command from a prompt instead of a batch file, use 1 percent sign for the variable instead of 2.

delete a file from local drive after successful upload to ftp using batch file

i want to delete file from my local system after successful send to ftp using batch file. for scheduling purpose i m using window scheduler. below is my code which is able to post to ftp.how to delete that successful send to ftp otherwise file shld not delete.
%windir%\system32\ftp.exe -s:%~f0
goto done
cd C:\
open Host Name
user_name
password
bi
put user_input.csv
bye
:done
#echo off
cls
exit
if i will write delete here then it ll delete from remote server.pls suggest me how to do that using window ftp.exe
You need to redirect the output of the FTP command. Check for a succesful message and act upon it.
I can't provide an exact script, because it will depend on the FTP command you actually use, but the idea is like this.
echo open Host Name >%temp%\ftpin.txt
echo user_name >>%temp%\ftpin.txt
echo password >>%temp%\ftpin.txt
echo put user_input.csv >>%temp%\ftpin.txt
echo bye >>%temp%\ftpin.txt
ftp -n -s:%temp%\ftpin.txt >%temp%\ftpout.txt
for /f "tokens=* skip=9" %%a in (ftpout.txt) do (
if "%%a"=="226 Transfer complete." (
del user_input.csv
)
)
EDIT: You will have to adjust your BAT to the actual output of your FTP script. Probably you will need to change the "skip" parameter of the FOR command.

Batch script to wait for file to be generated on FTP server and download

I have a program in my FTP server to generate a file, which may take 3-5 minutes to complete and also I knew the name of the file which i being created by my program. Now, once I initiate the program in my server, I have keep checking until the file is created. Once it is created, I am using the below batch script to ftp the file to my local desktop.
#ftp -i -s:"%~f0"&GOTO:EOF
open 10.100.16.111
username
password
lcd c:\
cd root/output_folder
binary
mget "*partial_file_name*" REM mget using wildcard search
disconnect
bye
This script works fine for me. But the problem is, I need modify this script as such, script should keep running until the file is generated. Because i don't know when the file creation will get completed. So, it will great if some one help/guide me to make a looping script which will wait until the completion of file creation and download the same file through FTP.
With this edit you can launch the batch file with the file name on the command line, like this:
ftpscript.bat "filename.ext"
Note that your lcd uses c:\ which is a restricted location in later versions of windows.
#echo off
>file.tmp echo open 10.100.16.111
>>file.tmp echo username
>>file.tmp echo password
>>file.tmp echo lcd c:\
>>file.tmp echo cd root/output_folder
>>file.tmp echo binary
>>file.tmp echo mget "%~1"
>>file.tmp echo disconnect
>>file.tmp echo bye
:retry
ftp -i -s:"file.tmp"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
del file.tmp
pause
More elegant solution is to use an FTP client that supports parametrized scripts or commands on command-line, such as WinSCP, to avoid creating a temporary script file.
Parametrized script
The batch file would be more or less identical as with the Windows ftp:
#echo off
:retry
winscp.com /script=script.txt /parameter "%~1"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
The ftp script converts to following WinSCP script:
open ftp://username:password#10.100.16.111/
lcd c:\
cd root/output_folder
get -transfer=binary "%1%"
exit
Commands on command-line
You can also inline the commands the to the batch file:
#echo off
:retry
winscp.com /command ^
"open ftp://username:password#10.100.16.111/" ^
"lcd c:\" ^
"cd root/output_folder" ^
"get -transfer=binary ""%~1""" ^
"exit"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
References:
Automating file transfers to FTP server;
Upload to multiple servers / Parametrized script.
Had you ever need to upgrade to the FTPS or the SFTP, just modify the session URL in the open command command accordingly.
(I'm the author of WinSCP)

Creating a PDF file tracker as Batch file

I have created a batch file which is supposed to keep track of the opened PDF files on my system.As soon as someone opens the PDF file,the name of the file as well as time of access is recorded in a log file.
Here is code of my batch file:
#echo off
echo FILE ACCESSED %1 >> I:\Batch\log.txt
echo TIME OF ACCESS %TIME% >> I:\Batch\log.txt
"C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe" %1
EXIT
I have also set up this batch file as the default application fr the PDF files to open with.
Now,the problem is,as soon as I open any PDF file,many cmd windows start opening and closing unendingly. Please suggest some remedy.
Thanks....
#echo off
copy %1 %1.tmp
echo FILE ACCESSED %1 >> I:\Batch\log.txt
echo TIME OF ACCESS %TIME% >> I:\Batch\log.txt
"C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe" %1.tmp
del %1.tmp
EXIT

Resources