Link to home
Start Free TrialLog in
Avatar of elwayisgod
elwayisgodFlag for United States of America

asked on

Need to .zip in a .bat and use built in functionality in Win Server 2016

Windows Server 2016 - Need to zip up all *.txt files in a directory.  Client doesn't want to install 7zip.  So was thinking Powershell but I have never used it or know how to use it.

My current scripts are .bat

Can I run PShell within a .bat?


I found this example, but not sure how to actually execute it.   Can I use my variables I set in my .bat?  

Very confused.
Avatar of McKnife
McKnife
Flag of Germany image

https://4sysops.com/archives/zip-and-unzip-with-powershell explains and gives a usage example for powershell.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elwayisgod

ASKER

I run:

@echo off
setlocal
set Source=%LOGDIR%\*.log
set Target=%ARCHIVEDIR%\PBCS\Backups\%Timestamp%\%Timestamp%\Logs.zip
powershell.exe -Command "Get-ChildItem '%Source%' | Compress-Archive -DestinationPath '%Target%' -Force"'

Open in new window


Results in:

User generated image
crap... I see it... Hold on...
Avatar of oBdA
oBdA

You mean the additional single quote at the end of the last line in your script? :)
Nope... Same Error after changing my Target path....

@echo off

setlocal
set Source=%LOGDIR%\*.log
set Target=%ARCHIVEDIR%\PBCS\Backups\%Timestamp%\%Timestamp%_Logs.zip
powershell.exe -Command "Get-ChildItem '%Source%' | Compress-Archive -DestinationPath '%Target%' -Force"'

exit /b

Open in new window

Looks like you have an extra trailing single quote, remove that.

powershell.exe -Command "Get-ChildItem '%Source%' | Compress-Archive -DestinationPath '%Target%' -Force"'


»bp
didn't see that....Stand by
See https://www.experts-exchange.com/questions/29144411/Need-to-zip-in-a-bat-and-use-built-in-functionality-in-Win-Server-2016.html?anchorAnswerId=42854296#a42854296
Have a look at the very last character in the "powershell ..." line - in your script, it ends with a single quote, which shouldn't be there. The last character should be the closing double quote after -Force
Bingo!! My blind butt :)