1

I am creating a batch file that when a user runs it a file included in the folder is unzipped.

So like this:

Downloaded_Folder | Batch.bat | ZippedFile.zip

Though when I run the powershell command to unzip the powershell is ran from System32. I am also unsure where the user will be opening this folder from (Their downloads, or desktop, or documents?)

Here is what I have:

openfiles > NUL 2>&1 
if NOT %ERRORLEVEL% EQU 0 goto NotAdmin 
echo Hello from elevated command prompt
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar'); }"
goto End 
:NotAdmin 
echo This command prompt is NOT ELEVATED 
pause
:End

pause
Buddhism
  • 21
  • 1
  • 4
    I suggest you write the entire thing in Powershell. Both batch and Powershell can identify the current folder. – Nick.McDermaid May 25 '21 at 03:34
  • 1
    `%0` refers to the batch-file itself, therefore considering you've read `for /?` and specifically looked at variable substitution, it will mean that the path of the batch file will be `%~dp0` – Gerhard May 25 '21 at 04:16
  • @Gerhard It is better to read the help output on running `call /?` in a command prompt window as this help explains how to reference the arguments of a batch file with argument 0 being the batch file itself. `for /?` contains nearly the same list of modifiers, but this list is for the loop variable. For example, there is the difference regarding to `%*`. – Mofi May 25 '21 at 06:07
  • 1
    I recommend reading also the help output on running `if /?` in a command prompt window. The condition `if NOT %ERRORLEVEL% EQU 0 goto NotAdmin` is very unusual. The recommended syntax is `if errorlevel 1 goto NotAdmin`. See also my answers on [Can't run as Admin](https://stackoverflow.com/a/41493926/3074564) and on [Why does 'Run as administrator' change (sometimes) batch file's current directory?](https://stackoverflow.com/a/31655249/3074564) – Mofi May 25 '21 at 06:12
  • Here is a quick example that i tested on my side for downloading and Unzipping File [Download-UnZipFile.bat](https://pastebin.com/naTnnf4Z) – Hackoo May 25 '21 at 12:25

0 Answers0