Move from FTP and count in Target folder automated

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Move from FTP and count in Target folder automated

#16 Post by aaksar » 26 Nov 2012 05:00

the fTP location contains dir and subdir, but later all the files will be in 1 folder only so no subfolder concept later on.

i have permission to create folder in temp
C:\DOCUME~1\AnSharma\LOCALS~1\Temp>


yup all the zipped files will come with .zip only

regarding pswd, actually i tried before also 2 times ^^ wont work,
but when i gave ^^^^ it worked, so i dont know, i hv tried both option but no luck.
Thanks
Ankur

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

Re: Move from FTP and count in Target folder automated

#17 Post by abc0502 » 26 Nov 2012 05:09

the fTP location contains dir and subdir, but later all the files will be in 1 folder only so no subfolder concept later on.

No i mean, suppose the frp server has folder1, folder2 and folder3

now does you files exist with these 3 folders in the main ftp directory or it is in a separate folder alone, with no folder with it. ?

by the way, i just tested the special characters and it works with me without the need for any escape charachters. does your password has the separator between the input fields ";"

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

Re: Move from FTP and count in Target folder automated

#18 Post by aaksar » 26 Nov 2012 05:29

icurrently in the FTP location , it contains 3 folders, my file exist in all these 3 folders in which 2 folders contains subfolder which consist files.

but later on my files will be in 1 folder only, there wont be any subfolder.

password doesn't contains ; char.

its same as i mentioned in previous post.

might be there is some other prob.

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

Re: Move from FTP and count in Target folder automated

#19 Post by abc0502 » 26 Nov 2012 05:35

ok i will post the code i changed in few minutes, but it will generate errors and won't continue if it try to download a folder cause i can't find a way yet to identify the folder as you have files with no extensions.

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

Re: Move from FTP and count in Target folder automated

#20 Post by aaksar » 26 Nov 2012 05:57

thro mget we can fetch all the files which are there...
i know the folder name which contains the file without extension, and 1folder contains 2 subfolder which contains the zip file, and 3rd folder contains .txt file

or can add.txt which dont have extension?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move from FTP and count in Target folder automated

#21 Post by foxidrive » 26 Nov 2012 05:58

On an FTP server you can execute an l command or ls and get a list of files with attributes. It's not universal though.

That's one reason why writing FTP scripts is not fun. You cannot easily separate extensionless filenames from the FTP server chatter without the attributes.

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

Re: Move from FTP and count in Target folder automated

#22 Post by abc0502 » 26 Nov 2012 06:14

Edited:
Changes Log:
> Added double quotes arround all %~5 and %~6 in Code.bat
> Changed GEQ to EQU in this line

Code: Select all

IF "%Fcount%" EQU "%~4" (

Here is the Files, changed some codes and added another, and changed FTP_locations.txt to FTP.set

Start.bat

Code: Select all

@Echo OFF
Title FTP Main Download Script

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

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


Code.bat

Code: Select all

@Echo OFF

::"remote_server" -----------------------> %~1
::"remote_server_username" --------------> %~2
::"remote_server_password" --------------> %~3
::"total_number" ------------------------> %~4
::"local_folder" ------------------------> %~5
::"remote_folder" -----------------------> %~6
::"Temp Folder For FTP servers" ---------> %~7

:loop
set "zip_program=%programfiles%\7-Zip\7z.exe"

:: FTP commands to get list of files on the server
IF "%~6"=="" (
   (
   Echo open %~1
   Echo %~2
   Echo %~3
   Echo ls
   Echo disconnect
   Echo Bye
   )>"%temp%\%~7\commands_1.ftp"
) Else (
   (
   Echo open %~1
   Echo %~2
   Echo %~3
   Echo cd "%~6"
   Echo ls
   Echo disconnect
   Echo Bye
   )>"%temp%\%~7\commands_1.ftp"
   )

:: start FTP session and log output
Ftp -v -i -s:"%temp%\%~7\commands_1.ftp" >"%temp%\%~7\dump.log"

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

:: 2nd FTP commands to download only new files that dosn't exist locally
IF "%~6"=="" (
   (
   Echo open %~1
   Echo %~2
   Echo %~3
   Echo lcd "%~5"
   )>"%temp%\%~7\commands_2.ftp"
) Else (
   (
   Echo open %~1
   Echo %~2
   Echo %~3
   Echo lcd "%~5"
   Echo cd "%~6"
   )>"%temp%\%~7\commands_2.ftp"
   )

:: get the all file names that dosn't exist in the local folder and put then in ftp command file 2 to be downloaded
For /F "tokens=* delims=" %%a in ('Type "%temp%\%~7\server_list.lst"') Do (
   IF not exist "%~5\%%a" echo get %%a >>"%temp%\%~7\commands_2.ftp"
   )

:: Add disconnect & bye commands to 2nd FTP command file
(
Echo disconnect
Echo Bye
)>>"%temp%\%~7\commands_2.ftp"

:: start FTP session and download new files
Ftp -v -i -s:"%temp%\%~7\commands_2.ftp"

:: Check files count
Setlocal EnableDelayedExpansion
set Fcount=0
For /F "delims=" %%a in ('Dir /B "%~5"') Do set /a Fcount += 1

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


FTP.set
FTP_SERVER;USER_NAME;PASSWORD;MAX_NUMBER_TO_DOWNLOAD;LOCAL_FOLDER;REMOTE_FOLDER
ftp.domain1.com;username;password;3;C:\documents and settings\admin\desktop\1234;
ftp.domain2.com;username;password;4;C:\documents and settings\admin\desktop\FTP Project;

>don't leave empty space after the last ; if you won't put a remote folder.
>don't use "%userprofile%\desktop" instead of "C:\documents and settings\admin\desktop" cause FTP dosn't understand that.


All this 3 files must be in the same folder, and the temp files will be created in the "%temp%" directory.

About the password i will change my ftp password to match yours but can you post your password and replace all letters, numbers, etc. with "x" and leave the special character locations as it so i can make a password like yours.

I tried to test special character but work with me no problems here.

And this won't work unless all files in one location to keep track of the file that is missing, you can add 1 ftp server more than once if the server support more than one connection and change the remote folder but it will keep all the cmd windows looping and waiting for the complete files.
Last edited by abc0502 on 27 Nov 2012 17:50, edited 4 times in total.

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

Re: Move from FTP and count in Target folder automated

#23 Post by aaksar » 26 Nov 2012 08:14

MY FTP is down due to 3 attaempt wrong pswd, will update tmrow

and pswd for ftp is ^XXXX4X

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

Re: Move from FTP and count in Target folder automated

#24 Post by abc0502 » 26 Nov 2012 08:17

ok no problems and i will do more tests, but is it ok to delete files after they have been downloaded, or some one else download them.

if we delete them that will make the code.bat easier and no need for checking for the already downloaded files.
It will just keep downloading till the files in the local folder reach a specific amount.

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

Re: Move from FTP and count in Target folder automated

#25 Post by aaksar » 26 Nov 2012 09:22

i dont have permission to delete files from FTP loc.

so cant delete, in the mean time we are requesting to provide .txt, and all the files will come in 1 folder only

and 1 more thing, can you pls explain how you are reading the columns from file as Dos works on row basis...

Thanks
Ankur

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Move from FTP and count in Target folder automated

#26 Post by Squashman » 26 Nov 2012 10:25

aaksar wrote:
and 1 more thing, can you pls explain how you are reading the columns from file as Dos works on row basis...

Thanks
Ankur

FOR loops have a delimiter option to split a line apart into several variables.

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

Re: Move from FTP and count in Target folder automated

#27 Post by abc0502 » 26 Nov 2012 18:19

As Squashman said,
For /F "tokens=1-6 delims=;" %%a in (..............

delims is telling the For command to splite the row every time it find the ; separator and the tokens tell the for loop that there from 1 to 6 parts in that line.

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

Re: Move from FTP and count in Target folder automated

#28 Post by abc0502 » 26 Nov 2012 20:11

I tested a Password Using All This :
! @ # $ % ^ & * ( ) _ + - = \ / | > < ? ~ , . ' " : ] [ } { ;

as a first Character in a password & The result was:
Only
! ^ & ) | > < " ;
didn't work work in the test, and the rest worked without the need to escape them

And Only
! ^ & ) | > <
worked after adding "^" before it.

The rest
" ;
couldn't escape it so don't put it in a password.

So, in your case, IF your password has any of these Charachters:
! ^ & ) | > <

Add before it "^" in the "FTP.set", I tested on my FTP server and it worked.


Here is the output of the FTP window after logging in and downloading the 2 7z files:
ftp> open ftp.drivehq.com
User (ftp.drivehq.com:(none)):

ftp> lcd "C:\documents and settings\admin\desktop\1234"
Local directory now C:\documents and settings\admin\desktop\1234.
ftp> get "1.7z"
ftp> get "2.7z"
ftp> disconnect
ftp> Bye
and the password I used was "^storage" ( without the double quotes )

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

Re: Move from FTP and count in Target folder automated

#29 Post by aaksar » 27 Nov 2012 02:59

script working fine, but not picking up the files from folders, if i m giving the folder name in the remote dir, then also its not picking up.
only copying the files which are not in any folder.

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

Re: Move from FTP and count in Target folder automated

#30 Post by abc0502 » 27 Nov 2012 03:18

if i m giving the folder name in the remote dir, then also its not picking up.
only copying the files which are not in any folder.
Do you mean that you want to download the whole folder with it's content ?? you can't download folders.

Also,
can you post your FTP.set file after removing all sensitive information from it like ftp, user, pass, etc.
but make sure that the structure of the remote and local folder is exact when replacing there names, so if a folder has a space in it's name, make a name having the same.

Post Reply