Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Copy only is the file does not exits

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

0
Steynsk
Asked:
Steynsk
  • 4
  • 3
  • 3
  • +1
2 Solutions
 
TakedaTCommented:
You can probably use the If not FSO.FileExists method before the copy.  Im not sure what you mean by the statement "And all duplicate files should be filed in a array." though.
0
 
t0t0Commented:
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.
0
 
SteynskAuthor 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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

if not FSO.FileExist(strVerwerkt & "\" & file.name) then
0
 
AmazingTechCommented:
Nope. You're right it's FileExists.

if not FSO.FileExists(strVerwerkt & "\" & file.name) then
0
 
TakedaTCommented:
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.

0
 
AmazingTechCommented:
Good eye TakedaT!
0
 
SteynskAuthor 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
0
 
TakedaTCommented:
Id be happy with a split.
0
 
AmazingTechCommented:
Doesn't really matter to me. TakedaT, Split, accept one and assist another. Whatever you decide is fine by me.
0
 
SteynskAuthor Commented:
Thanks a lot
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now