2

I need to write a batch file that uploads two files via FTP to different folders.

Example: filename_0001.txt copy to folder1 on server1 filename_0002 copy to folder2 on server1.

The names of target files are fixed.

My current batch file uploads only the first file that has the lower number -the only difference in the filenames are the numbers, that are changed daily.

user name >>script.txt
%1 >>script.txt (password as parameter to batch file)
put filename_????.txt folder1
ftp -s:script.txt [server name]

How can the other file with higher number be uploaded? I thought of checking the file names and then put them in the script. Can anyone tell any command to do that?

I need something like this:

put filename_????+1.txt folder2
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
geminatores
  • 53
  • 1
  • 2
  • 6
  • So what is your input? You get a filename like `filename_0001.txt` as an input of your script (like command-line parameter?) and you need to "calculate" the other name? – Martin Prikryl Mar 25 '15 at 14:23
  • I've edited the question, mayby now it's more clear what I want to do. – geminatores Mar 26 '15 at 07:35
  • So there are always (only) two files in the folder. The numbers in the filenames are different each time. And you need to upload the file with the lower number (whatever it is) to `server1/folder1` and the file with the higher number to `server2/folder2`. Is that correct? – Martin Prikryl Mar 26 '15 at 07:39
  • Almost correct. There is only one server not 2 and sometimes there is only one file sometimes even three. So the batch file should do this:
    put filename_0001.txt server1/folder1 put filename_0002.txt server1/folder2 put filename_0003.txt server1/folder3
    – geminatores Mar 26 '15 at 08:42
  • And is it `folder1`, `folder2`, `folder3` or does the number in folder name somehow derive from the file number? – Martin Prikryl Mar 26 '15 at 08:58
  • Yes the name of folder differ from file name. – geminatores Mar 26 '15 at 09:51
  • And is it fixed to 1,2,3,...? – Martin Prikryl Mar 26 '15 at 09:54
  • No it's just an example :) – geminatores Mar 26 '15 at 10:15
  • So how does the script determine the folder names? If you want our help, you should really try to explain what you want to do. – Martin Prikryl Mar 26 '15 at 10:30
  • What's to explain? :) I would like to write a script that will act something like this: `put filename_0001.txt [server name]/[X folder name] put filename_0002.txt [server name]/[Y folder name] put filename_0003.txt [server name]/[Z folder name]` How to make the batch file to recognise the file name number and put it into correct folder? Script that i wrote just puts the first file and skips the higher number files. – geminatores Mar 26 '15 at 10:41
  • Well, noone has provided you an answer, so that obviously indicates you did not ask good question! So the folder names are fixed (hard coded)? But not all of them are used in some cases (if there are fewer files), right? What if there are more files than the number of hard coded folders? – Martin Prikryl Mar 26 '15 at 10:44
  • Mayby, sorry for that... Folders are fixed to files. For instance file with the lowest number should go to folder1 the next file should go to folder2 etc. I just have no idea how to write a command to put the second file to folder2. It wouldn't be a problem if the file name didn't change every day. It would be perfect to write it like this: `put [filename_????] [folder1]` `put [filename_????+1] [folder2]` `put [filename_????+2] [folder3]` – geminatores Mar 26 '15 at 10:58
  • Are the numbers sortable as text? I.e. is it like `filename_0009`, `filename_00010`? As opposite to `filename_9`, `filename_10`. – Martin Prikryl Mar 26 '15 at 12:03
  • Yes. Example: `filename_1234`, `filename_1235` etc [filename] is always the same, the number differ. – geminatores Mar 26 '15 at 12:56
  • The question was what happens when number of digits change. I.e. before `filename_1000`, is it `filename_999` or `filename_0999`? – Martin Prikryl Mar 26 '15 at 12:59
  • Now the file is something about 4700 so it's not a problem. But it would 4 digits. – geminatores Mar 27 '15 at 08:10

1 Answers1

0

Prepare a text file (folders.txt) with a sorted list of folders to use, like:

folderA
folderB
folderC
folderD

From a batch file save a sorted list of files to upload to another file (files.txt). Merge the two files together as described in question Combining multiple text files into one.

The code would be like:

@echo off
dir /ON /B filename_????.txt > files.txt

setlocal EnableDelayedExpansion

3< folders.txt (for /F "delims=" %%a in (files.txt) do (
   set /P FOLDER=<&3
   echo put %%a !FOLDER!
))

The output will be like:

put filename_4100.txt folderA
put filename_4101.txt folderB
put filename_4102.txt folderC
put filename_4103.txt folderD

It won't work when number of digits in filenames is different, as filename_10000.txt will be sorted before filename_9999.txt. For possible improvement, see Naturally Sort Files in Batch

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992