Solved

Vb script - copy files

Posted on 2014-11-03
13
181 Views
Last Modified: 2014-11-05
Hi

Can someone help me, I am trying to write a vb script that will do the following

1. Check certain files e.g cars.txt, bacs1.txt, maint.txt exist in a file location \\cslwinapp08\bacs, check that three files exist if not report, log error 'files don't exist'
2. if these three files exist in \\cslwinapp08\bacs copy these files to \\cslwinapp10\bacs
3. Check that these files have been copied to \\cslwinapp10\bacs and report a log to confirm 'files copied'

Thanks
0
Comment
Question by:rakkad
  • 6
  • 5
  • 2
13 Comments
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40420580
something like this perhaps?

dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "\\cslwinapp08\bacs\"
mainfolder2= "\\cslwinapp10\bacs\"
file1="cars.txt"
file2="bacs1.txt"
file3="maint.txt"
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  'all files exist
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    msgbox "Copy Successful"
  else
    msgbox "Copy Failed"
  end if
else
  msgbox "not all files exist" 'if you want the msgbox to specify which files are missing then we have to store the results of the fileexists checkups in a variable
end if

Open in new window


EDIT: if you want to replace the files in the destination directory in case they existed, then just specify true as a third parameter to the copyfile command

ex: oFSO.CopyFile mainfolder1 & file1, mainfolder2, true
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40420677
Just to add some clarification that the "default value" of the third param (OverWriteFiles) of the CopyFile method of the FSO is True, so no need to specify or include it.  Other than that, Dany's code should do the job.
0
 

Author Comment

by:rakkad
ID: 40421296
Instead of creating a msgbox,  I would like the contents to be written to a log file

Thanks
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40421347
try this

dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "\\cslwinapp08\bacs\"
mainfolder2= "\\cslwinapp10\bacs\"
file1="cars.txt"
file2="bacs1.txt"
file3="maint.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\mylog.log", 2, True) '2 is forwriting, if you want to append to the log then use 8 instead
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  'all files exist
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist" 'if you want the msgbox to specify which files are missing then we have to store the results of the fileexists checkups in a variable
end if
mylog.Close

Open in new window

0
 

Author Comment

by:rakkad
ID: 40421924
I ran the script above, but it is saying 'not all files exist'  I have specified three different files..

Thanks
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40422099
try to access the files using explorer: (windows, run)
and type: \\cslwinapp08\bacs\
if it requires authentication enter your username and password (and tick save password)
when the explorer opens, are your 3 files there?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rakkad
ID: 40422158
I saved the file to a .vbs extension and renamed the file paths as a test e.g.  to see if the copy process worked:-

dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "c:\Temp\test1"
mainfolder2 = "c:\Temp\test2"
file1 = "A.txt"
file2 = "B.txt"
file3 = "C.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist"
end if
mylog.Close

The files exist see attachment...  Re-ran the .vbs file and files are not being copied.  Thanks
U--UploadFiles.PNG
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40422344
mainfolder1 should be: mainfolder1 = "c:\Temp\test1\" (the backslash at the end is crucial)
same for mainfolder2: mainfolder2 = "c:\Temp\test2\" <<notice the backslash at the end

final code is:
dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "c:\Temp\test1\"
mainfolder2 = "c:\Temp\test2\"
file1 = "A.txt"
file2 = "B.txt"
file3 = "C.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist"
end if
mylog.Close

Open in new window

0
 

Author Comment

by:rakkad
ID: 40423483
Thanks for the update.

I changed the path and it works.. Great !!

I would like an additional development on the script, can you include a variable to state what files are missing i.e. if they do not exist ?

Thanks
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 500 total points
ID: 40423584
dim mainfolder1, mainfolder2, file1, file2, file3
dim file1exists, file2exists, file3exists
mainfolder1 = "c:\Temp\test1\"
mainfolder2 = "c:\Temp\test2\"
file1 = "A.txt"
file2 = "B.txt"
file3 = "C.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
file1exists=oFSO.FileExists(mainfolder1 & file1)
file2exists=oFSO.FileExists(mainfolder1 & file2)
file3exists=oFSO.FileExists(mainfolder1 & file3)
If file1exists and file2exists and file3exists Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  if not file1exists then 
    mylog.WriteLine mainfolder1 & file1 & " does not exist."
  end if
  if not file2exists then 
    mylog.WriteLine mainfolder1 & file2 & " does not exist."
  end if
  if not file3exists then 
    mylog.WriteLine mainfolder1 & file3 & " does not exist."
  end if
end if
mylog.Close

Open in new window

0
 

Author Comment

by:rakkad
ID: 40423708
The three different files need to have wildcards e.g.

Y01A.CARS*.*
GPL.MT940*.*
GPL.CRCSV*.*

As these files change on a daily basis, so... like below:-


dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "c:\Temp\test1\"
mainfolder2 = "c:\Temp\test2\"
file1 = "Y01A.CARS*.*"
file2 = "GPL.MT040*.*"
file3 = "GPL.CRCSV*.*"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1 ) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist"
end if
mylog.Close

but the log file states not all files exist

Can you help thanks again ?
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40423771
ok, if you copy "c:\Temp\test1\Y01A.CARS*.*" to another folder, it works.. however you can't check if "c:\Temp\test1\Y01A.CARS*.*" exists.. because the function expects a file name.. so you have to remove your checkups and just make the script copy files for you.. or you would have to loop through the folder to see if any file matching that criteria exists and then proceed.. which is a totally new project and question! you can post another question and see if other experts can help you with that part!
good luck
dan
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40425212
If I were the Asker, I would accept Dany's solution right away!  He has tirelessly provided a very useful and thorough solution.  The original questions have already been answered, so why not accept his work now and close the question?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBSCRIPT in Page Load C# ASP.NET 6 61
Protecting vb6 & .Net code Obfuscation 18 99
VB Script to add site to Java Exception List 4 53
ASP CDO - Limiting Connections 2 41
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now