Solved

javascript to append date to filename and move file

Posted on 2013-11-01
2
418 Views
Last Modified: 2013-11-15
I'm using the software smartFTP to automate some file transfers from a clients server to an ftp location. I've got it setup and scheduled and its working well. The software offers the ability to assign a post-action script via javascript, and the sdk provides samples to use.  I'm using the script as instructed by smartftp support, but it appears to delete the files instead of moving them to the specified directory, and smartFTP staff is of no assistance. The directory argument is specified through the software when you assign the script. Also, I need to append the date at time of transfer to the file name as it moves. For example. File A resides in folder X. The automated transfer runs, file A gets transferred, and then gets moved from folder X  to folder Yand the file name gets appended. So folderx\fileA becomes foldery\fileA10-31-2013. Here's the script I've attempted to make work. I've fiddled with it myself, but I have little to no experience with javascript and unfortunately have less time to learn than I'd like. So basically, the transfer works fine, but the files don't get moved, and I need to add the date to them.
////////////////////////////////////////////////////////////////////
// Summary: Moves the items that have been added during an operation to another folder after they have been processed
//
// Logic:
// TransferQueueOperation::OnAddItems(items)
// 1. Enumerate through all the items
// 2. Creates a new queue item for each item in items
// 3. Copies the Source property
// 4. Create a Destination property based on the Source property but with a different folder
// 5. Sets the operation of the new item to move
// 6. Adds the new items to the items passed to the OnAddItems() function
//
// Usage:
// - Use to move the items the copy operation creates
//
// Example Usage:
// - Assume that you have setup a copy operation that uploads a folder to the server. After the operation completed
//   you want to move all the files and folders that have been uploaded to another folder on the local computer.
// - Assume that you have setup a copy operation that downloads a folder to the local computer. After the operation completed
//   you want to move all the files and folders that have been downloaded to another folder on the server
//
// (c) SmartSoft Ltd.
////////////////////////////////////////////////////////////////////

// const
var DestinationFolder = ScriptHost.Arguments(0);

//var DestinationFolder = "c:\\temp";
// or if for a remote folder
//var DestinationFolder = "/htdocs/temp";


//ScriptHost.Echo("Load");

function TransferQueueOperation::OnBegin()
{
	//ScriptHost.Echo("OnBegin");
}

// This event is called for operations acting on folders because operations acting on a single file usually do not add new items
function TransferQueueOperation::OnAddItems(items)
{
  //ScriptHost.Echo("OnAddItems");

	// check if this is a copy operation
	if(TransferQueueOperation.TransferQueueItem.Operation != sfTransferQueueItemOperationCopy)
	{
		TransferQueueOperation.TransferQueueItem.LogWithCreate.Add(sfTransferQueueItemLogItemTypeError, "Not a copy operation");
		return;
	}

	// create a temporary collection to which we add the new items
	var newItems = new ActiveXObject("sfTransferQueue.TransferQueueItems");

	// enumerate through all the items
	for (var enumerator = new Enumerator(items) ; !enumerator.atEnd(); enumerator.moveNext()) 
{
    var item = enumerator.item(); 

		// Create new transfer queue item based on the item
		var newItem = TransferQueueOperation.CreateItem();
  	// copy its properties
  	newItem.Type = item.Type;
  	newItem.Size = item.Size;
  	newItem.SizeUnit = item.SizeUnit;  
  	// move the item
  	newItem.Operation = sfTransferQueueItemOperationMove;
  	newItem.Source = item.Source.Clone();
  	// to a new destination which is based on the Source
  	newItem.Destination = item.Source.Clone();
  	// path magic
  	if(newItem.Destination.Type == sfTransferQueueItemSubItemTypeLocal)
  		newItem.Destination.Path = DestinationFolder + "\\" + item.Source.GetFileName();		
  	else if(newItem.Destination.Type == sfTransferQueueItemSubItemTypeRemote)
  		newItem.Destination.Path = DestinationFolder + "/" + item.Source.GetFileName();		
    
    // set the dependencies of newItem
    // This is necessary to make sure that the move operation (newItem) is executed after the copy operation (item)
    newItem.DependenciesWithCreate.AddItemTail(item);
    
    // add item to our new items
    newItems.AddItemTail(newItem);
	} 
	
	// add our new items to the items that will be added to the transfer queue in a second
	items.AddItemsTail(newItems);
}

function TransferQueueOperation::OnEnd()
{
  //ScriptHost.Echo("OnEnd Result = " + TransferQueueOperation.Result);
}

Open in new window

0
Comment
Question by:sdholden28
2 Comments
 
LVL 16

Accepted Solution

by:
AlexPace earned 500 total points
ID: 39617819
I dunno about SmartFTP but this would be really easy to do in Robo-FTP like this:
SET source_folder = "c:\local\pickup\folder\" 
SET archive_folder = "c:\sent\" 

:start
WORKINGDIR source_folder
FTPLOGON "ftp.mydomain.org" /user="UserID" /pw="secret" 
IFERROR GOTO done
:process_local_file
GETNEXTFILE "*" 
IFERROR GOTO done
SENDFILE %nextfile 
IFERROR GOTO done
SET new_file_name = %nextfile + %date 
RENAME %nextfile new_file_name 
MOVE new_file_name archive_folder 
IFERROR GOTO done
GOTO process_local_file

:done
FTPLOGOFF  ;; this will return a harmless error if we are not logged in
PAUSE /until=03:00  ;; sleep until 3am in the morning
GOTO start 

Open in new window

Obviously I didn't know your file paths, account credentials or schedule so I just made up those values for the sake of the example... this example is designed to run as a windows service and it runs once a day at 3am but that kinda stuff is easy to change.
0
 

Author Closing Comment

by:sdholden28
ID: 39651424
Great. Thank you. I was able to take many cues from this and get what I needed.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now