How to extract ONLY the contents of the JDK installer - installation

I just downloaded the Java SDK/JDK versions 5 and 6, and I just need the development tools (and some libraries) contained in the installation packages, I don't need to perform an installation and that's why I was only looking for a zip package at first (for Windows there is only an exe installation file), I only need to extract the contents of the installation packages, I think this can be done from the command line but so far I haven't found how to do this (I already considered WinRar and 7-Zip, but I really want to find how to do it without using these tools)
Have you done this before and how?

Here's .bat script for unpacking "pack" files. Must be run in the root of unzipped JDK.
#echo off
echo **********************
echo unpack JDK pack-files
echo **********************
pause
set JAVA_HOME=c:\glassfish4\jdk7
setlocal enableextensions
for /r %%f in (*) do call :process %%f
endlocal
goto :eof
:process
if NOT "%~x1" == ".pack" goto :eof
set FOLDER=%~p1
set PWD=%CD%
pushd %FOLDER%
echo Unpacking %~nx1
%JAVA_HOME%\bin\unpack200.exe %~nx1 %~n1.jar
popd
goto :eof

I use 7-zip to do that. It seems to handle that installer/self-extracting executables nicely.

I've created cygwin script to do that:
https://gist.github.com/4ndrew/f9dca61cedf0e8340b54
#!/bin/sh
# usage example: prepareJdk.sh jdk-7u67-windows-x64.exe (result will be in jdk/)
# Requires: p7zip, unzip
JDK_EXE=$1
7z x -ojdk "$JDK_EXE"
unzip jdk/tools.zip -d jdk/
find jdk/ -type f \( -name "*.exe" -o -name "*.dll" \) -exec chmod u+rwx {} \;
rm jdk/tools.zip
find jdk/ -type f -name "*.pack" | while read eachFile; do
echo "Unpacking $eachFile ...";
./jdk/bin/unpack200.exe $eachFile ${eachFile%.pack}.jar;
rm $eachFile;
done

You can do the installation once and then zip up the installed stuff placed under \Programs\Java.
This can be unzipped elsewhere later and used as a JDK in most IDE's without needing a full reinstall (but then Windows does not know about it)

You can extract both JDK 1.5 and 1.6 from .exe files, using the Universal Extractor (really a great tool). But don't forget to convert all *.pack files (compressed with Pack200 format) into corresponding *.jar files, in order to obtain a full working environment. You can use the unpack200.exe command provided in the JDK itself.

Maybe you can try Universal Extractor. The site looks legit, but I haven't tried it myself.

This is e-egiazarov's script, modified to use unpack200.exe from the JDK archive and also to remove the pack file after conversion.
#echo off
setlocal enableextensions
for /r %%f in (*) do call :process %%f
endlocal
goto :eof
:process
if NOT "%~x1" == ".pack" goto :eof
set FOLDER=%~p1
set PWD=%CD%
pushd %FOLDER%
echo Unpacking %~nx1
%PWD%\bin\unpack200.exe %~nx1 %~n1.jar
del %~nx1
popd
goto :eof

a little late to the party
here's an extractor written in powershell
param($installdir)
$list=$(gci -Recurse ./$installdir/*.pack) | %{
return #{
source=$_.FullName
parent=$_.Directory.FullName
target="$($_.Directory.FullName)\$([io.path]::GetFileNameWithoutExtension($_.Name)).jar"
}
} | %{
$result = $(unpack200 $_.source $_.target)
$_result=$result
return $_
}
Write-Host $(ConvertTo-Json $list)

Being in the folder where it was unzipped by 7zip, the same one that has the bin and lib folders, the command below does the "magic" in Windows, of course. Thanks.
for /f %f in ('dir /s/b *.pack') do bin\unpack200.exe %~pf%~nxf %~pf%~nf.jar

Related

bat script to unzip .gz file in a location and extract to another location

I need to know how to write bat script to unzip file in a location and save the extracted file to a different location, also I need to delete the original file after the extraction is done.
For example suppose my .gz file are in location C:\Users\jibin\Desktop\CDR\ and I need to extract all the files in this location to C:\Users\jibin\Desktop\CDR\CDR_Extract(destination).
This can easily be achieved using 7-zip, combined with forfiles, something like (not tested, don't kill me if it doesn't work immediately):
forfiles /M *.gz /C "cmd /c 7z x #path -oC:\Users\jibin\Desktop\CDR\CDR_Extract *.* -r"
Where:
x means "extract"
-o means "output directory"
*.* means "filename inside can be anything, extract every file"
-r means "recursive subdirectories"
Deleting the zipfile afterwards is obvious.
Without need to install third party software - you can use gzipjs.bat:
md "C:\Users\jibin\Desktop\CDR\CDR_Extract\" >nul 2>&1
for %%# in ("C:\Users\jibin\Desktop\CDR\*.gz") do (
call gzipjs.bat -u "%%~f#" "C:\Users\jibin\Desktop\CDR\CDR_Extract\%%~n#"
rem del "%%~f#" /q /f
)
Check if this is what you need and delete the rem word in the last file in order to activate deletion of the source files as you requested.
If you are using windows 10 with all the latest updates (from build 17063 and above) you can use TAR.
If you're using windows-10, version 1803+, (December 2017 to April 2018 - minimum 10.0.17063), then you may be better advised to use the built-in tar executable:
#If Exist "C:\Users\jibin\Desktop\CDR\*.gz" (
If Not Exist "%__AppDir__%tar.exe" Exit /B 1
PushD "C:\Users\jibin\Desktop\CDR"
If Not Exist "CDR_Extract\" MD "CDR_Extract" 2>NUL || Exit /B 1
For %%G In (*.gz) Do #("%__AppDir__%tar.exe" -xf "%%G" -C "CDR_Extract"
If Not ErrorLevel 1 Del "%%G")
PopD)

Get FCIV to scan hidden files within a batch script

I am attempting to hash all or most of the files off of a machine using a batch script. What I thought would be straight forward was of course not as FCIV will not scan hidden files. I attempted to make a for loop that would scan the individual files themselves but what works in the command line does not work in the batch file.
I would go to the root of my drive and attempt this:
FCIV -r -both c:\
However I noticed that quite a few files were missing (even as admin) with most of them being hidden files.
Thanks,
Any help would be appreciated.
I use this snippet of the code for CertUtil, it might also work for FCIV
cd /d %targetDir%
for /r %%e in (*) do (
if exist "\\?\%%e" (
FOR /F "tokens=* USEBACKQ" %%F IN (`certutil -hashfile "\\?\%%e" %hashType% ^| findstr /v "certutil hash"`) DO (SET var=%%F)
echo !var! "%%e" >> %hashDatabaseOutput%
SET /A fileCount += 1
) else (
echo ERROR 1 - Failed to Hash file, File or Directory contains special characters >> %errorlog%
echo %%e >> %errorlog%
echo.>> %errorlog%
)
)
cd /d "%workingDir%"
I have a similar issue as you, I'm scanning some files in C:\ but those aren't hidden files.
EDIT: Seems that using this method makes no difference because even when the file path that is feed to FCIV, the hashing will fail because it cannot find the file. you can look at my question here https://stackoverflow.com/questions/62111957/fciv-fails-to-find-some-files-when-hashing-c-drive
EDIT2: Found the root cause, it's caused by redirection when you attempting to scan files in C:\Windows\System32. One guy explained it clearly here
The system cannot find the path specified
Since FCIV only have 32bit mode, the only solution is to use other hashing program like rHash with 64bit.

cmd: run exe from a folder with dynamic name

I have an exe file, say, C:\Programs\tools\4.0.97869\program.exe
This, obviously, version number may vary, but I'm totally sure that it will always be 4.0.something
I can execute some command from batch file, specifying path to that exe like this:
"C:\Programs\tools\4.0.97869\program.exe" /option:Key somevalue
Which works perfectly fine. However, I would like to place a wildcard here, for example like this:
C:\Programs\tools\4.0.*\program.exe
Since I can perfectly navigate like this using cd
I do not want to specify that exe in Path
I do not want to cd to that directory and call program.exe from there
It there a way to specify first matching directory which has a
necessary file to execute in one line?
Thanks.
Here is a solution that uses a PowerShell script:
$pathPattern = 'C:\Programs\tools\4.0.*\program.exe'
if(!(Test-Path $pathPattern)){
throw "Could not find a single executable"
}
$paths = Get-Item -Path $pathPattern
Invoke-Expression $paths[0]
A PowerShell solution would be a better idea. If only cmd.exe can be used, the following might work. It is not a one-liner. Store this in a .bat file and CALL it. It works by running the first "program.exe" it can find. It tries to get the most recent one by ordering the directory search as most recent first.
SETLOCAL ENABLEDELAYEDEXPANSION
SET EXITCODE=0
SET "EXEWILD=C:\Programs\tools\4.0.*"
FOR /F %%d IN ("%EXEWILD%") DO (SET "EXEBASE=%%~dpd")
IF NOT EXIST "%EXEWILD%" (
ECHO ERROR: Tool directory "%EXEWILD%" does not exist.
SET EXITCODE=4
GOTO TheEnd
)
FOR /F "usebackq tokens=*" %%d IN (`DIR /B /O-D "%EXEWILD%"`) DO (
IF EXIST "%EXEBASE%\%%~d\program.exe" (
"%EXEBASE%%%~d\program.exe" %*
SET EXITCODE=!ERRORLEVEL!
GOTO TheEnd
) ELSE (
ECHO WARNING: program.exe not found in "%EXEBASE%\%%~d"
)
)
:TheEnd
EXIT /B %EXITCODE%

How do you loop inside sub directories in windows, and repeat command using windows batch scripting

I am on Windows 10. I have a folder with sub folder. All subfolders have "Subs" folder inside them. I want to loop through all subdirectories, goto Subs directory, unrar a file, Goto next subdirectory and repeat.
I tried below script but could not get to work.
#echo off
setlocal enableextensions enabledelayedexpansion
set "rootFolder=C:\Users\MNM\MAT"
echo ----------------------------------------------------------------------
for /d /r "%rootFolder%" %%a in (.) do (
set mypath=%cd%
#echo %mypath%
cd %%a
set mypath=%cd%
#echo %mypath%
cd Subs
set mypath=%cd%
#echo %mypath%
C:\Users\MNM\MAT\unrar e *subs.rar C:\Users\MNM\mat2\
cd C:\Users\MNM\MAT
)
This simple task can be done with just a single command line:
#echo off
for /R "%USERPROFILE%\MAT" %%I in ("*subs.rar") do "%USERPROFILE%\MAT\UnRAR.exe" x -c- -idcdp -y "%%I" "%USERPROFILE%\mat2\"
USERPROFILE is a predefined Windows Environment Variable which is on your computer for your user account defined with value C:\Users\MNM.
The command FOR searches in directory C:\Users\MNM\MAT and all its non hidden subdirectories because of /R for non hidden files matching the pattern *subs.rar. Each file name found is assigned with full path to loop variable I.
UnRAR is executed for each found RAR archive file for extracting the archive to directory C:\Users\MNM\mat2 with extracting also the directory structures inside the RAR archive file because of command x instead of e. Existing files in destination directory (tree) are automatically overwritten because of -y. The switches -c- and -idcdp are for displaying less information during extraction process.
For a brief description of used and additionally available switches of UnRAR run in command prompt window UnRAR without any parameter or with /? as parameter. A complete description of the commands and switches of UnRAR can be found in text file Rar.txt in program files folder of WinRAR if that shareware application is also installed and not just the freeware UnRAR.
It is absolutely not needed to change into the directory containing the RAR archive file on extracting all RAR archives into same destination directory as it can be seen here.
This is one possible way if I understood your folder structure correctly:
#echo off
set "Base=C:\Users\MNM\MAT"
echo ----------------------------------------------------------------------
for /F "delims=" %%A in (
'dir /B/S "%Base%\*subs.rar" ^| findstr /i "^%Base:\=\\%\\[^\\]*\\Subs\\[^\\]*subs.rar$"'
) do Echo "C:\Users\MNM\MAT\unrar.exe" e "%%~fA" "C:\Users\MNM\mat2\"
the for /f will parse the output of the dir and findstr
dir will iterate all *subs.rar in the tree starting from %Base%
the complex RegEx in findstr will filter the rar's to those in a folder subs in a subfolder of %Base%
as a backslash is an escape char in a RegEx, literal backslashes have to be doubled.
If the output looks ok remove the echo in the last line.
Just because recursing all subdirectories and extracting all *subs.rar files wasn't requested here's an example that is based upon my assumptions:
#ECHO OFF
SET "rootDir=%USERPROFILE%\MAT"
IF /I NOT "%CD%"=="%rootDir%" CD /D "%rootDir%"
FOR /D %%A IN (*
) DO IF EXIST "%%A\Subs\*subs.rar" UNRAR e "%%A\Subs\*subs.rar" mat2\

Get output of batch script into the correct subdir with /R flag

I'm currently facing the problem that I need process files in every sub-directory, I'm using a for loop like this one:
for /R %%x in (*.dat) do tool.exe -c -f -e "%%x"
Now it does process everything but the tool outputs a file and that file always lands in the directory where the batch script got launched in and not in the directory where the original file was located.
I tried a quite a few approaches like using %CD% as the output directory option for the tool but it looks like the tool didn't like that very much and just gave errors.
I'm looking for a general solution as I had this problem with quite a few CLI tools now.
I'm late on this but, complementing Andriy answer, you can streamline the BAT by combining two FOR loops, which might be slightly more efficient, and more clear.
for /D /R %%d in (*) do (
pushd %%d
for %%x in (*.dat) do tool.exe -c -f -e "%%x"
popd
)
As an added bonus, this version will solve the case when the main loop refers to a different drive (which is not the case on this particular OP question) and has the relative side value of restoring the current directory on each iteration, which might also be of slight importance in case other commands are incorporated in the loop.
You could CD to the directory where the current .dat lies before calling tool.exe:
for /R %%x in (*.dat) do (cd "%%~dpx" & tool.exe -c -f -e "%%x")
You can use forfiles ...
forfiles /s /C /m *.dat "cmd /c tools.exe #file"
For more information see http://ss64.com/nt/forfiles.html

Resources