Link to home
Start Free TrialLog in
Avatar of sdholden28
sdholden28Flag for United States of America

asked on

javascript to append date to filename and move file

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

ASKER CERTIFIED SOLUTION
Avatar of AlexPace
AlexPace
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sdholden28

ASKER

Great. Thank you. I was able to take many cues from this and get what I needed.