Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod crash desktop driver drivers error ethernet excel freeze games gaming hard drive hardware hdmi internet laptop malware memory missing monitor motherboard network printer problem ram random registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless xbox
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Windows 7 and batch FTP coding

Reply  
Thread Tools
Tomo8281's Avatar
Computer Specs
Member with 135 posts.
 
Join Date: Aug 2006
Location: UK
Experience: Studying Computer Science and AI at University
04-Feb-2010, 01:03 PM #1
Windows 7 and batch FTP coding
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
Code:
::---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's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
04-Feb-2010, 01:49 PM #2
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.
Code:
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:
Code:
ftp> open MYFTPSERVER
Unknown host MYFTPSERVER.
ftp> user USERNAME PASSWORD
Not connected.
ftp> bye
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Tomo8281's Avatar
Computer Specs
Member with 135 posts.
 
Join Date: Aug 2006
Location: UK
Experience: Studying Computer Science and AI at University
04-Feb-2010, 01:58 PM #3
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's Avatar
Computer Specs
Member with 135 posts.
 
Join Date: Aug 2006
Location: UK
Experience: Studying Computer Science and AI at University
04-Feb-2010, 02:05 PM #4
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%

Last edited by Tomo8281; 04-Feb-2010 at 02:19 PM..
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
04-Feb-2010, 02:21 PM #5
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:
Code:
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%
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Tomo8281's Avatar
Computer Specs
Member with 135 posts.
 
Join Date: Aug 2006
Location: UK
Experience: Studying Computer Science and AI at University
04-Feb-2010, 02:32 PM #6
Quote:
Originally Posted by TheOutcaste View Post
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:
Code:
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%
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

Last edited by Tomo8281; 04-Feb-2010 at 02:37 PM..
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
04-Feb-2010, 03:10 PM #7
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.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:41 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.