• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

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

0
sdholden28
Asked:
sdholden28
1 Solution
 
AlexPaceCommented:
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
 
sdholden28Author Commented:
Great. Thank you. I was able to take many cues from this and get what I needed.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now