Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1496
  • Last Modified:

7-Zip Scripting Issue

Hi All,

Let me explain my issue first. We are planning to do the following:

1.) 7z (Zip) up the files from a certain directory that is greater than 2 weeks old from the current date.
2.) This script will be in a task scheduler run every month.

Current Script:
----------------------------------------------------------------------------------------------

Dim objFSO :Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFolder = "C:\XXX"

intArch = 14

dtOld = DateAdd("d", -intArch, Date)

Set objFolders = objFSO.GetFolder(strSourceFolder)

Set wshShell = WScript.CreateObject ("WSCript.shell")

For Each fil In objFolders.Files
  If DateDiff("d", fil.datelastmodified, dtOld) >= intArch Then
    wshshell.run "C:\Program Files\7-Zip\7z.exe a c:\Test.7z" & fil.path  
End If
Next

set wshshell = nothing
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit

----------------------------------------------------------------------------------------------

Theoretically this script should find all the files in the C:\XXX Directory and then if a file is greater then 2 weeks old it will archive the data into Test.7z.

The problem is that it never seems to actually run the 7z.exe in cmd.

In cmd the exact code is as follows:

"C:\Program Files\7-Zip\7z.exe" a c:\Test.7z c:\Path Of What to Zip

In my code I have:

wshshell.run "C:\Program Files\7-Zip\7z.exe a c:\Test.7z" & fil.path

where fil.path will be "c:\Path Of What to Zip" but I even replaced the fil.path with a standard directory and it doesn't work either it just seems like my command after "wshshell.run" is off.

Any advice is GREATLY appreciated.

THANKS!
0
PssTech
Asked:
PssTech
  • 6
  • 6
  • 4
2 Solutions
 
Bill PrewCommented:
One problem I see is the fact that the 7z EXE path contains spaces.  As a result I think you will need to add double quotes to the RUN command line, as in:

wshshell.run """C:\Program Files\7-Zip\7z.exe""" & "a c:\Test.7z" & fil.path

(assuming fil.path does not contain embedded spaces)
~bp
0
 
PssTechAuthor Commented:
Thanks for the quick response...

I changed it to your way in my code and get a VBS error. I think it's because of the concatenation.

I then changed it to the following:

wshshell.run """C:\Program Files\7-Zip\7z.exe""" & " a c:\Test.7z " & fil.path

It always seems like it runs but it never actually does anything I even set my intArch to 1 day so I would definitely get results. I still get no results.

Any other thoughts?
0
 
Steve KnightIT ConsultancyCommented:
it seems to me like you havent got quotes around your shell line for the program files dir due to the spaces, try this:

wshshell.run """c:\program fikes\u-zip\7z.exe"" a c:\test.7z " & fil.path

i.e where you need  " you use "" plus the " around your literal strings

steve

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
Steve KnightIT ConsultancyCommented:
sorry bill was slow typing there....

Try changing the wshshell.run for wscript.echo and then it will show you on screen the commands it is trying to run.

Steve
0
 
Steve KnightIT ConsultancyCommented:
oh yes and can't type....  files not fikes and 7-zip not a-zip of course.

Steve
0
 
Bill PrewCommented:
As Steve suggests, let's see the exact command being executed.  Add the Echo line above the Run, as in:

Wscript.Echo """C:\Program Files\7-Zip\7z.exe""" & " a c:\Test.7z " & fil.path
wshshell.run """C:\Program Files\7-Zip\7z.exe""" & " a c:\Test.7z " & fil.path

And post what that looks like.

~bp
0
 
PssTechAuthor Commented:
Guys you are AWESOME both ways worked and I appreciate it greatly!
0
 
Steve KnightIT ConsultancyCommented:
no problem... Sorry for diving in there bill :-)

steve
0
 
PssTechAuthor Commented:
Hey guys another question!

What if the fil.path has a space in it and I need to put quotes around that for example:

fil.path = C:\XXX\S P A C E

How do I enforce parentheses on this variable? I tried the following with no luck:

"""C:\Program Files\7-Zip\7z.exe"" a c:\Test.7z " & " & fil.path &  " -> Returns & fil.path &
"""C:\Program Files\7-Zip\7z.exe"" a c:\Test.7z " & "" & fil.path &  "" -> Returns C:\XXX\S P A C E (No ")
"""C:\Program Files\7-Zip\7z.exe"" a c:\Test.7z " & """ & fil.path &  """ -> Returns "& fil.path &"

Thanks!
0
 
Bill PrewCommented:
No problem Steve, I missed the spaces between parms so that's cool.

You are certainly crushing MSDOS this month, way to go!

Real work keeping me from too much EE, but I guess that's a good problem to have.

~bp
0
 
Bill PrewCommented:
Here's my preferred way to do this, I have a small function that adds double quotes around the string passed to it.  I find it much easier to read in the code...

Wshshell.Run Quote(C:\Program Files\7-Zip\7z.exe) & " a c:\Test.7z " & Quote(fil.path)

' Add surrounding double quotes to a string
Function Quote(s)
   Quote = Chr(34) & s & Chr(34)
End Function

Open in new window

~bp
0
 
PssTechAuthor Commented:
Perfect! Excellent, THANK YOU!
0
 
Bill PrewCommented:
Welcome.

~bp
0
 
Steve KnightIT ConsultancyCommented:
Bill -- real work picking up luckily, school half term, and son #3 is 2 tomorrow so you should be able to catch up a bit ;-)
0
 
Bill PrewCommented:
@Steve,

Good to hear you'll be distracted for a bit (and I mean that with sincerity, but heck, your are the "freight train" this month as I think you once referred to me as :-) ).

I may wait up for you a bit, it would be kinda cool if we could both hit 1000000 on the same question, but I guess that would be a bit of a stretch, wouldn't it.

Happy birthday to #3, enjoy the festivities.

~bp
0
 
Steve KnightIT ConsultancyCommented:
Thanks Bill - heh what the hell 1,000,000 wise... though someone will go and close an old Q and spoli it :-)
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now