Move from FTP and count in Target folder automated

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move from FTP and count in Target folder automated

#151 Post by abc0502 » 22 Dec 2012 09:55

you can't make two ftp server download files in the same folder, it will make troubles.
beside, i don't really understand what you are trying to do, so first take the codes again from the forum, the last one that was working for you then add the code that create the zerobyte code and test for 1 ftp server then add another ftp server and test again.
by that you will understand what is wrong and it will be easy to fix then.

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: Move from FTP and count in Target folder automated

#152 Post by aaksar » 22 Dec 2012 10:39

I hv tested on 1 target folder iits working fine....

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: Move from FTP and count in Target folder automated

#153 Post by aaksar » 25 Dec 2012 22:42

i have tested, actually my FTP server is same, but i have given different folder name.

when i am giving diff. target folder its working fine and after copying all files creating zerobyte file, but when i am giving same target folder for both location, its looping,

i got your point , it wont work when i give 1 target folder for more than 1 ftp location/folder.

i have given

Code: Select all

Del /F /Q "%~6\*.%zipped%" 2>nul

but its still giving error, *.zip not found.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move from FTP and count in Target folder automated

#154 Post by abc0502 » 26 Dec 2012 02:50

just replace it with this:

Code: Select all

If Exist "%~6\*.%zipped%" Del /F /Q "%~6\*.%zipped%" 2>nul

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: Move from FTP and count in Target folder automated

#155 Post by aaksar » 18 Apr 2013 01:55

Hi,My FTP location is replaced with SFTP

i am making changes in script.
to run it through command prompt I am using PSFTP

so when i am taking the file list into 1 temp file its giving me error , I have tried different ways , but its not working
ssh_init: Host does not exist

file contents
-P 2200 65.2.1.36 -2
DWHT
password
cd "abcdir"
ls
disconnect
Bye


pls help me out

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move from FTP and count in Target folder automated

#156 Post by abc0502 » 18 Apr 2013 03:05

PSFTP not like the usual FTP, I didn't use PSFTP before so i don't have any experience in that but i made few changes based on the PSFTP help page.

Start.bat

Code: Select all

@Echo OFF
Title FTP Main Download Script

:: IF The batch dosen't downloading your files make sure to provide full location
:: of these files in "FTP.set" and "Code.bat" at this Batchfile "Start.bat"
:: also provide the full location of the "FILE.lstX" inside "FTP.set file

set C1=0
set C2=%RANDOM%
setlocal enabledelayedexpansion
For /F "skip=1 tokens=1-8 delims=;" %%A in ('Type "FTP.set"') Do (

   set /a C1 += 1
   set "file_list=%%A
   set "remote_server=%%B"
   set "remote_server_username=%%C"
   set "remote_server_password=%%D"
   set "Port=%%E"
   set "total_number=%%F"
   set "local_folder=%%G"
   set "remote_folder=%%H"
   
   IF exist "%temp%\!C2!" (
      Set /a C2 += 51
      MD "%temp%\!C2!" >nul
      ) Else ( MD "%temp%\!C2!" >nul )
   
   start "%%B" cmd.exe /c Code.bat "!file_list!" "!remote_server!" "!remote_server_username!" "!remote_server_password!" "!Port!" "!total_number!" "!local_folder!" "!remote_folder!" "!C2!"
   )
Added "Port" Variable


Code.bat

Code: Select all

@ECHO OFF

:: Don't Forget to Specify the Zipped file type in line 20

::"file_list" --------------------> %~1
::"remote_server" ----------------> %~2
::"remote_server_username" -------> %~3
::"remote_server_password" -------> %~4
::"Port_Number" ------------------> %~5
::"total_number" -----------------> %~6
::"local_folder" -----------------> %~7
::"remote_folder"  ---------------> %~8
::"FTP servers temp folder" ------> %~9

::Create Local Folder if not exist
::Echo Creating LOCAL FOLDER
IF not Exist "%~7" MD "%~7" >nul

:loop
set "FTPapp=C:\Program Files\PSFTP\PSFTP.exe"
set "zip_program=%programfiles%\7-Zip\7z.exe"
set "zipped=zip"

:: FTP commands to get list of files from the server
::Echo Creating Commands_1.ftp file
IF "%~8"=="" (
   (
   Echo ls
   Echo disconnect
   Echo Bye
   )>"%temp%\%~9\commands_1.ftp"
) Else (
   (
   Echo cd "%~8"
   Echo ls
   Echo disconnect
   Echo Bye
   )>"%temp%\%~9\commands_1.ftp"
   )

:: start FTP session and log output
::Echo Connicting to FTP server to get files list
"%FTPapp%" %~3@%~2 -pw %~4 -P %~5 -b "%temp%\%~9\commands_1.ftp" >"%temp%\%~9\dump.log"

:: extract file name from the ftp dump file
::Echo Processing dump.log File and creating server_list.lst
For /F "delims=" %%a in ('Type "%temp%\%~9\dump.log"') Do (
   Echo "%%a"|findstr /I /V /R ^ftp^>|findstr /I /V /R ^bye>>"%temp%\%~9\server_list.lst"
   )

:: 2nd FTP commands to download only new files that dosn't exist locally [Part1]
::Echo Creating commands_2.ftp File (part1)
IF "%~8"=="" (
   (
   Echo lcd "%~8"
   )>>"%temp%\%~9\commands_2.ftp"
) Else (
   (
   Echo lcd "%~7"
   Echo cd "%~8"
   )>>"%temp%\%~9\commands_2.ftp"
   )

:: compare files that will be downloaded with the file.list and if there is a match
:: it see if this match isn't in the local folder so it can download that file. [Part2]
::Echo Creating commands_2.ftp File (part2)
For /F "delims=" %%a in ('Type "%~1"') Do (
   For /F "delims=" %%b in ('Type "%temp%\%~9\server_list.lst"') Do (
   Set "sf=%%b"
   setlocal enabledelayedexpansion
      IF "%%a"=="!sf:~1,-1!" (
         IF NOT EXIST "%~7\%%b" echo get %%b>>"%temp%\%~9\commands_2.ftp"
      )
   endlocal
   )
)

:: Add disconnect & bye commands to 2nd FTP command file [Part3]
::Echo Creating commands_2.ftp File (part3)
(
Echo disconnect
Echo Bye
)>>"%temp%\%~9\commands_2.ftp"

:: start FTP session and download new files
::Echo Connecting to FTP server to download files
"%FTPapp%" %~3@%~2 -pw %~4 -P %~5 -b "%temp%\%~9\commands_2.ftp"

:: Check files count
::Echo Counting files in local folder
set Fcount=0
For /F "delims=" %%a in ('Dir /B "%~7\*.*"') Do set /a Fcount += 1

:: Check Condition to Exit or Continue Looping
::Echo Check count number
Setlocal EnableDelayedExpansion
IF "%Fcount%" EQU "%~6" (
   "%zip_program%" x -y "%~7\*.%zipped%" -o"%~7" >nul
   Del /F /Q "%~7\*.%zipped%" >nul
   RMDIR /S /Q "%temp%\%~9" >nul
   Goto :EOF
) Else (
   Del /F /Q "%temp%\%~9\server_list.lst" >nul
   :: Wait 5 min
   Ping Localhost -l 1 -n 300 >nul
   Goto :loop )

Line 20 : Add the Path of the PSFTP exe file.
Line 26 till 39 : removed the server, sername, password from script
Line 43 : changed the FTP command to the new one.
Line 53 to 62 : removed the server, username, password from script
Line 87 : changed the FTP command to the new one.
Also, changed the order of the variable as i added the Port variable as a 5th var.


FTP.set
FILE_LIST;FTP_SERVER;USER_NAME;PASSWORD;PORT;MAX_NUMBER_TO_DOWNLOAD;LOCAL_FOLDER;REMOTE_FOLDER
file.lst1;ftp.domain1.com;USERNAME;PASSWORD;2200;3;%userprofile%\desktop\1234;test_folder
file.lst2;ftp.domain2.com;USERNAME;PASSWORD;2300;3;%userprofile%\desktop\new2;test_folder_2
Added the Port Number between Password and Max Number

It should work, but i think the dump.log will contain different form and I'm not sure if it will extract the files names correctly.

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: Move from FTP and count in Target folder automated

#157 Post by aaksar » 18 Apr 2013 07:49

"%FTPapp%" %~3@%~2 -pw %~4 -P %~5 -b "%temp%\%~9\commands_1.ftp" >"%temp%\%~9\dump.log"


above code is not executing properly, seems like its not be able to append var.

when i am doing this thing on cmd with actual value its working fine.
but with these variables its not working
giving same error
ssh_init: Host does not exist

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move from FTP and count in Target folder automated

#158 Post by abc0502 » 18 Apr 2013 17:55

I think we missed this, Did you changed the FTP_SERVER column in the FTP.set from
ftp.domain1.com
To

Code: Select all

domain1.com
?

Remove The "ftp."

The Error is a host related, and by using the already existing settings, it will look like this:
User@ftp.domain.com

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: Move from FTP and count in Target folder automated

#159 Post by aaksar » 18 Apr 2013 22:26

I hv changed all the things, but it was not working .
but when i run thro CMD with actual values its working fine......

seems like its not be able to append varriable in username/pswd/hostname

I have tried other seq also , but still not working

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move from FTP and count in Target folder automated

#160 Post by abc0502 » 20 Apr 2013 05:57

have you tried this:

starting from line 23 (in the above script) in Code.bat, add these lines:

Code: Select all

set "Rserver=%~2"
set "RserverU=%~3"
set "RserverP=%~4"
set "Pnum=%~5"
set "ftpTMP=%~9"


Now Replace %~2, %~3, %~4, %~5 and %~9 in lines 43 and 87 ( lines that starts with "%FTPapp%" )
with it's corresponding variables we just add in the previous step and see if it will work.

For Example:

Code: Select all

"%FTPapp%" %~3@%~2 -pw %~4 -P %~5 -b "%temp%\%~9\commands_2.ftp"
will be:

Code: Select all

"%FTPapp%" %RserverU%@%Rserver% -pw %RserverP% -P %Pnum% -b "%temp%\%ftpTMP%\commands_2.ftp"

Post Reply