We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Copy only is the file does not exits

Medium Priority
270 Views
Last Modified: 2012-05-07
Hi Expert,

I've got this script (see beneath) that copies files to a directory but no I have to add a condition.

The file should only be copied if it does not exists in the target directory.
And all duplicate files should be filed in a array.

Thanks
Option Explicit
Const ForReading = 1
Const ForWriting = 2 
Const FOF_CREATEPROGRESSDLG = &H0& 
Dim i, networkdrive, Network_share, Drives, strPath, strTempPath, WshNetwork, objShell, objFolder, WshShell, FSO, objTextFile, FLD, fil, strOldName, strNewName, strFileParts, strDateParts, strdate, strVerwerkt
Dim strMade, strUnique, templocation, scriptname, numbersfile, username, f1_out, f2_out, Mycmd, rval, WMQ, wmi, PRS, GO, aFile, a1File
 
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
 
Network_share="\\zkh\appdata\CDLjava"
networkdrive="O:"
strPath = "O:\cdldata\application"
strTempPath = "O:\cdldata\application\temp" 
strVerwerkt = "O:\cdldata\application\verwerkt" 
 
Set Drives = WshNetwork.EnumNetworkDrives
For i = 0 to Drives.Count - 1 Step 2
   if Drives.Item(i)=networkdrive Then
		WshNetwork.RemoveNetworkDrive networkdrive, True, True
   end if
Next
WshNetwork.MapNetworkDrive networkdrive, Network_share
 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(strVerwerkt) 
 
objFolder.CopyHere strPath & "\*.pdf", FOF_CREATEPROGRESSDLG

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
The simplest way to achieve this is using DOS. It would be in the following for:

@echo off
set source=c:\temp
set destination=d:temp

for /f "tokens=*" %%a in ('dir /a-d /b %source%\*.pdf') do (
   if not exist %destination%\%%~nxa (
      copy "%%a" "%destination%\"
   )
)
 

It's what DOS does best.

Author

Commented:
Hi TakedaT,

Yes I've tried to solve the problem by using FSO.FileExists but get an error on the line that usses it.

I get error:  Error 800A01A8 Object Required on line 37  

This is the line:

if not FSO.FileExists(strVerwerkt & file.name) then

Thanks,

Steynsk

Option Explicit
Const ForReading = 1
Const ForWriting = 2 
Const FOF_CREATEPROGRESSDLG = &H0& 
Dim i, networkdrive, Network_share, Drives, strPath, strTempPath, WshNetwork, objShell, objFolder, WshShell, FSO, objTextFile, FLD, fil, strOldName, strNewName, strFileParts, strDateParts, strdate, strVerwerkt
Dim strMade, strUnique, templocation, scriptname, numbersfile, username, f1_out, f2_out, Mycmd, rval, WMQ, wmi, PRS, GO, aFile, a1File, fldr, file
 
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
 
Network_share="\\server\appdata\data"
networkdrive="O:"
strPath = "O:\application\test"
strTempPath = "O:\application\temp" 
strVerwerkt = "O:\application\verwerkt\test" 
 
Set Drives = WshNetwork.EnumNetworkDrives
For i = 0 to Drives.Count - 1 Step 2
   if Drives.Item(i)=networkdrive Then
		WshNetwork.RemoveNetworkDrive networkdrive, True, True
   end if
Next
WshNetwork.MapNetworkDrive networkdrive, Network_share
 
Set objFolder = objShell.NameSpace(strVerwerkt) 
 
objFolder.CopyHere strPath & "\*.pdf", FOF_CREATEPROGRESSDLG
 
 
        Set objTextFile = FSO.OpenTextFile ("counter.txt", ForReading)
		strUnique = objTextFile.ReadAll
		objTextFile.Close
		Set FLDR = FSO.GetFolder(strPath)
        for each fil in fldr.files
      		if not FSO.FileExists(strVerwerkt & file.name) then
	        	if right(fil.Path, 4) = ".pdf" then
	               strUnique = strUnique +1
	               strOldName = fil.Path
	               strMade = fil.DateLastModified
	               If InStr(strOldName, "_") > 0 Then
	                    strFileParts = Split(strOldName, "_")
						strdate= Year(strFileParts(1)) & Right("0" & Month(strFileParts(1)),2) & Right("0" & Day(strFileParts(1)), 2)
						strMade = Year(strMade) & Right("0" & Month(strMade),2) & Right("0" & Day(strMade), 2)
	                    strNewName = strFileParts(0) & "_" & strdate & "_" & strMade & "_PSA_30" & "_" & strUnique & "_10000000.pdf"
	                  	strNewName = replace(strNewName, strPath, strTempPath)
	                    FSO.MoveFile strOldName, strNewName
	               End If
				end if
			end if               
        Next
        Set objTextFile = FSO.OpenTextFile ("counter.txt", ForWriting)
		objTextFile.Write strUnique
		objTextFile.Close
 
        
        
			templocation = WshShell.ExpandEnvironmentStrings("%temp%")
			scriptname = templocation & "\upload.ftp"
			numbersfile = templocation & "\info.txt"
			username = WshShell.ExpandEnvironmentStrings("%username%")
  
			
			'------------------------------------------------
			'  Create a file that later gets uploaded and holds
			'  information about this session.
			'------------------------------------------------
			
			set f1_out = FSO.CreateTextFile(numbersfile,TRUE) ' New file
			Set FLD = FSO.GetFolder(strTempPath)
			f1_out.writeline("Date/time :" & NOW()) 
			f1_out.writeline("Number of files in the upload dir  :" & FLD.Files.Count)
			f1_out.writeline("From local directory  :" & strTempPath)			
			f1_out.writeline("Files uploaded by  :" & username)
 
			'------------------------------------------------
			'  Create new ftp script with current filenames
			'------------------------------------------------
						
			set f2_out = FSO.CreateTextFile(scriptname,TRUE) ' New file
			f2_out.writeline("username") ' log-on with User ID
			f2_out.writeline("password")
			f2_out.writeline("binary") 
			f2_out.writeline("lcd " & chr(34) &  strTempPath & chr(34)) 
			f2_out.writeline("cd test")
			For Each fil in FLD.Files
			     if LCase(right(fil.name,4)) = ".pdf" THEN
			        f2_out.writeline("put " & chr(34) & fil.name & chr(34))
			        f1_out.writeline(chr(34) & fil.name & chr(34))     
			     END if
			Next
			f2_out.writeline("bye")  ' log off ftp session
			f2_out.Close
			f1_out.Close
			
			'------------------------------------------------
			'  Build the command and start the real update
			'------------------------------------------------			
			
			mycmd = "ftp -s:" & scriptname & " hostname"
			rval = WshShell.Run("%comspec% /c " & mycmd ,1,TRUE) 
			
			'------------------------------------------------
			'  Check if proces "ftp.exe" still is active and 
			'  if it is not clean up the scriptfile.
			'------------------------------------------------
			
			set wmi=getobject("winmgmts:") 
			wmq="select * from Win32_Process where name='ftp.exe'"
			go = 0 
			Do While go = 0
				wscript.sleep(4000)
				set prs = wmi.Execquery(wmq) 
				if prs.count = 0 then
					Set aFile = FSO.GetFile(scriptname)
					Set a1File = FSO.GetFile(numbersfile)
					aFile.Delete
					rval = WshShell.Run("notepad " & a1File)
					'a1File.Delete
					go = 1
				end if
			Loop
			Set FLD = FSO.GetFolder(strTempPath)
	        For Each fil in FLD.Files
	        	if right(fil.Path, 4) = ".pdf" then
	               fso.DeleteFile(fil)
				end if               
	        Next
			
Set FLD = Nothing
Set FSO = Nothing    
WshNetwork.RemoveNetworkDrive networkdrive, true

Open in new window

Top Expert 2009

Commented:
FileExist not FileExists. Plus I think you'll need a "\"

if not FSO.FileExist(strVerwerkt & "\" & file.name) then
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
You have this in lines 36 and 37:

        for each fil in fldr.files
                if not FSO.FileExists(strVerwerkt & file.name) then

Looks like you need to either drop the  "e" in "file" in line 36 or add an "e" to the "fil.name" in all the other lines that reference it after.
And also as AmazingTech said, possibly a "/" in between the other line:
                if not FSO.FileExists(strVerwerkt & "\" & fil.name) then

However you choose, just make sure that they all match.

Top Expert 2009

Commented:
Good eye TakedaT!

Author

Commented:
TakedaT and Amazing Tech,

Yoe've helped a great deal. Thanks for that. Now I have dicide who gets the points.
Do have ideas?

Thanks
CERTIFIED EXPERT

Commented:
Id be happy with a split.
Top Expert 2009

Commented:
Doesn't really matter to me. TakedaT, Split, accept one and assist another. Whatever you decide is fine by me.

Author

Commented:
Thanks a lot
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.