We help IT Professionals succeed at work.

Convert this script to work with Windows 10 ftp

963 Views
Last Modified: 2017-04-06
I would like to convert this script, which is meant for CuteFTP, to work on with Windows 10 ftp, without any special software.
Is this possible?

Set MySite = CreateObject("CuteFTPPro.TEConnection")

' Initialize remote server host name, protocol, port, etc.
MySite.Host = "xxxx.xxxxxxx.com"
MySite.Protocol = "FTP"
MySite.Port = xx
MySite.Retries = xx
MySite.Delay = xx
MySite.MaxConnections = 4
MySite.TransferType = "AUTO"
MySite.DataChannel = "DEFAULT"
MySite.AutoRename = "OFF"
' WARNING!!! SENSITIVE DATA: user name and password.
MySite.Login = "xxx"
MySite.Password = "xxx"
MySite.SocksInfo = ""
MySite.ProxyInfo = ""
' Connect to remote server
MySite.Connect

MySite.RemoteFilterInclude = "*.csv"

MySite.GetList "/out", "", "%NAME"
FileList = MySite.GetResult
FileArray = Split(FileList, vbCrLf)
For Each FileName In FileArray
    If FileName <> "" Then
        MySite.Download "/out/" & FileName, "\\xxx\xxx\xxx\" & FileName
        If CBool(MySite.LocalExists ("\\xxx\xxx\xxx\" & FileName)) Then
            MySite.RemoteRemove "/out/" & FileName
        End If
    End If
Next

MySite.Disconnect
MySite.Close
Comment
Watch Question

arnoldEE Topic Advisor, IT Consultant
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
The command line FTP client is not scriptable, see if the powershell referenced in the link helps you do what you need.

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Commandline ftp IS scriptable but not dynamically, i.e. if you have set download requirement of everything in a subdir and similar or can do... was trying to work out specifically what it is doing at the moment - can you explain what you WANT it to do, on the way out but looks like grab all the CSV in a directory?

e.g.  http://scripts.dragon-it.co.uk/links/batch-get-ftp-files-and-delete

Steve

Author

Commented:
Thanks Steve, yes to grab all csv files in a specific directory, and then delete all the files it just downloaded in the source directory, perhaps only deleting based on the names of the files it downloaded, just in case a new file was placed on that directory, as it was copying the files out.
Steve KnightIT Consultancy
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
If you'd like to hide the password a little too then try this that will ask for password first time then store it away:

@echo off
set /p password=<%~nx0:password
if not "%password%"=="" goto OK
set /p password=Enter password: 
echo %Pwd%
echo %pwd%>%~nx0:password
set password=%pwd%
:OK

set localdir=c:\mydir
set remotedir=/out
set ftpserver=x.x.x.x
set username=user

del "%localdir%\*.*" /q

call :GetFtpFiles
call :ClearFTPFiles

exit /b

:GetFTPFiles

(echo open %ftpserver%
echo user %username%
echo %password%
echo bin
echo prompt 
echo cd %remotedir%
echo lcd "%localdir%"
echo mget *.csv
echo quit
) | ftp -n -i

exit /b

:ClearfTPFiles

(echo open %ftpserver%
echo user %username%
echo %password%
echo prompt 
echo cd %remotedir%
for /f "tokens=*" %%a in ('dir /b /a-d "%localdir%\*.*"') do echo DELE "%%~a"
echo quit
) | ftp -n -i

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Steve, I don't think you can execute the DOS for command as an FTP command during the FTP session, can you?

~bp
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Hi Bill,

It isn't, it is writing a script on the fly with static commands before and after and the FOR command outputting a DELE line for each file present that was downloaded the previous time and piping that command script into the ftp command.

So you end up with a command like:

open x.x.x.x
user username
password
prompt 
cd /out
dele fil1.csv
dele fil2.csv
quit

Open in new window



Steve
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Ah yes, of course, was reading it wrong.

I think the real challenge here is to only delete a file off the remote ftp server if it is known to have transferred to the local machine successfully.  Sounds simple enough, but not all that easy to do with DOS ftp, unless each file was a new session.  Or the log file is parsed looking for errors, etc.  But it might be enough to just confirm that a file with that name exists locally, even though the transfer could have failed part way through and it could be a partial transfer...

Not sure how exact the requirements are.

~bp
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Yes, agreed.  If a file ends up existing that has transferred partially or 0 bytes then will trigger a deletion too.

If it needs to be rock solid only delete if has been known to fully retrieved the file then might need to be different.

All depends how criticial files are, how likely that will be interrupted -- i.e. 1Gb CSV from remote site down ADSL line or 10Kb file from local LAN...

Steve

Author

Commented:
The CSV files are small, and likely not to be interrupted.
If they are we will be able to tell, since hard copies PDF's of the contents of the CSV files are also available.

Author

Commented:
Steve, how can I run your script?
Is it within a .bat file?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Steve's proposed solution was a BAT script.

~bp

Author

Commented:
Thanks again.
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Yes,  save it as a .cmd or .bat - the batch file commands write the ftp scripts and then run the command line ftp command with those Scripts to do the download and deletion.

Steve
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
If there is the need for dynamic stuff, there is the way to use Internet Explorer for FTP access, or the .NET class (IIRC), or map a FTP location as kind of network drive with fftpuse - this allows access like you are used to with network drives.

Author

Commented:
Steve, thanks. Would I just double click the .cmd or .bat file to get this working?

Author

Commented:
Steve, I double checked the username and password, yet when run your script it does not login.
It says it's incorrect.

Author

Commented:
Hi everyone, this is not working for me, I cant run it properly in cmd or bat, when I double click it seems it doesn't like the credentials, which are accurate. So at this stage, this is not working.
arnoldEE Topic Advisor, IT Consultant
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
steve did not provide a script, but a step by step what an ms ftp client process looks like.
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks Bill, do I just run this by double clicking on the .bat?

Author

Commented:
Bill, thanks for this however it doesn't seem to delete the files on the remote directory though.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Here are the results:


C:\Users\user\Desktop>newftp.bat
The system cannot find the file specified.
Please login with USER and PASS.
Local directory now \\new\directory\NewInbound.
---> OPTS UTF8 ON
Please login with USER and PASS.
---> USER xxxxxxxx
---> PASS xxxxxxxx
---> CWD /out_order
Invalid command.
---> DELE xxxxxxxxxxxxxx_xxxxxx.csv
---> QUIT

C:\Users\user\Desktop>
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Okay, good, that is exactly the problem I saw here.  The way Steve has it coded (and it seems more like a BAT script bug than anything he missed) it sends a QUIT command after each DELE command, rather than sending all the DELE commands and then one final QUIT.

We'll work on this and see if we can figure out why it's happening, or a work around.  We should be able to get around this, I just don't like when things don't work the way they are supposed to...

Glad everything else is sorted out.

~bp

Author

Commented:
Thanks very much Bill, glad to hear this.. thanks kindly for all your help so far.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks to Bill and Steve. This now works.
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Only just had all the notifications come through all at once, will check back and see what has changed, I've used that script before ok but quite possible I have copy pasted something wrongly.

Will check posts over when on Pc in bit.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Steve,

Give this test a try too Steve, case TWO was interesting...

@echo off
setlocal

echo.TEST ONE
echo line1
echo line2
for /l %%a in (3,1,4) do echo line%%~a
echo line5
echo.===========

echo.
echo.TEST TWO
(
echo line1
echo line2
for /l %%a in (3,1,4) do echo line%%~a
echo line5
)|more
echo.===========

echo.
echo.TEST THREE
(
echo line1
echo line2
(for /l %%a in (3,1,4) do @echo line%%~a)
echo line5
)|more
echo.===========

Open in new window

~bp
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Blimey, batch strikes again, as does my bodged late night quick editing to include that password routine and not swapping the %PWD% lines.

I can't think of any logical reasoning behind batch interpreter being designed to work out like that, doesn't matter if you are just inside ( ), seems only if you are piping out of ( )  then using a for command in side the ( ) repeats everything upto the next ). Weird!

Sure that I had previously had it working as it is, I did originally have it write script to text file and then use that with ftp rather than piping it so possibly only tested with one file which would have worked.

I'll amend my script page with a link back to here.

Steve
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Yes, totally bizarre !

~bp
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Strange. Changing TEST TWO to TEST FOUR works too.
echo.
echo.TEST FOUR
(
echo line1
echo line2
for /l %%a in (3,1,4) do @echo line%%~a
echo line5
)|more
echo.===========

Open in new window

Interesting bug ...
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
TEST FOUR failed here, producing:

TEST FOUR
line1
line2
line3
line5
line4
line5

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
You are correct. Didn't spot that, just that there is no echoed echo.
Guess because internally
(
  echo line1
  echo line2
  for /l %%a in (3,1,4) do (echo line%%~a)
  echo line5
)

Open in new window

is converted into
echo line1& echo line2& for /l %%a in (3,1,4) do echo line%%~a& echo line5

Open in new window

which has the exact same behaviour as FOUR. The behaviour of TWO with added echoes is "caused" by adding the pipe.
As we know, everything following the DO in FOR is executed inside of the loop.
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
And same result consistently on Windows 2000, XP, WIn 7 and Win 10.  I suppose when you see it like https:#a42082301 it is obvious why and reminds you that batch is a command interpreter not a structured programming language!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.