0

I'll try and make this quick and simple. It can be hard to search of information on something to your not 100% sure on how you would search for it.

So I have a small batch script create to execute powershell ftp access and log me in and all that fun stuff.

Basically it's a cron job to upload files to my FTP server when i need to in a heartbeat.

I have HOME server, and on a regular basis or when told to, I want my script to execute and be able to upload a directory from my HOME server to the REMOTE ftp server.

Here is the script i am using.

@echo off<br>
echo user yourusername> ftpcmd.dat<br>
echo yourpassword>> ftpcmd.dat<br>
echo bin>> ftpcmd.dat<br>
echo cd \folder>> ftpcmd.dat<br>
echo put %1>> ftpcmd.dat<br>
ftp -n -s:ftpcmd.dat yourservername<br>

so I've gathered that put %1 = filetoupload

so filename.bat (the script) filetoupload = put %1 but it gives me errors saying

Saying: so Put I:\Documents Return: Error opening local file I:\Documents

Local on the FTP server? or Local on my HOME server.

How do i make it so i can upload a chosen directory from my HOME server to the FTP REMOTE server instead of files one by one???

Thank you sincerly

SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • Use a for loop, looping through all files in the given directory. – initramfs Feb 28 '15 at 05:54
  • Care to explain please? Cause it seems most terminal commands are for a given file, not usually for a Directory. and how would i be able to execute the For loop to scan the whole directory uploading each file in the directory without having to manually type it all in – Chris Mccafferty Feb 28 '15 at 06:00
  • Please move your question to [su]. It's off-topic here. – Martin Prikryl Mar 02 '15 at 07:57

1 Answers1

2

I am assuming %1 here refers to a directory to upload via FTP. You can instead use the batch for function/keyword to instead add many put directives, corresponding to files in your upload directory.

For example:

@echo off

echo user yourusername> ftpcmd.dat
echo yourpassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo cd \folder>> ftpcmd.dat

for %%f in (%1\*.*) do echo put "%%f">> ftpcmd.dat

ftp -n -s:ftpcmd.dat yourservername

and for the recursive edition:

@echo off

echo user yourusername> ftpcmd.dat
echo yourpassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo cd \folder>> ftpcmd.dat

for /R %1 %%f in (*.*) do echo put "%%f">> ftpcmd.dat

ftp -n -s:ftpcmd.dat yourservername
initramfs
  • 8,275
  • 2
  • 36
  • 58
  • Hmmmm. Thats very very interesting. Learning some good ol Batch Scripting :P :P So this for command than you have
    > ftpcmd.dat
    So if I have a directory names Documents and i want all the sub dirs or files within that folder/folders to be uploaded to the FTP server.
    that for command will do that for me.. and all i gotta do is run the filename.bat to execute it and itll automatically grab the files from the dir and upload them without user input?
    I very greatly appreciate it.
    – Chris Mccafferty Feb 28 '15 at 06:18
  • @ChrisMccafferty I'm not fully proficient in batch scripting, but it seems like [this](http://stackoverflow.com/questions/8397674/windows-batch-file-looping-through-directories-to-process-files/8398621#8398621) answer here shows its possible to do recursive directory handling. I'll try to modify the existing answer to include recursive directory handling. – initramfs Feb 28 '15 at 06:25
  • I greatly appreciate it. and hey wether you know a crap ton or nothing at all, it all adds up to a lot of knowledge in the end. and I wonder if anyone else or you would be able to explain why no matter what i do, iy always says "Error opening Local Directory (Directory) When the file is accessible from my home server and the HDD that the files are on is turned on. ??? So why the error? It would also be nice if people didnt just post their script and expect you to know everything about it. Why not do side notes of what the command or line of code is doing???????? – Chris Mccafferty Feb 28 '15 at 06:27
  • @ChrisMccafferty Perhaps you are specifying a directory instead of a file? I've never used the `ftp` program before so I wouldn't know. I added the recursive edition, this [page](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true) should give you some decent documentation on how to use the for structure in batch. – initramfs Feb 28 '15 at 06:33
  • Thank you very very very much. The recursive doesnt seem to work as we all think it does. It pretty much just send the script in a constant looping.. doing actually nothing at all.. it just looks like it is. – Chris Mccafferty Feb 28 '15 at 07:02
  • @ChrisMccafferty Hmm... Are you sure its not doing anything? I tested it on my computer first and it seemed to work fine. Maybe you just have too many files? – initramfs Feb 28 '15 at 07:13
  • Well it may simply be that i didnt set a directory to look for files in. and hmm. if you tested it all on your own computer and it worked. I probably just did something wrong, I think it didnt work because it does one put command(even if its recursive) and than logs me out of FTP.....(unless i edit the logout command) Though its 2:20am the time of sending this mssg so my brain has been hurting for ahwile. Ill try some more in the morning, but I greatly appreciate the assistance greatly, it deffinately helped me understand Batch a little better – Chris Mccafferty Feb 28 '15 at 07:24
  • Hey I was just reading that little manual.
    for /R [[Drive :]Path] {%% | %}variable in (set) do command [CommandLineOptions]
    So basically. IF i set the for command in my script with the drive letter and path that has the files needed for upload.
    Thus removing the put command replaing it with the FOR command.
    so now all you gotta do is run fileup and it uploads it all automatically because the params are set
    I just dont know what commands you would input where it asks. Unless
    for /R [[I:]Folder] %1 %%f in (*.*) do echo put "%%f">> ftpcmd.dat
    – Chris Mccafferty Feb 28 '15 at 07:56
  • @ChrisMccafferty Sorry I am a bit confused by what you are proposing. Perhaps update the question with your current situation? Also note the `
    ` tag as no effect in comments.
    – initramfs Feb 28 '15 at 08:29