Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2012, 01:59 PM   PM User | #1
Adrian1995
New to the CF scene

 
Join Date: Aug 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Adrian1995 is an unknown quantity at this point
Unhappy Copy files using File System Object (VBScript)

The code below is what I already have and I just need someone to add the rest.
I require this program to copy multiply files in folders on F: and C: to either F: or C: based on last modified date and rename the old ones with "_old" at the end of the file names.
Please I need this done.
Cheers
Adrian1995





Code:
<HTML>
	<HEAD>
		<TITLE>Copy Project Files</TITLE>
		<SCRIPT language="JavaScript">
			<!--
				//Call function here
				
				//this function will check if the given folder is exists.
				function checkExistingFolder(Project)
				{
					var fs = new ActiveXObject("Scripting.FileSystemObject");
					return fs.FolderExists(Project);
					
				}
				
				//this function will create a folder and display a message.
				function createFolder(Project)
				{
					var fs = new ActiveXObject("Scripting.FileSystemObject");
					fs.CreateFolder(Project);
					document.write("New folder created.");
				}
				
				//this function will copy the file from the source to the destination.
				function copyFile(sourceFile, destFile)
				{
					var fs = new ActiveXObject("Scripting.FileSystemObject");
					fs.copyFile(sourceFile, destFile);
					document.write(sourceFile + " has been transferred! <br>");
					alert(sourceFile + "Has been transferred<BR>");
				}
				//this function will rename the file with _old at the end.
				function rename()
				{
					var new file name;
					
					new file name = destination + getbasename(getfile.(destinationfile).name)
					+ "_old"
					
					copyFile(destinationfile, new file name);
				}
				
				function loopThroughFiles(source,dest)
				{
					var fs = new ActiveXObject("Scripting.FileSystemObject");
					//get the file system folder object - source.
					var sourceFolder = fs.GetFolder(source);
					//get the file system folder object - destination.
					var destFolder = fs.GetFolder(dest);
					
					//get the file collection of the file system folder object.\
					var fileCollection = sourceFolder.Files;
					
					//enumerate file collections.
					var objEnum = new Enumerator(fileCollection);
					//move enumerator to the first item of the collection.
					objEnum.moveFirst();
					
					//declare a filename variable to hold the item in the enumerator.
					var sourceFileName;
					var destFileName;
					var sourceFileDate;
					var destFileDate;
					
					//loop through each file in the enumerator.
					while(!objEnum.atEnd())
					{
						alert (source);
						sourceFileName = objEnum.item();
						sourceFileDate = new Date(sourceFileName.DateLastModified);
						//create the destination file name by concatenating the detsination path
						//with the file name from the source file.
						destFileName = dest + "\\" + sourceFileName.Name;
						
						alert(sourceFileDate);
						
						//display the name of the file
						//document.write("<BR>" + fileName.Name);
						//document.write("<BR>" + new Date(fileName.DateLastModified).toDateString());
					
						//check if the file has been modified, 32 means has been modified.
						if(sourceFileName.Attributes==32)
						{
							//check if destination file exists
							if(fs.FileExists(destFileName))
							{
								alert("yes");
								//check if the destination file exists.
								if(fs.FileExists(destFileName))
								{
									//get the date last modified of the destination.
									destFileDate = new Date(sourceFileName.DateLastModified).toDateString();
									//if the last date modified of the source is greater then
									//destination last date modified.
									if(sourceFileDate > destFileDate)
									{
										//rename the destination file by appending "_old"
										//.....put your code here for renaming file.
										
										//copy the file from the source to the destination.
										copyFile(sourceFileName,destFileName);
										
										//change the fill attribute to 0.
										//.....put your code here for changing the file attribute.
									}
									else
									{
										alert("File doesn't need copying");
								}
							}
							else
							{
								alert("File doesn't exist please copy the files");
							}
						}	
						alert(sourceFileName + "-" + destFileName);	
						//move enumerator to the next object.
						objEnum.moveNext();
			
					}
	
				}
	
	
				
					//this function is the start of the program.
					function runProgram()
					{
							var fs = new ActiveXObject("Scripting.FileSystemObject");
							//to get the value of the source folder textbox.
							var sourcePath = document.getElementById("from").value;
							//to get the value of the destination folder textbox.
							var destPath = document.getElementById("to").value;
							//Check if source folder exists.
							var sourceFolderExists = checkExistingFolder(sourcePath);
							//Check if destination folder exists.
							var destFolderExists = checkExistingFolder(destPath);
						
								//if source folder exists.
								if (sourceFolderExists)
								{
									//continue with the process below.
									//if destination folder exists.
								
									if (destFolderExists)
									{
										//get the file collections.
										loopThroughFiles(sourcePath,destPath);
									}
									else
									{
										//create a folder and copy all files.
										//proceed with copying all files.
										fs.CopyFolder(sourcePath,destPath);
										alert("Folder has been copied");
									}
								}	
								else
								{
										//Otherwise display a error message.
										alert("The source folder (FROM: ) doesn't exist" + "check your entry and try again");
								}
					}						
				
						
				//-->
			</SCRIPT>
	</HEAD>
	<BODY bgcolor="brightred">
		<CENTER>
			<H1>Copy Project Files</H1>
			FROM: <input type="text" id="from" value="F:\\Project" />
			TO: <input type="text" id="to" value="D:\\Project"/>
			<input type="button" onclick="runProgram()" value="Transfer Project Files" />
			<SCRIPT language="JavaScript">
			<!--
				//Call function here
				
			//-->
			</SCRIPT>
		</CENTER>
	</BODY>
</HTML>
Adrian1995 is offline   Reply With Quote
Old 08-19-2012, 10:10 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,236
Thanks: 59
Thanked 3,997 Times in 3,966 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Why did you say "VBScript" when clearly all of that code is in JavaScript?

I do hope you know this code will only work:
(a) in MSIE
(b) if the user has marked your site as a "trusted" site (or if it is run locally, of course)
(c) if the user allows the script to run after getting a message that the code is marked as "unsafe for scripting".

Anyway, all that code is horribly clumsy and ugly. And it really would be easier for me to throw it away and code it in VBScript. Since this will only work in MSIE, why not use VBScript?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:01 AM.


Advertisement
Log in to turn off these ads.