Solved

Copy only is the file does not exits

Posted on 2009-07-08
11
245 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to open text file 11 110
Command prompt output to variable? 9 45
Problem to cell option 1 28
Assigning VBA code to a specific worksheet 3 24
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 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