I am having problems with a batch script that should zip some pdf files and upload them to a FTP server.
My script looks like this:
#echo off
set d=%date:~0,2%%date:~3,2%%date:~6,4%
set d=%d: =_%
set t=%time:~0,2%%time:~3,2%%time:~6,2%
set t=%t: =0%
set file="C:\Program Files (x86)\7-Zip\7z.exe" a -r 46730_%d%_%t%.zip *.pdf
echo open my.host.name>> temp
echo myusername>> temp
echo mypassword>> temp
echo ascii>> temp
echo cd /
echo binary
echo put %file%>> temp
echo close>> temp
echo quit>> temp
ftp -s:temp
del temp
TIMEOUT /T 5
The zip files is created OK with the correct name, and I am able to open it afterwards. I'm pretty sure the problem(s) lies in the upload part of the script, but I can't find any working solution on google :-( The reciever is not able to open the zip file. He gets this error: Could'nt open the file as an archieve.
What am I doing wrong ??
You are missing to redirect the "CD /" and "Binary" instruction to the temp file...
And you are trying to set a command instruction in the "file" variable...
try this:
#echo off
set d=%date:~0,2%%date:~3,2%%date:~6,4%
set d=%d: =_%
set t=%time:~0,2%%time:~3,2%%time:~6,2%
set t=%t: =0%
Start /W "C:\Program Files (x86)\7-Zip\7z.exe" a -r "46730_%d%_%t%.zip" "*.pdf"
set "file=46730_%d%_%t%.zip"
(
echo open my.host.name
echo myusername
echo mypassword
REM Use a correct dir command, "cd /" isn't openning any folder
REM echo cd ".\folder\"
echo binary
echo put "%file%"
echo close
echo quit
)> "%TEMP%\ftp.txt"
ftp -s:"%TEMP%\ftp.txt"
TIMEOUT /T 5
If you have problems you better can use the WPUT command:
https://sourceforge.net/projects/wput/files/wput/pre0.6/wput-pre0.6.zip/download?use_mirror=freefr&download=
C:\Users\Administrador\Desktop>wput --help
Usage: wput [options] [file]... [url]...
url ftp://[username[:password]#]hostname[:port][/[path/][file]]
Startup:
-V, --version Display the version of wput and exit.
-h, --help Print this help-screen
Logging and input file:
-o, --output-file=FILE log messages to FILE
-a, --append-output=FILE append log messages to FILE
-q, --quiet quiet (no output)
-v, --verbose be verbose
-d, --debug debug output
-nv, --less-verbose be less verbose
-i, --input-file=FILE read the URLs from FILE
-s, --sort sorts all input URLs by server-ip and path
--basename=PATH snip PATH off each file when appendig to an URL
-I, --input-pipe=COMMAND take the output of COMMAND as data-source
-R, --remove-source-files unlink files upon successful upload
Upload:
--bind-address=ADDR bind to ADDR (hostname or IP) on local host
-t, --tries=NUMBER set retry count to NUMBER (-1 means infinite)
-nc, --dont-continue do not resume partially-uploaded files
-u, --reupload do not skip already completed files
--skip-larger do not upload files if remote size is larger
--skip-existing do not upload files that exist remotely
-N, --timestamping don't re-upload files unless newer than remote
-T, --timeout=10th-SECONDS set various timeouts to 10th-SECONDS
-w, --wait=10th-SECONDS wait 10th-SECONDS between uploads. (default: 0)
--random-wait wait from 0...2*WAIT secs between uploads.
--waitretry=SECONDS wait SECONDS between retries of an upload
-l, --limit-rate=RATE limit upload rate to RATE
-nd, --no-directories do not create any directories
-Y, --proxy=http/socks/off set proxy type or turn off
--proxy-user=NAME set the proxy-username to NAME
--proxy-pass=PASS set the proxy-password to PASS
FTP-Options:
-p, --port-mode no-passive, turn on port mode ftp (def. pasv)
-A, --ascii force ASCII mode-transfer
-B, --binary force BINARY mode-transfer
--force-tls force the useage of TLS
See the wput(1) for more detailed descriptions of the options.
Mail bug reports and suggestions to <itooktheredpill#gmx.de>
Related
I have multiple .sql script files and want to execute them one by one in batch file with proper logging mechanism.
I wrote below batch command to solve the problem
#ECHO OFF
SET SQLCMD="C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\SQLCMD.EXE"
SET PATH="C:\Users\sql_scripts\"
SET SERVER="server_name"
SET DB="database"
SET LOGIN="user_name"
SET PASSWORD="password"
SET OUTPUT="C:\Users\sql_scripts\output\OutputLog.txt"
CD %PATH%
ECHO %date% %time% > %OUTPUT%
for %%f in (*.sql) do (
%SQLCMD% -S %SERVER% -d %DB% -U %LOGIN% -P %PASSWORD% -i %%~f -b >> %OUTPUT%
)
But it has few problems that I want to solve as listed below :
I want to create dynamic out put file names based on input file name. Example if input sql file
name is "test.sql" then I want to create output file in "output" folder with name "test.sql".
Currently It writes all the log to one file i.e. OutputLog.txt
I want to show output log with script of execution to identify which script produced what
output. Let's say in one script file 5 scripts present then in output file I want to show the
script and its output log.
Currently, the below For/Do loop goes through all SQL files that are in the same folder as the batch file itself and outputs the CSVs to a user-specified folder (%OUTPUTFOLDER% is actually created earlier in the batch file as a subfolder of %SCRIPTFOLDER%, which is specified by the user):
FOR /F "tokens=*" %%S IN (
'DIR /B "%SCRIPTFOLDER%\*.sql" '
) DO (
echo Reading scripts from: %SCRIPTFOLDER%\*.sql
echo Script: %%~fS
echo Output: %OUTPUTFOLDER%\%%~nS.csv
sqlcmd -b -S %INSTANCE% -d %DATABASE% -i "%%~fS" -s "|" -o "%OUTPUTFOLDER%\%%~nS.csv" -W
IF ERRORLEVEL 1 GOTO errorhandling
ECHO %%~nS.csv successfully created
)
For a visual example, if I had the folder structure
C:\Extract\extract.bat
C:\Extract\Scripts\
C:\Extract\Output\
The variable %SCRIPTFOLDER% is set by the user and is the folder that, you guessed it, holds the scripts. But the batch file has to be in that folder, too. I need to change this so that the scripts do not have to be in the same folder as the batch file. I.e. the user can specify both %SCRIPTFOLDER% and %OUTPUTFOLDER%
Due to the output of echo Script: %%fS, I'm guessing that's what I need to change - possibly what's in the FOR line as well, but I'm not seeing how exactly to do that.
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)
I am running a batch script and trying to create a txt file with the file information. Below is what I have written. The PROJECT_PATH AND REMOTE_FTP_OUTPUT_PATH can change depending on what is assigned to the variable in the script. I am getting errors. Here is the part of the script to create the file:
echo **** PROJECT PATH IS *** %PROJECT_PATH%
echo **** directory location is ***** dir
REM ***** Creat ftp.txt dynamically *********************************
echo open clientftp.accuweather.com > !PROJECT_PATH!\ftp.txt
echo user>> %PROJECT_PATH%\ftp.txt
echo accubot >>%PROJECT_PATH%\ftp.txt
echo HpD7f1sTymq2 >>%PROJECT_PATH%\ftp.txt
echo cd download\mobile_builds\android\v3 >>%PROJECT_PATH%\ftp.txt
echo if not exist %REMOTE_FTP_OUTPUT_PATH% mkdir %REMOTE_FTP_OUTPUT_PATH% >>%PROJECT_PATH%\ftp.txt
echo cd download\mobile_builds\android\v3\%REMOTE_FTP_OUTPUT_PATH% >>%PROJECT_PATH%\ftp.txt
echo put %APK_FULL_OUTPUT% >>%PROJECT_PATH%\ftp.txt
echo quit >>%PROJECT_PATH%\ftp.txt
and here is my output when I run for those lines:
**** PROJECT PATH IS *** temp_builds\6228-10.06
**** directory location is ***** dir
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
Error opening script file temp_builds\6228-10.06\ftp.txt.
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:recvbuffer] [-b:asyncbuffers] [-w:windowsize] [host]
-v Suppresses display of remote server responses.
-n Suppresses auto-login upon initial connection.
-i Turns off interactive prompting during multiple file
transfers.
-d Enables debugging.
-g Disables filename globbing (see GLOB command).
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
-a Use any local interface when binding data connection.
-A login as anonymous.
-x:send sockbuf Overrides the default SO_SNDBUF size of 8192.
-r:recv sockbuf Overrides the default SO_RCVBUF size of 8192.
-b:async count Overrides the default async count of 3
-w:windowsize Overrides the default transfer buffer size of 65535.
host Specifies the host name or IP address of the remote
host to connect to.
Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.
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)