<

Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x

Drag and Drop to Move and Rename and Add Current Date to Filename

Published on
13,537 Points
4,037 Views
Last Modified:
Awarded
How to avoid the "Confirm File Replace" prompt.

One of my clients had a specific need and requested that I write a program to handle it:  Every day, he needs to move files from a "staging" directory to an "archive" directory.  The problem is that just dragging and dropping the files won't work -- the "current" files would have the same names as the "archived" file.

The solution is to move the files to the target folder, but rename each of them with an inserted date prefix, so that the new filenames will not collide with, or overwrite the older ones.

I first tried to write a complicated DOS batch file -- you can use the Windows Explorer to drop files onto a .BAT file icon and they end up as %1, %2, etc. during execution.   But I ran into a hassle when there are more than 9 files in the dropped group.  Obtaining the date as a usable string is a bit of a trick, as well.

Anyway, batch files are way obsolete.  There is no need to use the arcane batch-file syntax when the Windows Scripting Host (WSH) provides a choice of full-featured scripting languages and access to powerful tools such as the FileSystemObject object.

Anyway, I solved my client's problem with this simple JScript program:
//-------- JScript utility to move files, and rename with a date component

var sDestFolder= "F:\\Accounting\\ArchivedBackups\\";

var oArgs= WScript.Arguments;
var oFSO=  new ActiveXObject("Scripting.FileSystemObject");
var oDt=   new Date();   
var sRenPrefix= oDt.getFullYear() 
    + ("0"+(oDt.getMonth()+1)).slice(-2) // 01 to 12
    + ("0"+oDt.getDate()).slice(-2)      // 01 to 31
    + ("_");
//--------- cycle through the filenames in the dropped selection

for (var j= 0; j < oArgs.Count(); j++ ) {
    var sSrcFile= oArgs( j );
    var sFilename= oFSO.GetFileName( sSrcFile );  // e.g., filename.ext 
    var sDestFile= sDestFolder;
        sDestFile += sRenPrefix;  // e.g., 20090714_ 
        sDestFile += sFilename;   // ArData.dta
    oFSO.MoveFile( sSrcFile, sDestFile ); // move & rename to target folder
}
WScript.Echo( oArgs.Count()+" Files renamed and moved to ARCHIVE folder" );	

Open in new window


Just create a text file on your desktop, and paste the above text into it.  Then rename that file with an extension of .JS; for instance, DropHereToArchive.JS  The desktop icon changes from a text document to a script document.  

Now, when you want to move the files to the archive folder, just use the Windows Explorer to select the files, drag them over the script icon, and drop them.

There are lots of ways to improve this and/or modify that script for more flexibility.  For instance, that code will not work if you drop an entire folder on the icon.  You would need to use the oFS.MoveFolder() method.   Also, if you need to drop the files more than once per day, you would want to add a time component to the renaming prefix.

And of course, if you do this same thing -- for the exact same set of files -- every day, then it would be silly to force your user to select and drag the files... Instead, just hard-code a list of source filenames into the script.

One other note:  
Take a look at the code that generates the "dating prefix."  Note that I went to some trouble to ensure that the format would be YYYYMMDD; that is, two digits for the month and two digits for the day.  The JScript String.slice() method can be used to emulate the often-needed Right$()-type function of many programming languages (to extract from the end of the string).  Just use a negative value as the first (and only) parameter.

The point of using "normalized" dates (with two-digit month and two-digit day) is that they will always be displayed in the correct order when sorted.  That will make it easy to locate and retrieve a specific-day's files from the archive folder.

References:

Date Object (Windows Scripting - JScript)
http://msdn.microsoft.com/en-us/library/cd9w2te4(VS.85).aspx

String Object (Windows Scripting - JScript)
http://msdn.microsoft.com/en-us/library/ecczf11c(VS.85).aspx

FileSystemObject Object
http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx

Microsoft Windows Script Technologies
http://msdn.microsoft.com/en-us/library/d1et7k7c(VS.85).aspx

Windows Script Host Object Model
http://msdn.microsoft.com/en-us/library/a74hyyw0(VS.85).aspx

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If you liked this article and want to see more from this author,  please click the Yes button near the:
      Was this article helpful?
label that is just below and to the right of this text.   Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
0
Comment
Author:DanRollins
[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
0 Comments

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Join & Write a Comment

This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month