oopsiee
oopsiee

Reputation: 11

How to check if files in a path are of a particular type (in VB script)?

I currently have a batch file which calls a vbscript that actually does the work I need (coz I have no experience with doing batch files). Take note, I just got this code off the internet.

What I want to happen in the VB Script Basically, I will provide a path and the code will check if specific files (either CSV or XML) exist. If these files exist, they will be placed in a zip file.

What is happening in the VB Script Currently, the vbscript will get all the files in the path provided and all of it will be zipped into one file. I want to change this code so that it will only zip files that are CSV for example.


Batch code

CScript czip.vbs "C:\Users\donatoma\Documents\Folder1\" "C:\Users\donatoma\Documents\Folder2\CSV files.zip"

VB Script

'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items <-- GET CSV FILES, ADD TO AN ARRAY

objShell.NameSpace(ZipFile).CopyHere(source)

'Required!
wScript.Sleep 2000

UPDATED CODE

Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

For i = 0 to source.Count - 1
    If InStr(".csv", Right(source.item(i).Name, 4)) > 0 Then
        objshell.Namespace(ZipFile).CopyHere (source.item(i))
    End If
Next

Upvotes: 0

Views: 3730

Answers (1)

Fionnuala
Fionnuala

Reputation: 91326

Check the name.

CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objshell = CreateObject("Shell.Application")

Set Source = objshell.Namespace(InputFolder).Items 

For i = 0 To Source.Count - 1
    If InStr(".csv,.xml", Right(Source.item(i).Name, 4)) > 0 Then
        objshell.Namespace(ZipFile).CopyHere (Source.item(i))
        WScript.Sleep 200
    End If 
Next

Upvotes: 1

Related Questions