ftp automation

ScriptFTP

The professional tool to automate FTP, SFTP, FTPS and schedule FTP batch jobs

The forum is now read only. Please, go to the the main ScriptFTP website if you need help.
Need help writing a script? Have any question about ScriptFTP?
Hi everyone.

I have a remote server that contains files, named as follows:
*BLABLA_2018May01.txt
*BLABLA_2018May02.txt
*BLABLA_2018May03.txt

What command can I use to rename the files as follows: *May* -> *05* ; *Jun* -> *06*
*BLABLA_20180501.txt
*BLABLA_20180502.txt
*BLABLA_20180503.txt

I'm able to navigate to the correct folder and list the contents, but replacing the strings in filenames is what's causing me headaches.

Any help in achieving the above effect is highly appreciated.

Thanks everyone.
Last edited by castor_bg on 08 Aug 2018, 15:29, edited 1 time in total.
Hi,

There are two new commands in the ScriptFTP 4.6 Beta to handle text strings that can be helpful for this. The commands are named TEXTINDEXOF and TEXTREPLACE, they are not documented yet (there is no help page for them in this website) but I have left you an example below.

The 4.6 Beta can be downloaded from here:

https://www.ScriptFTP.com/beta


FTP Script
  1. $filename = "*BLABLA_2018May03.txt"
  2.  
  3. # If we find Jan in the filename replace with 01
  4. IF(TEXTINDEXOF($filename,"Jan")!=-1)
  5.     $new_filename = TEXTREPLACE($filename,"Jan","01")
  6.  
  7. # If we find Feb in the filename replace with 02
  8. IF(TEXTINDEXOF($filename,"Feb")!=-1)
  9.     $new_filename = TEXTREPLACE($filename,"Feb","02")
  10.  
  11. # If we find Mar in the filename replace with 03
  12. IF(TEXTINDEXOF($filename,"Mar")!=-1)
  13.     $new_filename = TEXTREPLACE($filename,"Mar","03")
  14.  
  15. # If we find Apr in the filename replace with 04
  16. IF(TEXTINDEXOF($filename,"Apr")!=-1)
  17.     $new_filename = TEXTREPLACE($filename,"Apr","04")
  18.  
  19. # If we find May in the filename replace with 05
  20. IF(TEXTINDEXOF($filename,"May")!=-1)
  21.     $new_filename = TEXTREPLACE($filename,"May","05")
  22.  
  23. # And so on for Jun, Jul...
Hello there.

Thanks for your quick answer, but I can't seem to get it working. I tried it on a single file and this is the output I'm getting:

TEXTINDEXOF("PRIMEX.Admin.2018Aug01.txt","Aug")

Finding Aug in PRIMEX.Admin.2018Aug01.txt

Text found on position 17


TEXTREPLACE("PRIMEX.Admin.2018Aug01.txt","Aug","08")

Replacing Aug in PRIMEX.Admin.2018Aug01.txt with 08


Then, when I check the file it's still with the old name.

I've upgraded to the version above.
Hi,

You need to actually change the file name, not only the variable that holds the file name. This is done with RENAMEFILE (if it is a remote file) or EXEC("rename ......") if it is a local file. If you post your script here I can show you how. You can also post only the part of the script related to this, for privacy.
Hi, everyone.

Thanks to the much appreciated help from the Support I was able to get it working.


FTP Script
  1. CHDIR("/Fix Logs/PrimeX Logs")
  2. GETLIST($list,REMOTE_FILES)
  3. FOREACH $filename IN $list
  4.  
  5.     IF(TEXTINDEXOF($filename,"May")!=-1)
  6.         $new_filename = TEXTREPLACE($filename,"May","05")
  7.         RENAMEFILE($filename,$new_filename)
  8.     END IF
  9.    
  10.  
Thanks everyone.