Solved

javascript to append date to filename and move file

Posted on 2013-11-01
2
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
Progress

691 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