Home > OS >  Looping Batch File Extraction
Looping Batch File Extraction

Time:08-22

I am not very keen with batch file nor with cmd command lines but I'm at the edge where I need to use them. For instance, I'm going to need to write something as follows (but in javascript):

for(var i=0, i>100,i  ){
    console.log("Extract this ("   i   ").extension");
    console.log("Exit if error");
}

...or something like that

here's the actual thing I made. (Please pardon for being noob on this)

@echo off
set x=0

:loop
set /a x=x 1
powershell -command "Expand-Archive -Force '%~dp0zip (" and %x% and ").zip' '%~dp0\extracted'"
if %errorlevel% neq 0 exit /b %errorlevel%
goto loop

My aim is to extract these files:

zip (1).zip
zip (2).zip
zip (3).zip
zip (4).zip
zip (5).zip
zip (6).zip
...

into /extracted using a batch file.

Please help me out, I'm begging you. Haha~

CodePudding user response:

Eventually, @Stephan and @Mofi with their contribution, corrected my implementation into this:

@echo off
set x=1

:loop
set /a x=x 1
powershell -command "Expand-Archive -Force '%~dp0zip (%x%).zip' '%~dp0\extracted'"
if %errorlevel% neq 0 exit /b %errorlevel%
goto loop

and it worked like magic. If there's a better and shorter way to achieve the end goal, I'd gladly check it out and try.

CodePudding user response:

If there's a better and shorter way to achieve the end goal, I'd gladly check it out and try.

I'm going to keep talking about powershell.

This link

https://social.technet.microsoft.com/Forums/en-US/ee897671-fa3a-4e06-8cb6-0986900c032e/powershell-function-unzip-all-files-in-a-folder?forum=winserverpowershell

gives this example, which in my opinion is less gibberish than batch.

Get-ChildItem 'path to folder' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force

Just like batch, you paste this line into a .ps1 file. You can use the old school powershell ISE editor or Visual Studio Code

I've done many years of batch in the past and I was glad to see the end of it.

The only thing this doesn't do it increment the i variable but that isn't really used in your code (unless you specifically want to force a stop after 100 files)

  • Related