Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •