Solved

Copy only is the file does not exits

Posted on 2009-07-08
11
247 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

0
Comment
Question by:Steynsk
[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
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 10

Accepted Solution

by:
TakedaT earned 250 total points
ID: 24803785
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24804034
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
 
LVL 1

Author Comment

by:Steynsk
ID: 24805109
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 21

Expert Comment

by:AmazingTech
ID: 24806625
FileExist not FileExists. Plus I think you'll need a "\"

if not FSO.FileExist(strVerwerkt & "\" & file.name) then
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 250 total points
ID: 24806656
Nope. You're right it's FileExists.

if not FSO.FileExists(strVerwerkt & "\" & file.name) then
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24808338
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24808562
Good eye TakedaT!
0
 
LVL 1

Author Comment

by:Steynsk
ID: 24822292
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
 
LVL 10

Expert Comment

by:TakedaT
ID: 24822781
Id be happy with a split.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24826006
Doesn't really matter to me. TakedaT, Split, accept one and assist another. Whatever you decide is fine by me.
0
 
LVL 1

Author Closing Comment

by:Steynsk
ID: 31601098
Thanks a lot
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question