WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Windows NT Batch Script to Install Core Linux ISO to USB Logical Drive  (Read 5469 times)

Offline CirothUngol

  • Newbie
  • *
  • Posts: 1
Thanks for Core Linux! Love the project and the system runs great on every desktop I've tried.
The install process was simple from Windows (following this guide), simple enough for me to cobble together this short batch script to automate the process. Copy this script to a text file and rename it as "tc-install.cmd".
Uses VBScript to elevate permissions for syslinux.exe and PowerScript to mount the first ISO it finds (but works by pre-mounting or inserting the CD as well). Tested on Win10, but it should work on any version back to WinXP.
Code: [Select]
:: tc-install.cmd v0.1 - Core Linux Install for Windows NT
:: Batch script installs to Logical Drive by following this guide:
:: http://wiki.tinycorelinux.net/wiki:install_usb_stepbystep
::
:: Copy this .cmd file to a folder containing both:
::   syslinux.exe for Windows, or it's available somewhere in PATH.
::   Core Linux current.iso, or it's already mounted/inserted.
:: Insert a USB drive and execute batch file after windows finds it.

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

:: set/clear variables
FOR %%A IN (lf ans last skip allDrv srcDrv srcIso tarDrv) DO SET "%%A="
SET "EKO=<NUL SET/P="
(SET \n=^
%=Do not remove this line=%
)

:: Use VBScript to request elevated permissions
>NUL fsutil dirty query %SYSTEMDRIVE% 2>&1 || (
MODE CON cols=80 lines=3
COLOR CF
TITLE Please wait...
ECHO.
ECHO                         Requesting elevated shell...
ECHO Set UAC = CreateObject^("Shell.Application"^)>"%TEMP%\elevate.vbs"
ECHO UAC.ShellExecute "%~f0", "%TEMP%\elevate.vbs", "", "runas", 1 >>"%TEMP%\elevate.vbs"
IF EXIST "%TEMP%\elevate.vbs" START /B /WAIT >NUL cscript /nologo "%TEMP%\elevate.vbs" 2>&1
IF EXIST "%TEMP%\elevate.vbs" >NUL DEL /F "%TEMP%\elevate.vbs" 2>&1
EXIT /B
)

:: check for needed files and mount ISO with PowerShell if present
ECHO %~nx0 v0.1 - Core Linux Install for Windows
CD /D "%~dp0" >NUL 2>&1
syslinux.exe >NUL 2>&1
IF %ERRORLEVEL% EQU 9009 ECHO syslinux.exe could not be found.& GOTO :stop
syslinux --version
PowerShell.exe /? >NUL 2>&1
IF %ERRORLEVEL% NEQ 9009 FOR %%A IN (*Core*.iso) DO IF "!srcIso!"=="" (
SET "srcIso=%%~fA"
%EKO%PowerShell mounting !srcIso!...
PowerShell.exe -Command "& {Dismount-DiskImage -ImagePath "%%~fA"}" >NUL 2>&1
PowerShell.exe -Command "& {Mount-DiskImage -ImagePath "%%~fA"}" >NUL 2>&1
IF ERRORLEVEL 1 (ECHO  failed.) ELSE (ECHO  done.)
ECHO.
)

:: discover and display all logical drives, find Core Linux files
ECHO Logical drives found (-^> indicates Core Linux source).
FOR %%@ IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO %%@: 2>NUL && (
SET "volume%%@="
SET "allDrv=!allDrv!%%@ "
FOR /F "tokens=1-5*" %%A IN ('DIR /AD %%@: 2^>^&1') DO (
IF "!volume%%@!"=="" SET "volume%%@=%%F"
SET "line=%%A %%B %%C %%D %%E %%F"
SET "freeBytes%%@=%%C")
SET "tv%%@=!volume%%@!                  "
SET "tv%%@=!tv%%@:~0,18!"
SET "outLine=   %%@:!tv%%@! !freeBytes%%@! bytes free"
FOR /F "tokens=* delims=0123456789-,. " %%A IN ("!freeBytes%%@!") DO IF "%%A" NEQ "" (
SET "outLine=   %%@:No Media Inserted  !line!")
IF EXIST %%@:\boot\core.gz ( SET "srcDrv=!scrDrv!%%@ "
IF !freeBytes%%@! EQU 0 ( SET "outLine=-> %%@:!tv%%@! Core Linux CD"
) ELSE SET "outLine=-> %%@:!tv%%@! !freeBytes%%@! bytes free")
ECHO !outLine!
)
CD /D "%~dp0" >NUL 2>&1
ECHO.

:: user input and exit conditions
IF "%allDrv%"=="" ECHO No logical drives found.& GOTO :stop
IF "%srcDrv%"=="" ECHO No Core Linux install source found.& GOTO :stop
SET /P "tarDrv=Install Core Linux on to which drive? %allDrv: =,% ENTER to exit: "
IF "%tarDrv%"=="" ECHO Exiting script.& GOTO :stop
SET "tarDrv=%tarDrv:~0,1%"
IF NOT EXIST %tarDrv%: ECHO %tarDrv%: does not exist.& GOTO :stop
IF !freeBytes%tarDrv%! EQU 0 ECHO %tarDrv%: is either full or unwritable.& GOTO :stop

:: format logical drive
SET /P "ans=Format %tarDrv%:!volume%tarDrv%! as FAT32? Type desired volume name or ENTER to skip: "
IF DEFINED ans (
ECHO Formatting will destroy all data on %tarDrv%:!volume%tarDrv%!
%EKO%If certain, press ENTER to continue...
FORMAT %tarDrv%: /FS:FAT32 /V:%ans% /Q >NUL
IF ERRORLEVEL 1 GOTO :stop
SET "volume%tarDrv%=!ans!"
)

:: choose source and copy files
SET "srcDrv=!srcDrv:%tarDrv% =!"
IF "%srcDrv%" NEQ "" ( SET "ans="
SET /P "ans=Source drive for Core Linux files? %srcDrv: =,% ENTER to skip: "
IF DEFINED ans (SET "ans=!ans:~0,1!"
IF NOT EXIST !ans!: ECHO !ans!: does not exist.& GOTO :stop
IF EXIST !ans!:\boot XCOPY !ans!:\boot %tarDrv%:\boot /E /I /Q /H /R /Y
IF EXIST !ans!:\tce XCOPY !ans!:\tce %tarDrv%:\tce /E /I /Q /H /R /Y
IF EXIST !ans!:\cde IF NOT EXIST %tarDrv%:\tce XCOPY !ans!:\cde %tarDrv%:\cde /E /I /Q /H /R /Y
)
)

:: install syslinux and adjust copied Core.iso files
%EKO%Installing syslinux on %tarDrv%:!volume%tarDrv%!...
syslinux.exe -mafi %tarDrv%: >NUL 2>&1
IF ERRORLEVEL 1 (ECHO  failed.) ELSE (ECHO  done.)
IF NOT EXIST %tarDrv%:\boot\core.gz ECHO %tarDrv%:\boot\core.gz not found.& GOTO :stop
%EKO%Adjusting Core.iso files...
RENAME %tarDrv%:\cde tce>NUL 2>&1
RENAME %tarDrv%:\boot\isolinux syslinux>NUL 2>&1
RENAME %tarDrv%:\boot\syslinux\isolinux.bin syslinux.bin>NUL 2>&1
IF NOT EXIST %tarDrv%:\boot\syslinux\isolinux.cfg GOTO :stop
CD.>%tarDrv%:\boot\syslinux\syslinux.cfg
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "%tarDrv%:\boot\syslinux\isolinux.cfg"') DO (
SET "line=%%A"
SET "line=!line: cde waitusb= waitusb!"
SET "line=!line: cde= waitusb=5!"
SET "line=!line:*:=!"
IF DEFINED line (
IF DEFINED skip ( SET "skip="
IF DEFINED last %EKO%!last!!\n!!\n!
) ELSE IF DEFINED last %EKO%!last!!\n!
SET "last=!line!"
) ELSE SET "skip=1"
)>>%tarDrv%:\boot\syslinux\syslinux.cfg
(IF DEFINED skip ( SET "skip="
IF DEFINED last %EKO%!last!!\n!!\n!
) ELSE IF DEFINED last %EKO%!last!!\n!
)>>%tarDrv%:\boot\syslinux\syslinux.cfg
DEL /Q /F /A %tarDrv%:\boot\syslinux\isolinux.cfg >NUL 2>&1
ECHO  done.

:stop
IF DEFINED srcIso PowerShell.exe -Command "& {Dismount-DiskImage -ImagePath "!srcIso!"}" >NUL 2>&1
IF DEFINED tarDrv ATTRIB -H -S -R "%tarDrv%:\System Volume Information" /S /D >NUL 2>&1
IF DEFINED tarDrv RD /S /Q "%tarDrv%:\System Volume Information" >NUL 2>&1
ENDLOCAL
timeout.exe >NUL 2>&1
IF %ERRORLEVEL% EQU 9009 (PAUSE) ELSE (timeout.exe /T 30)
EXIT /B 0

Hopefully this helps others to more easily install Core Linux using a Windows platform.

Offline NewUser

  • Full Member
  • ***
  • Posts: 169
Re: Windows NT Batch Script to Install Core Linux ISO to USB Logical Drive
« Reply #1 on: September 10, 2019, 03:30:12 AM »
As another lover of the Windows "shell", that's good work. I have always used core2usb for that purpose, but I see where this can be handy. Thanks!