Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compress-Archive works manually, but not in a .bat file?

I am trying to set up a .bat script to zip specific files in a folder. The input to this script is the folder name.

My current .bat file looks like this.

REM Takes DATE as input param
if "%1"=="" (
  echo Provide DATE as an argument ex. 20230425
  exit /b 1
) else (
  set DATA_DATE=%1
  echo Accepted.
)

REM Zip all data
Compress-Archive .\data\%DATA_DATE%\p* -f test.zip

When I execute this script with .\collect_data.bat 20230504, it fails at the Compress-Archive line with the following error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'Compress-Archive' is not recognized as an internal or external command,
operable program or batch file.

Weirdly when I remove the Compress-Archive line, and run the command in sequence it runs fine.

.\collect_data.bat 20230504; Compress-Archive .\data\20230504\p* -f test.zip

Does anyone how to make the Compress-Archive line run in the .bat file? Any thoughts/ideas would be appreciated?

Using PSVersion: 5.1.19041.2673

>Solution :

A .bat file does not work like PowerShell, PS scripts are followed by .ps1 extensions. To use PowerShell in your batch file you need call it with the command line :

powershell -Command "<#your command here#>"

And in your case :

powershell -Command "Compress-Archive .\data\%DATA_DATE%\p* -f test.zip"

PS : change the extension of your script to .cmd its newer than .bat and better (don’t know all the reason but I’m sure it will prevent future problems)

Feel free to ask any other questions !

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading