Vb script - copy files

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
rakkadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dany BalianCTOCommented:
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
jkaiosIT DirectorCommented:
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
rakkadAuthor Commented:
Instead of creating a msgbox,  I would like the contents to be written to a log file

Thanks
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Dany BalianCTOCommented:
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
rakkadAuthor Commented:
I ran the script above, but it is saying 'not all files exist'  I have specified three different files..

Thanks
0
Dany BalianCTOCommented:
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
rakkadAuthor Commented:
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
Dany BalianCTOCommented:
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
rakkadAuthor Commented:
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
Dany BalianCTOCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rakkadAuthor Commented:
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
Dany BalianCTOCommented:
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
jkaiosIT DirectorCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.