Solved I want to Zip the files using vbs script

June 5, 2013 at 05:15:27
Specs: Windows 7
Can onyone please help me to zip the files using VB script ?

My requirement is like:

There will be two files in the folder C:\Users\MEDICARE\
File1 = MEDICARE_%CCYY%%MM%%DD%_CHANGE.txt
FIle2 = MEDICARE_%CCYY%%MM%%DD%_FULL.txt

CCYY = Current year like 2013
MM = Current month like 06
DD = Current date like 05

The above files come to this folder C:\Users\MEDICARE\ with current date.

The above files will not be there in the folder togethter. Some times MEDICARE_%CCYY%%MM%%DD%_CHANGE.txt will be there or some times MEDICARE_%CCYY%%MM%%DD%_FULL.txt

So we need to have if condition here, Whichever file is availbe we need to ZIP that file.

For eg..

If MEDICARE_%CCYY%%MM%%DD%_CHANGE.txt is availbe in C:\Users\MEDICARE\
Then we need to zip the file like C:\Users\MEDICARE\MEDICARE_%CCYY%%MM%%DD%_CHANGE.zip

If MEDICARE_%CCYY%%MM%%DD%_FULL.txt is availbe in C:\Users\MEDICARE\
Then we need to zip the file like C:\Users\MEDICARE\MEDICARE_%CCYY%%MM%%DD%_FULL.zip

Please help me to get this done in VB script


See More: I want to Zip the files using vbs script

Report •


✔ Best Answer
June 11, 2013 at 06:31:53
Replace "c:\vbs\" with whatever folder you're trying to enumerate. "C:\Users\MEDICARE\", I suspect.

How To Ask Questions The Smart Way



#1
June 5, 2013 at 21:17:37
set w=createobject("wscript.shell")
d=datepart("yyyy",date)&right("0"&datepart("m",date),2)&right("0"&datepart("d",date),2)
folder="C:\USERS\MEDICARE\"
f=folder&"MEDICARE_"&d
f1=f&"_FULL"
f2=f&"_CHANGE"
w.run "zip "&f1&".zip "&f1&".txt 2>nul"
w.run "zip "&f2&".zip "&f2&".txt 2>nul"
'======== end, not tested, way too many unknowns.


Report •

#2
June 6, 2013 at 03:31:42
Thanks a lot for the response nbrane,

When i execute the below code im getting error.

set w=createobject("wscript.shell")
d=datepart("yyyy",date)&right("0"&datepart("m",date),2)&right("0"&datepart("d",date),2)
folder="\\netapp04b.trhp.com\public\Rowan\Suhail\5013_StatefleEvidence\"
f=folder&"MEDVANTX_"&d
f1=f&"_FULL"
f2=f&"_CHANGE"
Wscript.echo(f1)
w.run "zip "&f1&".zip "&f1&".txt 2>nul"
w.run "zip "&f2&".zip "&f2&".txt 2>nul"

I executed the above code from command prompt by saving with .vbs.

Result :

C:\Documents and Settings\sabba10\Desktop> Cscript 2361zip.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

\\netapp04b.trhp.com\public\Rowan\Suhail\5013_StatefleEvidence\MEDVANTX_20130606_FULL
C:\Documents and Settings\sabba10\Desktop\2361zip.vbs(9, 1) (null): The system c
annot find the file specified.


Report •

#3
June 6, 2013 at 12:04:43
I don't know what program you intended to use to zip the files, so I just used a generic place-holder ("zip"). You'll need to determine the exact name and location of you zipper-upper. (my ancient PKZIP choked on the longfilenames.) I'm not sure if that's what kicked the error (since line 8 should have kicked it before line 9).

Report •

Related Solutions

#4
June 7, 2013 at 11:02:45
Still don't have time, so here's two hints, nbrane:
1) I call VBScript Windows' native scripting language not only because it's on the greatest number of machines, but also because it interacts with Windows' native shell, Explorer.

2) Explorer can add files to a zip file.

How To Ask Questions The Smart Way


Report •

#5
June 7, 2013 at 17:40:02
@razor: Ha ha! Add that to the long list of things I don't know! ;-)
ps: now that you mention it, i do recall that "send-to" option for compressed files, just never used it.
Here's adaptation I googled-up:
set fso=createobject("scripting.filesystemobject")
set shellapp=createobject("shell.application")
set f=shellApp.NameSpace( "c:\vbs\" ).Items
d=datepart("yyyy",date)&right("0"&datepart("m",date),2)&right("0"&datepart("d",date),2)
folder="\\netapp04b.trhp.com\public\Rowan\Suhail\5013_StatefleEvidence\"
fname="MEDVANTX_"&d
fbase1=fname&"_FULL"
fbase2=fname&"_CHANGE"
set f=shellApp.NameSpace( folder).Items
on error resume next 'a "cheaty" way to see if the files exist
full=f.items(fbase1&".txt")
change=f.items(fbase2&".txt")
on error goto 0
if full<>"" then
zip1=fbase1&".zip"
fso.opentextfile(zip1,2,true).write("PK"&chr(5)&chr(6)&string(18,chr(0)))
shellApp.NameSpace( zip1.zip" ).CopyHere f.item(full)
else
'put notification of missing file FULL here
end if
if change<>"" then
zip2=fbase2&".zip"
fso.opentextfile(zip2,2,true).write("PK"&chr(5)&chr(6)&string(18,chr(0)))
shellApp.NameSpace(zip2).CopyHere f.item(change)
else
'put notification of missing file CHANGE here
end if
'====== end
I had a lot of trouble getting my prototype to work right, so getting this thing to work is problematical. I learned that the zipfiles must have a .zip extension.

Report •

#6
June 11, 2013 at 01:18:54
Thanks again for the above script..

Im still getting error . See below

C:\Documents and Settings\sabba10\Desktop\2361zip.vbs(3, 1) Microsoft VBScript r
untime error: Object required: 'shellapp.NameSpace(...)'


Report •

#7
June 11, 2013 at 06:31:53
✔ Best Answer
Replace "c:\vbs\" with whatever folder you're trying to enumerate. "C:\Users\MEDICARE\", I suspect.

How To Ask Questions The Smart Way


Report •


Ask Question