Solved

Zip selected files

Posted on 2013-12-08
10
282 Views
Last Modified: 2013-12-11
I have this script that checks for selected files in "strsource". From there I need to be able to zip these files to "strdest".  I really don't know how to do that.


' Isalogs & Export are shared folders

Dim Strdate, StrSource, StrDest, Fso, objFolder, colFiles, objFile, strFile

Set fso = CreateObject("Scripting.FileSystemObject")

set wshshell = wscript.createobject("wscript.shell")

strDate = Date()
strSource = "\\isaserver\isalogs\"
strDest   = "\\isaserver\export\"

If fso.FolderExists(strSource) Then
   Set objFolder = fso.GetFolder(strSource)
   Set colFiles = objFolder.Files
End if
 
For Each strFile in colFiles
    if InStr(strfile.datecreated, strdate) > 0 then
   ??????????????????      
    end if
Next
0
Comment
Question by:Bianchi928
  • 5
  • 4
10 Comments
 

Author Comment

by:Bianchi928
ID: 39705616
Forgot to mention that I have 'winrar' installed on the computer
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39705651
This is bad style:
   if InStr(strfile.datecreated, strdate) > 0 then

Open in new window

You should always compare dates with dates. Using string comparision provokes issues.

I take it the target ZIP files do not exist yet, and you want to create a single ZIP for each source file. I'm not clear about whether you want to copy or move the files, however.
' Isalogs & Export are shared folders

Dim dt, strSrc, strDst, fso, srcFile, dstFile

dt        = Date() 
strSrc = "\\isaserver\isalogs\"
strDst = "\\isaserver\export\"

Set fso  = CreateObject("Scripting.FileSystemObject") 
set shApp= CreateObject("Shell.Application")

For Each srcFile in fso.GetFolder(strSrc).Files
  If srcFile.DateCreated >= dt Then
    dstFile = strDst + srcFile.BaseName + '.zip'
    If not fso.FileExists(dstFile) Then
      With fso.CreateTextFile(dstFile, True)
        .Write "PK" + chr(5) + char(6) + String(18, chr(0))
      End With
    shApp.NameSpace(dstFile).CopyHere srcFile.FullName
    WScript.Sleep 250
  End If
Next

Open in new window

This copies the files. The sleep is recommended as the compression is done in the background, and the code not waiting for completion. If you do not wait, you'll start a compression thread for each file (almost) simultanously, slowing down all file operations.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39705656
Ah, WinRAR, but you do not need it here, as you can see.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Bianchi928
ID: 39706959
Hi,

Thanks for your answer...I have done some changes to your script as it was coming with some errors. I also modifi3ed the date check for testing purposes. Now I'm getting an error message

Object required shApp.NameSpace(...).

Also , I forgot to say that I want all selected file to be zipped together.


I will really appreciate if I can also have the script lines if I wanted to use winrar.

Thanks
Cheers




Dim dt, strSrc, strDst, fso, srcFile, dstFile

dt        = Date() - 1
strSrc = "\\isaserver\isalogs\"
strDst = "\\isaserver\export\"
Newname    = replace(date,"/","")
ZipFile    = "Isalogs" & newname & ".zip"

Set fso  = CreateObject("Scripting.FileSystemObject")
set shApp= CreateObject("Shell.Application")


For Each srcFile in fso.GetFolder(strSrc).Files
  If srcFile.DateCreated >= dt Then
     If not fso.FileExists(zipfile) Then
      With fso.CreateTextFile(strdst & zipfile, True)
        .Write "PK" + chr(5) + chr(6) + String(18, chr(0))
      End With
     End if
     shApp.NameSpace(zipFile).CopyHere srcFile.Name
     WScript.Sleep 250
  End If
Next
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39707239
For the integrated ZIP folder feature you need to use full paths; zipfile is only the filename. Further you used date instead of dt
Dim dt, strSrc, strDst, fso, srcFile, dstFile, ZipFile

dt        = Date() - 1
strSrc = "\\isaserver\isalogs\"
strDst = "\\isaserver\export\"
ZipFile    = strDst + "Isalogs" + replace(dt,"/","") + ".zip"

Set fso  = CreateObject("Scripting.FileSystemObject") 
set shApp= CreateObject("Shell.Application")

For Each srcFile in fso.GetFolder(strSrc).Files
  If srcFile.DateCreated >= dt Then
     If not fso.FileExists(zipfile) Then
      With fso.CreateTextFile(zipfile, True)
        .Write "PK" + chr(5) + chr(6) + String(18, chr(0))
      End With
     End if
     shApp.NameSpace(zipFile).CopyHere srcFile.FullName
     WScript.Sleep 250
  End If
Next

Open in new window

Using WinRAR would look like (you might have to tweak the options):
Dim dt, strSrc, strDst, fso, srcFile, dstFile, ZipFile, shell

dt        = Date() - 1
strSrc = "\\isaserver\isalogs\"
strDst = "\\isaserver\export\"
ZipFile    = strDst + "Isalogs" + replace(dt,"/","") + ".zip"

Set fso  = CreateObject("Scripting.FileSystemObject") 
set shell= CreateObject("WScript.Shell")

For Each srcFile in fso.GetFolder(strSrc).Files
  If srcFile.DateCreated >= dt Then strFiles = strFiles + " " + srcFile.FullName
Next
If strFile != "" Then
  shell.Run "winrar a -afzip " + ZipFile + strFiles
End If

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 39707296
Qlemo,

In the revised script, I have a problem with Line 18

Object doesn't support the property or method of  ;'Fullname'
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 300 total points
ID: 39707387
Sorry for that, this part was taken over from a PowerShell script, which is different here. I see we have to use Path instead:
Dim dt, strSrc, strDst, fso, srcFile, dstFile, ZipFile

dt        = Date() - 1
strSrc = "\\isaserver\isalogs\"
strDst = "\\isaserver\export\"
ZipFile    = strDst + "Isalogs" + replace(dt,"/","") + ".zip"

Set fso  = CreateObject("Scripting.FileSystemObject") 
set shApp= CreateObject("Shell.Application")

For Each srcFile in fso.GetFolder(strSrc).Files
  If srcFile.DateCreated >= dt Then
     If not fso.FileExists(zipfile) Then
      With fso.CreateTextFile(zipfile, True)
        .Write "PK" + chr(5) + chr(6) + String(18, chr(0))
      End With
     End if
     shApp.NameSpace(zipFile).CopyHere srcFile.Path
     WScript.Sleep 250
  End If
Next

Open in new window

respective
Dim dt, strSrc, strDst, fso, srcFile, dstFile, ZipFile, shell

dt        = Date() - 1
strSrc = "\\isaserver\isalogs\"
strDst = "\\isaserver\export\"
ZipFile    = strDst + "Isalogs" + replace(dt,"/","") + ".zip"

Set fso  = CreateObject("Scripting.FileSystemObject") 
set shell= CreateObject("WScript.Shell")

For Each srcFile in fso.GetFolder(strSrc).Files
  If srcFile.DateCreated >= dt Then strFiles = strFiles + " " + srcFile.Path
Next
If strFile != "" Then
  shell.Run "winrar a -afzip " + ZipFile + strFiles
End If

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 39712581
Good job. Thanks a lot
0
 

Author Comment

by:Bianchi928
ID: 39712953
Cool man. Thanks again

Cheers
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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