Windows Batch file execute X files at a time - windows

I have this code in my batch file:
#echo off
set real_parent_path=%1%
for %%F in (%real_parent_path%*.*) do call set files=%%files%% %%F
START cmd.exe /k "cd S:\Production\CrushFTP7_PC\modules\photomatixpro5 & PhotomatixCL -z -Z AA8E67A0 -x "S:\Production\CrushFTP7_PC\modules\photomatixpro5\BuiltinPresets\LCP360_settings.xmp" -mp 8 -a2 -am 12 -tr -r 11 -n 0 -ns 0 "_HDR" -bi 8 -h remove -s jpg -d %real_parent_path%HDR\\%files%"
%files% contains a list of all the files in the working folder, how would I send 3 files at a time to the START command?

Given your feedback in the comments - I might do something like the following
#echo off
setLocal enableDelayedExpansion
set real_parent_path=%1%
set count=0
set "files="
for %%F in (%real_parent_path%*.*) do (
set /a count+=1
set files=!files! %%F
if !count!==3 (
START cmd.exe /k "cd S:\Production\CrushFTP7_PC\modules\photomatixpro5 & PhotomatixCL -z -Z AA8E67A0 -x "S:\Production\CrushFTP7_PC\modules\photomatixpro5\BuiltinPresets\LCP360_settings.xmp" -mp 8 -a2 -am 12 -tr -r 11 -n 0 -ns 0 "_HDR" -bi 8 -h remove -s jpg -d %real_parent_path%HDR\\!files!"
set count=0
set "files="
)
)
Run the start command inside the for loop, every third iteration - then clear the files variable, and continue the loop.

Related

.bat curl with for complaining about a ( was unexpected at this time

I have the following code.
#echo off
SetLocal EnableDelayedExpansion & REM All variables are set local to this run & expanded at execution time rather than at parse time (tip: echo !output!)
REM Get full path to run.bat file (excluding filename)
set wrapper_dir=%~dp1dist
set arch=amd64.exe
set system=windows
mkdir %wrapper_dir%
REM get_my_version - Find the latest version available for download.
(for /f %%i in ('curl -f -s https://prysmaticlabs.com/releases/latest') do set my_version=%%i) || (echo [31mERROR: require an internet connection. ESC[0m && exit /b 1)
echo ESC[37mLatest release is %my_version%.ESC[0m
IF defined USE_MY_VERSION (
echo [33mdetected variable USE_MY_VERSION=%USE_MY_VERSION%[0m
set reason=as specified in USE_MY_VERSION
set my_version=%USE_MY_VERSION%
) else (
set reason=automatically selected latest available release
)
echo Using version %my_version%.
set PROGRAM_REAL=%wrapper_dir%\program-%my_version%-%system%-%arch%
if [%1]==[program-real] (
if exist %PROGRAM_REAL% (
echo ESC[32mBeacon chain is up to date.[0m
) else (
echo ESC[35mDownloading beacon chain %my_version% to %PROGRAM_REAL% %reason%[0m
for /f "delims=" %%i in ('curl --silent -w "%%{http_code}" -L "https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%" -o "%PROGRAM_REAL%" ') do set http=%%i
if %http%=="400" (
echo ESC[35mNo program real found for %my_version%ESC[0m
exit \b 1
)
curl --silent -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%.sha256 -o %wrapper_dir%\beacon-chain-%my_version%-%system%-%arch%.sha256
curl --silent -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%.sig -o %wrapper_dir%\beacon-chain-%my_version%-%system%-%arch%.sig
)
)
At the line of the second for loop inside the IF statement(second IF statement) it complains ( was unexpected at this time.
The way to work around it , is to "grease" the batch file by running this command (on the prompt directly)
>for /f "delims=" %i in ('curl --silent -w "%{http_code}" -L https://prysmaticlabs.com/releases/beacon-chain-%prysm_version%-%system%-%arch% -o gg.txt') do set http=%i
ONLY then does it work
>prysm1.bat program-real version
A subdirectory or file C:\Users\HP\Documents\Investments\ethbox\prysm\dist already exists.
Latest release is v1.4.2.
detected variable USE_MY_VERSION=fake
Using version fake.
Downloading beacon chain fake to C:\Users\HP\Documents\Investments\ethbox\prysm\dist\program-fake-windows-amd64.exe as specified in USE_MY_VERSION
I know it has to do with the way the variables are expanded at execution vs parse time and that maybe a bracket, _ or other special characters are causing this. But i tried a couple of things and the problem persists. (all did not work)
quote the entire for with double quotes "
use brakets around the for
not using the _ in my_version.
Any ideas?
thanks
UPDATES after comments below
The script now does not complain after #aschipfl 's suggestions. However the last if stat is not evaluating to true. Made the for echo the result to ensure value in if stat.
for /f "delims=" %%i in ('curl --silent -o nul -w "%%{http_code}" https://prysmaticlabs.com/releases/beacon-chain-%version%-%system%-%arch% ') do set "http=%%i" && echo %%i
Based on all the constructive feedback, here is the final working version the summary of which :
Replacing [] with ""( quotes) in all if statements in order to protect spaces and other special characters.
Using !! vs %% for http var since it is in a block with delayed expansion
Check if download exists (404 error) then proceed and download.
#echo off
SetLocal EnableDelayedExpansion & REM All variables are set local to this run & expanded at execution time rather than at parse time (tip: echo !output!)
.... same code as above...
if "%1"=="program-real" (
if exist "%PROGRAM_REAL%" (
echo ESC[32mBeacon chain is up to date.[0m
) else (
echo ESC[35mDownloading beacon chain %my_version% to %PROGRAM_REAL% %reason%[0m
for /f "delims=" %%i in ('curl --silent -o nul -w "%%{http_code}" -L "https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%" ') do set "http=%%i" && echo %%i
if "!http!"=="404" (
echo ESC[35mNo program real found for %my_version%ESC[0m
exit /b 1
)
curl -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch% -o %PROGRAM_REAL%
curl --silent -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%.sha256 -o %wrapper_dir%\beacon-chain-%my_version%-%system%-%arch%.sha256
curl --silent -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%.sig -o %wrapper_dir%\beacon-chain-%my_version%-%system%-%arch%.sig
)
)

Can't convert IMG to ISO

So I'm currently working on my own OS (just for fun), and it's currently being compiled into an IMG file. I know it's not needed but I would like to try and compile it into an ISO, but so far I've had no luck with it. I'm not quite sure which files are needed, so just ask for them and I will edit the post :)
When I run the img I get this:
But when I run the ISO I get this:
Here's my compile script I wrote in batch:
#echo off
echo Building KronkOS floppy image!
echo.
echo Assembling bootloader...
echo ======================================================
wsl nasm -f bin bootloader/boot.asm -o bootloader/boot.bin
echo Done!
echo.
echo Assembling KronkOS kernel...
echo ======================================================
wsl nasm -f bin kernel.asm -o KERNEL.BIN -l kernel_list.lst
echo Done!
::echo.
::echo Creating elf kernel file...
::echo ======================================================
::wsl nasm -f elf32 -o kernel.o kernel.asm
::wsl ld -m elf_i386 -o kernel.elf kernel.o
::del kernel.o
::echo Done!
::echo.
::echo Assembling programs...
::echo ======================================================
::cd programs
:: for %%i in (*.BKF) do del %%i
:: for %%i in (*.ASM) do wsl nasm -O0 -f BIN %%i
:: for %%i in (*.) do ren %%i %%i.BKF
::cd ..
::echo Done!
echo.
echo Adding bootsector to disk image...
echo ======================================================
wsl mformat -f 1440 -B bootloader/boot.bin -C -i images/KronkOS.img
echo.
echo Done!
echo.
echo Copying kernel and applications to disk image...
echo ======================================================
wsl mcopy -D o -i images/KronkOS.img KERNEL.BIN ::/
wsl mcopy -D o -i images/KronkOS.img programs/*.BKF ::/
wsl mcopy -D o -i images/KronkOS.img programs/*.BAS ::/
echo.
echo Done!
echo.
echo Do you want to build and ISO?
echo ======================================================
choice /c YN
if errorlevel 1 set x=1
if errorlevel 2 set x=2
if "%x%" == "1" (
cd images
wsl genisoimage -input-charset utf-8 -o KronkISO.iso -V KRONKOS -b KronkOS.img ./
cd..
echo.
echo Done!
)
echo.
echo ======================================================
echo Build done!
choice /c YN /m "Run KronkOS build in QEMU?"
if errorlevel 1 set y=1
if errorlevel 2 set y=2
if "%y%" == "1" (
if "%1" == "-d" (
wsl sh -c "export DISPLAY=0:0 && qemu-system-x86_64 -s -S -fda images/KronkOS.img"
) else (
if "%x%" == "1" (
wsl sh -c "export DISPLAY=0:0 && qemu-system-x86_64 images/KronkISO.iso"
) else (
wsl sh -c "export DISPLAY=0:0 && qemu-system-x86_64 -fda images/KronkOS.img"
)
)
)'
cls
So I changed the tool I used to convert it into an ISO from fat_imgen to mtools.I also followed what Michael Petch suggested in a comment which was to use the -cdrom option in qemu, and now everything seems to work as it should.

Execute python via batch based on some condition?

I have a python script which it is downloading some files, before executing the task, I want to know if directory is empty or if the number of files less than 5 then run the python otherwise just say the files already exist, how can I modify the batch code in order to apply this change.
#echo off
.....
:begin
python.exe "%ScriptFolderPath%"\extractDAILYdata.py -u %UserName% -p %Password% -s %BirstSpace% -r %ATIBaseUrl% -sp %DailyLoadPath% -f "%LogPath%" -i %LogLevel% -l %LogFile%
if %errorlevel%==0 goto bcsuccess
:bcerror
echo Task "%TaskName%" failed: %date% >> "%LogPath%\%LogFile%"
exit /B %errorlevel%
:bcsuccess
echo Task "%TaskName%" succeeded: %date% >> "%LogPath%\%LogFile%"

windows command set variable to grep output

Status: Noobie
Desired: in Windows command set a variable to grep output
I've done some looking around, and apparently the following strategy should work. But although the first grep output echoed to screen gives the correct output 20 16 21 16 16 as the file patterns.txt is looped through, the variable myvar is reported as always 16, even though I'm deleting tmpfile in each loop.
ECHO OFF
For /f %%a in (patterns.txt) do (
grep -E --count %%a Winter2015.tex
grep -E --count %%a Winter2015.tex > tmpfile
set /p myvar= < tmpfile
del tmpfile
echo %myvar%
)
What am I missing? Thanks!
You have to delay the expansion of variables by adding:
setlocal enabledelayedexpansion
And using ! around your variables instead of %:
echo !myvar!
See this.

simple xmlstarlet example to copy files

Windows Command Prompt. I want to do the following in a ONE-LINER COMMAND.
i want to set a variable with a simple xml structure:
<pathlist>
<path>C:\file.txt</path>
<path>C:\file2.txt</path>
</pathlist>
like this:
SET "_myvar=^<pathlist^>^<path^>C:\file.txt^</path^> ^<path^>C:\file2.txt^</path^>^</pathlist^>"
then i want to echo this and pipe it to xmlstarlet:
echo !_myvar!|xmlstarlet sel -t -v "//path"
then the result should be put into another var sourcefiles, with a for loop?
for /f %i in ('call echo %^_myVar%^|xmlstarlet sel -t -v "/*" ') do set sourcefiles=%i
and finally pscp sourcefile to a remote Unix
pscp -l user-pw password %sourcefiles% openstack#remoteIP:/opt/testfolder
i cannot use temporary files for this taks, i tried this:
SET "_myVar=^<pathlist^>^<path^>C:\file1.txt^</path^> ^<path^>C:\file2.txt^</path^>^</pat
hlist^>"& for /f %i in ('cmd /v:on /c echo !_myVar!|xmlstarlet sel -t -v "/*" ') do set sourcefiles=%i &&cmd /v:on /
c pscp -l user -pw password %sourcefiles% openstack#remoteIp:/opt/testfolder
and get this error:
| was unexpected at this time.
the problem is when i want to echo the var content to xmlstarlet, i think. Anybody know how to solve this?
Edit
using call echo instead of cmd /v:on in a slighty simplified command i get this error:
SET "_myVar=^<pathlist^>^<path^>C:\file1.txt^</path^> ^<path^>C:\file2.txt^</path^>^</pat
hlist^>"& 'call echo ^^%^_myVar!%^^|xmlstarlet sel -t -v "/*"
error:
-:1.1: Document is empty
^
-:1.1: Start tag expected, '<' not found
^
SET "_myVar=^<pathlist^>^<path^>C:\file1.txt^</path^> ^<path^>C:\file2.txt^</path^>^</pathlist^>"& for /f %i in ('cmd /v:on /c echo !_myVar!^|xmlstarlet sel -t -v "/*" ') do set sourcefiles=%i &&cmd /v:on /c pscp -l user -pw password %sourcefiles% openstack#remoteIp:/opt/testfolder
should fix your | was unexpected at this time. error. The only change is to escape the pipe with ^, which tells cmd that the pipe is part of the command, not an instruction to cmd.
My focus was on the error-report about the pipe.
What I found was that echo doesn't line < as the first character of the string being echoed - so I toyed around with it a while.
And then I cut the grass and cleaned up the yard a bit.
And then I thought - well, how about sed? I use GNUSED, and since you're outputting to a *Nix system, the world of SED shouldn't be too fearsome.
How about - as a batch file,
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "uname=%~1"&shift
SET "pass=%~1"&shift
SET "sourcefiles="
SET "_myVar=*pathlist?"
:dirloop
IF "%~1" neq "" SET "_myvar=%_myvar% *path?%~1*/path?"&shift&GOTO dirloop
SET "_myvar=%_myvar%*/pathlist?"
for /f %%i in ('echo %_myvar%^|sed s/\x2a/\x3c/g^;s/\x3f/\x3e/g^|xmlstarlet sel -t -v "/*" ') do ECHO %%i&set "sourcefiles=!sourcefiles! %%i"
ECHO pscp -l %uname% -pw %pass% %sourcefiles% openstack#remoteIp:/opt/testfolder
Which you could execute as a single line
thisbatch user password path1 path2 path3...
(yeah - the pscp is just being echoed for verification...)
or, for an all-in-one-line version, which I'll spread over several for clarity's sake (and because I wrote it in a "DOS" session):
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_myvar=*pathlist?*path?C:\file.txt*/path? *path?C:\file2.txt*/path?*/pathlist?"
SET "sourcefiles="
for /f %%i in ('echo %_myvar%^|sed s/\x2a/\x3c/g^;s/\x3f/\x3e/g^|xmlstarlet sel -t -v "/*" ') do set "sourcefiles=!sourcefiles! %%i"
ECHO pscp -l user -pw password !sourcefiles! openstack#remoteIp:/opt/testfolder
SInce * and ? can't be used as legitimate filename characters, including them in the string set into the environment, then using echo to send *? to sed which converts them to <> for input to xmlstarlet - now that might work (but I don't have xmlstarlet for testing - I conclude it simply outputs a list of some variety...)
Note the ^ to escape the ; in the sed command!
Here's the all-in-one-line version that may work
SETLOCAL ENABLEDELAYEDEXPANSION&SET "_myvar=*pathlist?*path?C:\file.txt*/path? *path?C:\file2.txt*/path?*/pathlist?"&SET "sourcefiles="&(for /f %%i in ('echo %_myvar%^|sed s/\x2a/\x3c/g^;s/\x3f/\x3e/g^|xmlstarlet sel -t -v "/*" ') do set "sourcefiles=!sourcefiles! %%i")&pscp -l user -pw password !sourcefiles! openstack#remoteIp:/opt/testfolder
but I've no way of testing this in your environment.
Here's the line I used:
SETLOCAL ENABLEDELAYEDEXPANSION&SET "_myvar=*pathlist?*path?C:\file.txt*/path? *path?C:\file2.txt*/path?*/pathlist?"&SET "sourcefiles="&(for /f %%i in ('echo %_myvar%^|sed s/\x2a/\x3c/g^;s/\x3f/\x3e/g^|xmlstarlet sel -t -v "/*" ') do set "sourcefiles=!sourcefiles! %%i")&pscp -l user -pw password !sourcefiles! openstack#remoteIp:/opt/testfolder
I used a batch file called xmlstarlet.bat
#ECHO OFF
SETLOCAL
ECHO phial1.txt
ECHO phial2.txt
ECHO phial3.txt
ECHO phial4.txt
ECHO phial5.txt
which should simply produce 5 lines of output when run
I used a batch file called pscp.bat
#ECHO OFF
SETLOCAL
ECHO running PSCP
ECHO %*
PAUSE
And the result was:
running PSCP
-l user -pw password phial1.txt phial2.txt phial3.txt phial4.txt phial5.txt openstack#remoteIp:/opt/testfolder
Press any key to continue . . .

Resources