# Windows 7 and batch FTP coding



## Tomo8281 (Aug 3, 2006)

Hi,

Im just messing around with FTP batch file coding since I was bored, and Ive come across a problem. What I want to do at the moment, is just access the ftp server and leave (then will progress onto other stuff).

I have at the moment saved as ftps.bat

```
::---Start ftps.bat---
@echo off
setlocal

::Make a txt file in temp to put all this shiz into
set f=%temp%\ftpc22.txt

::Edit the txt file and place the commands
echo open MYFTPSERVER>>%f%
echo user USERNAME PASSWORD>>%f%
echo bye>>%f%

::Run ftp with the script
ftp -n -d -s:%f%

::Remove the temp files
del /f /q %f%
endlocal
exit
::---End ftps.bat---
```
In theory this should work right? Accesses my ftp server, inputs username and password, then leaves. When I run this it doesnt do anything (I run through command prompt). Then if I remove "echo off" so I can see whats happening, all it is doing is repeating the word "ftp" line after line. 
I've tried many different ways of doing it in a batch file, but none of them work. I can easily do it manually via command prompt with no problems, it just doesnt like the script.

So, is this a problem with Windows 7 that I dont know about? Or something I can fix.

Thanks,
Tom


----------



## TheOutcaste (Aug 8, 2007)

That works just fine for me on Win 7 ultimate x64, so I don't know what to tell you, unless there is something in the file you haven't posted. A missing double quote or percent symbol, or parentheses can cause odd results.

Here's the output with the Echo command removed.
Names have been changed to protect the guilty, er I mean innocent.

```
C:\Testfolder>ftps.bat

C:\Testfolder>setlocal

C:\Testfolder>set f=C:\Users\THEOUT~1\AppData\Local\Temp\ftpc22.txt

C:\Testfolder>echo open MyFTPServer 1>>C:\Users\THEOUT~1\AppData\Local\Temp\ftpc22.txt

C:\Testfolder>echo user TheOutcaste Password 1>>C:\Users\THEOUT~1\AppData\Local\Temp\ftpc22.txt

C:\Testfolder>echo bye 1>>C:\Users\THEOUT~1\AppData\Local\Temp\ftpc22.txt

C:\Testfolder>ftp -n -d -s:C:\Users\THEOUT~1\AppData\Local\Temp\ftpc22.txt
ftp> open MyFTPServer
Connected to MyFTPServer.
220 FTP server (Version 5.339PWS/AI) ready.
ftp> user TheOutcaste Password
---> USER TheOutcaste
331 Password required for TheOutcaste.
---> PASS Password
230 User TheOutcaste logged in.
ftp> bye
---> QUIT
221 Goodbye.

C:\Testfolder>del /f /q C:\Users\THEOUT~1\AppData\Local\Temp\ftpc22.txt

C:\Testfolder>endlocal
C:\Testfolder>
```
Even with a dummy host you should just get an unknown host error:

```
ftp> open MYFTPSERVER
Unknown host MYFTPSERVER.
ftp> user USERNAME PASSWORD
Not connected.
ftp> bye
```


----------



## Tomo8281 (Aug 3, 2006)

I know, thats why its really weird! It should work but it doesnt! Im also running x64 Ultimate :/

This is what it looks like when I run the file via command prompt or on its own when I take the echo off.


----------



## Tomo8281 (Aug 3, 2006)

Er ok, it now works?

All I did was clean up some of the other files I have been working on in my C:\ directory...

Thats so wierd!

Anyway, Ive also got a question for batch programmers. What I want to do, is access my ftp server (done), go into sub directory (echo cd subdir), I want to find files and delete them. Is there a way to read from a local txt file and find those files in the directory and delete them.

For example, I have in my C:\ drive a file called "DeleteList.txt" which contains a list of filenames to delete formatted with a space, i.e.
123454.jpg
123555.jpg
655543.jpg
I want to read this txt file and find and delete those corrisponding files in the ftp server. How can I do this?

Thanks.

EDIT:

Something like this? 
echo for /f "tokens=*" %%A in(Deletelist.txt) do(delete %%A) >>%f%


----------



## TheOutcaste (Aug 8, 2007)

If you perhaps had a version of this file named *ftp.bat* instead of *ftps.bat*, everytime the *ftp -n -d -s:%f%* line ran, it would have started *ftp.bat*, not *ftp.exe*, which could cause a loop like that. It always looks in the current folder before checking the folders on the path.

Use a for loop to read the file and create the delete commands:

```
set f=%temp%\ftpc22.txt
Set _DelList=C:\Stuff\DeleteList.txt
::Edit the txt file and place the commands
echo open ftp://MYFTPSERVER>>%f%
echo user USERNAME PASSWORD>>%f%
Echo cd subdir
For /F "Tokens=*" %%I In ('Type "%_DelList%"') Do Echo Delete %%I>>%f%
echo bye>>%f%
```


----------



## Tomo8281 (Aug 3, 2006)

TheOutcaste said:


> If you perhaps had a version of this file named *ftp.bat* instead of *ftps.bat*, everytime the *ftp -n -d -s:%f%* line ran, it would have started *ftp.bat*, not *ftp.exe*, which could cause a loop like that. It always looks in the current folder before checking the folders on the path.
> 
> Use a for loop to read the file and create the delete commands:
> 
> ...


Thanks,
But could you explain the difference between my version:
echo for /f "tokens=*" %%A in(Deletelist.txt) do(delete %%A) >>%f%
and yours:
For /F "Tokens=*" %%I In ('Type "%_DelList%"') Do Echo Delete %%I>>%f%
Just so I can learn  Also do caps matter? e.g. For /F instead of for /f


----------



## TheOutcaste (Aug 8, 2007)

You line starts with *Echo*, so all it will do is echo the statement, it won't execute it. The *Echo* command needs to be after the *Do* part of the statement, as that is the command you want to execute, or Do.

Caps don't matter for the switches or options, I use them as it's a bit easier for me to read the switches, and I think the Options look better that way. Avoids confusion between lowercase L and 1, but using lowercase for the letter O avoids confusion with the number 0, so you can argue which is better. All personal preference, nothing required.
Case _does_ matter with the loop variable though. %%A and %%a are not the same.

I also tend to use the *Type* command to output the contents of a file inside a loop. It works without it if the file name does not contain spaces. If it contains spaces, you have to quote it, requiring the use of *Type* or the *Usebackq* option. So when using a variable for the file name I always use *Type* just in case the name contains spaces. Someone may change the file name 6 months from now to one with a space, then you have to figure out why it suddenly doesn't work anymore.


----------

