I must log in to multiple servers and upload some files. Is there a chance to code this with AutoIt?
I have tested it with an batch file, but doesn't work:
echo open < /FTP/ftphost.txt
echo < /FTP/ftpuser.txt
echo < /FTP/ftppass.txt
echo cd C:\Dokumente und Einstellungen\Administrator\Desktop\FTP\dateien>>ftp.txt
echo cd httpdocs>>ftp.txt
echo mput test.txt test2.txt
echo bye
p
in the ftphost.txt File should be the diffrent FTP Servers
in the ftpuser.txt file the diffrent ftp users
in the ftppass.txt file one password.
If you use a file with the FTP information like this (and each FTP server has the same folder structure)
FTPlist.txt
ftp.host1.com|username1|password1
ftp.host2.com|username2|password2
ftp.host3.com|username3|password3
ftp.host4.com|username4|password4
Then this batch file can upload the set of files to each FTP server:
#echo off
for /f "usebackq tokens=1,2,3 delims=|" %%a in ("FTPlist.txt") do (
(
echo.open %%a
echo.%%b
echo.%%c
echo.binary
echo.lcd "C:\Dokumente und Einstellungen\Administrator\Desktop\FTP\dateien"
echo.cd httpdocs
echo.mput test.txt test2.txt
echo.bye
) >ftp.script
ftp -i -s:ftp.script >> ftp.log
)
del ftp.script
Related
I am writing a quick batch to see if a file exists after executing an exe.
The file is created with a YYYYmmDDnumbernumbernumber.xml file name according to the current date.
How do I check for the file with a variable in the beginning? Here is what I have so far:
#echo off
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
if not exist "ftp://FTPsite/%mydate%*.xml" (echo nah) else (echo yea)
pause
you cannot check existence of file on ftp server with IF.Instead try this after replacing the parameters that starts with MY_ :
!cls&echo off&setlocal ENABLEDELAYEDEXPANSION
!cls&goto :ftp_end
open MY_FTP_SERVER
user MY_USER
pass MY_PASS
cd MY_REMOTE_DIR
ls . local.file
bye
:ftp_end
ftp -s:%0
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
type local.file | findstr /B "%mydate%" | find ".xml" && echo FILE IS OUT THERE && goto :skip_file_is_not_there
echo FILE IS NOT THERE
:skip_file_is_not_there
del local.file /q >nul
I am trying to create a batch file that gets a zipped folder for that particular date (v_date) from the sftp site and then unzip them. The zip file contains five files that are text documents. I have written batch scripts that successfully get the zip file from the remote site and save a copy of it on the local drive. I need to incorporate the unzip part in to my script.
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
echo option batch continue>FTP_File_Get.txt
echo option confirm off>>FTP_File_Get.txt
echo open Target>>FTP_File_Get.txt
echo lcd "M:\Development\Data History\File" >> FTP_File_Get.txt
echo cd /Export/File >> FTP_File_Get.txt
echo get /Export/File/Filename_%v_date%.zip "M:\Development\DataHistory\Filename_%v_date%.zip">>FTP_File_Get.txt
echo exit>>FTP_File_Get.txt
M:\temp\apps\WinSCP\winscp.com/script="M:\Development\SFTPBatchFiles\FTP_File_Get.txt"
del FTP_File_Get.txt
This is my code to UNZIP:
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
cd "M:\Development\Data History\"
::SET v_file="M:\Development\Data History\Filename_%v_date%.zip"
::unzip -o %v_file%
"C:\Program Files\7-Zip\7z.exe" e "Filename_%v_date%.zip"
I need to move the extracted files (6 Files) into their respective folders, Any help is greatly appreciated
To unzip the files you can use this command line:
"C:\Program Files\7-Zip\7z.exe" e "filename.zip"
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Use the above code by saving it to .bat file extension
Remember %userprofile% is for the directory, %programfiles% for the program files set as a variable in windows
hope that helps
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)
I'm trying to run a batch file that will connect to the list of ip's on my network, open up cmd then run a list of commands: like ipconfig /all, nbstat -c, arp -a. Then it must save the results into a folder renamed as that "computername".
I already have a batch file made that can do the commands I want and create a folder with the computer, then input the different commands into txt files within that folder.
Here is the WindowsCommands batch file:
md %computername%
echo off
echo ARP Command
arp -a >> %cd%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %cd%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %cd%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %cd%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %cd%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %cd%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %cd%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %cd%\%computername%\prefetch.txt
exit
I also created a hosts.txt file that contains my local Ip addresses that I want to run the commands on.
I also created another batch file name psexec for running a For loop.
Now my troubles and arising when trying to run the psexec batch file.
Here is my psexec file:
for /f %%a in (hosts.txt) do (
psexec \\%%a C:\Users\ISSG\Documents\WindowsCommands.bat
)
Now that is just a rough draft I'm not entirely sure if that is how it should be coded. This is one of the first automated scripts I have ever wrote.
So in a nutshell i need to be able to run this batch file from my local computer- psexec into the IP's. Gather the information and output it into txt files on my local computer.
If anyone could point me in the right direction that would be great!
Thanks!
If your Batch file doesn't exist in the system directory on all of the computers, you have to use the -c switch to copy it to them in order to run it. It's a good idea to try to ping the computer first to save time trying to connect to it.
for /f %%a in (hosts.txt) do (
for /f "tokens=2 delims=:" %%b in ('ping -n 1 %%a ^| find "TTL="') do (
if errorlevel 0 (
psexec \\%%a -c -f -u username -p password C:\Users\ISSG\Documents\WindowsCommands.bat
)
)
)
Also, keep in mind that this will create the folder in the System32 directory on the remote computer. If you want it on your local drive, do something like this:
#echo off
FOR /F "tokens=2" %%A IN (
'net use * "\\computer\share"'
) DO IF NOT %%A.==command. SET dl=%%A
if not exist "%dl%\%computername%" md "%dl%\%computername%"
echo ARP Command
arp -a >> %dl%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %dl%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %dl%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %dl%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %dl%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %dl%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %dl%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %dl%\%computername%\prefetch.txt
Net use %dl% /delete
I need to upload zip files to an FTP server.
My first file looks like this:
sqlcmd -S .\SQLEXPRESS -U <user> -P <pass> -i c:\sql_script.sql
7za a -tzip %~dp0\Archive\Backup_daily_full.zip *.bak -v100m
For uploading a single file I created this:
#echo off
echo user {user}> ftpcmd.dat
echo {pass}>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat {server}
I don't know how many .zip files I will have after the backup and how to upload all of them on FTP (how to call that file within main .bat) OR how to simply upload them all at once.
You can loop over all the .zip files in the folder, and run the ftp script for each of them.
#echo off
for /r %~dp0\Archive %%a in (*.zip) do (
echo user {user}> ftpcmd.dat
echo {pass}>> ftpcmd.dat
echo put %%a>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat {server}
)
Add a third line to your first file:
cmd /c <your-ftp-script>.bat <zip-file-name>
As for traversing through a list of zip files, do as the manual says (cmd /?):
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.
So store your zipped backup files at a clean location, then use for /r to go through the list, calling your second bat file in a loop.