I managed having a JSON file created by ffprobe which contains basic info about a video stream in a MKV container. By jq-win64.exe "[.format.duration]" %%~ni.mkv.json the duration of the movie is read correctly from the file and jq echos ["1:36:55.184000"]. Now I want to store this value in a global variable of my script for further processing. I tried several approaches but each of them led to errors and/or left %duration% empty. I tried e.g.
for %%i in (*.mkv) do (
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=* USEBACKQ" %%F IN ('_tools\jq\jq-win64.exe "[.format.duration]" %%~ni.mkv.json') DO (SET duration=%%F)
echo Duration is: %duration%
ENDLOCAL
)
but could not manage to echo %duration%. I think it can't be that hard, most likely I don't do the syntax right on Windows batch. Any ideas? Here is the JSON file as well:
{
"format": {
"filename": "TestFile_1080p_26Mbs_8bit_BT709.mkv",
"nb_streams": 1,
"nb_programs": 0,
"format_name": "matroska,webm",
"format_long_name": "Matroska / WebM",
"start_time": "0:00:00.000000",
"duration": "1:36:55.184000",
"size": "17.586597 Gibyte",
"bit_rate": "25.978148 Mbit/s",
"probe_score": 100,
"tags": {
"title": "TestFile",
"encoder": "libmakemkv v1.14.4 (1.3.5/1.4.7) win(x64-release)",
"creation_time": "2019-08-17T21:01:18.000000Z"
}
}
}
Here's a batch-file solution based upon my understanding after the comments:
For /F Tokens^=2Delims^=^" %%F In (
'_tools\jq\jq-win64.exe "[.format.duration]" "%%~ni.mkv.json" 2^>NUL')Do (
Set "duration=%%F"
SetLocal EnableDelayedExpansion
Echo( !duration!
EndLocal
)
If all you want is the duration, then there's no need for intermediate JSONs, because FFprobe can also tell you that:
ffprobe.exe -v 0 -i <input> -show_entries format=duration -of compact=p=0:nk=1
1:36:55.184000
Create a variable:
FOR /F "delims=" %%A IN (
'ffprobe.exe -v 0 -i <input> -show_entries format=duration -of compact=p=0:nk=1'
) DO SET duration=%%A
SET duration=1:36:55.184000
If you still want to parse FFprobe's JSON, then there's no need to create json-files either, as you can simply pipe it to JQ instead:
ffprobe.exe -v 0 -i <input> -show_format -of json | jq.exe -r .format.duration
1:36:55.184000
Create a variable:
FOR /F "delims=" %%A IN (
'ffprobe.exe -v 0 -i <input> -show_format -of json ^| jq.exe -r .format.duration'
) DO SET duration=%%A
SET duration=1:36:55.184000
Related
Ok so i am writing a batch file in which i delete every line containing "," after finding string:
"plugins": {
Is it possible to make this condition in for loop ?
now i know you can avoid quotes using ^ but i just cant make it work.
what i do right now is the following:
#echo OFF
::removePlugins
setlocal enabledelayedexpansion
echo. 2>tempPackage.json
SET #FOUND=0
for /F "delims=" %%G in (./package.json) do (
echo %%G|find "," > nul
if not errorlevel 1 (SET #FOUND=1)
if !#FOUND!==1(
#echo ON
SET #FOUND=0
ECHO: >> tempPackage.json
#echo OFF
)
ECHO %%G>>tempPackage.json
)
move "./tempPackage.json" ".package.json"
So in this case i would only make a new line in every line that contains ",".
So how does one write a for loop that only goes from this string forward and not make new line but delete it?.
the expected result after runing the batch would be :
{
"scripts"{ still the same scripts},
"dependencies"{ still the same dependencies},
"cordova": {
"platforms": [],
"plugins": {}
}
}
}
I tryed to use the code from #Stephan , like this:
#echo OFF
::removePlugins
setlocal enabledelayedexpansion
SET #findPlugins=0
find /i /c "plugins" ./package.json >NUL
if not errorlevel 1 (SET #findPlugins=1)
IF !#findPlugins!==1 (
SET "#FOUND=1"
>tempPackage.json (
for /F "delims=" %%G in (./package.json) do (
echo %%G|findstr /b "}" >nul && SET "#FOUND=1"
if defined #FOUND ECHO %%G
echo %%G|findstr /b "\"plugins\": {" >nul && SET "#FOUND="
)
)
type tempPackage.json
)
And it just rewrote the json to temp and didnt delete anything...what am i doing wrong ?
Keep in mind, batch can't interpret .json files and handles them as pure text. So any batch solution will highly depend on the exact format of the file. Any change in the format (a stray space might be enough) may cause trash.
That said: use a flag that changes at (each) line that starts with "plugins": and changes back when hitting the line starting with } (end of the block) and write the line dependent on the flag:
#echo OFF
::removePlugins
setlocal
SET "#FLAG=1"
>tempPackage.json (
for /F "delims=" %%G in (./package.json) do (
echo %%G|findstr /e "[^{]}" >nul && SET "#FLAG=1"
if defined #FLAG ECHO %%G
echo %%G|findstr "\"plugins\":" >nul && SET "#FLAG="
)
)
type tempPackage.json
This uses a regex to capture everything up to the "plugins" line.
powershell -NoLogo -NoProfile -Command ^
Get-Content -Raw .\pi.txt ^| ^
ForEach-Object { if ($_ -match '(?sm)(.*\"plugins\": {).*') { $Matches[1] + \"`n`n}\" ^| Out-File -FilePath '.package.json'}}
I have text file abc.txt with the contents as :
abc.txt :
{"nature":"calm","trees":"uprooted from the main area","name":"usdbuebcowecy821nkwh29y2bnso3ns389ye3wnsiwsn9usj","enrolled":"not yet"}
I need to extract the string "usdbuebcowecy821nkwh29y2bnso3ns389ye3wnsiwsn9usj" associated with name from the abc.txt. The strings associated with name vary and are not static. Hence whatever the string is asociated with name has to be extracted and updated in a sample.json file .
Sample.json :
{
"requisite":{
"name": "usdbuebcowecy821nkwh29y2bnso3ns389ye3wnsiwsn9usj"
},
"land": {
"key": "890"
}
}
Sample.json file name key should be updated with the appropriate name extracted from abc.txt name field.
I tried below code snippet to extract the name field abc.txt file :
For /f "tokens=1 delims=:" %%j in ('dir /b /s "C:\abc.txt" ^|findstr /I ""name":"') do echo "%%j"
echo name is: %%j
However the loop doesnt search for the name string and Im stuck to proceed further. Im new to batch script. Can anyone help me out?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q64985945.txt"
:: Read sourcefile to LINE
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO SET "line=%%a"
:: Change each { } and : to comma
SET "line=%line:{=,%"
SET "line=%line:}=,%"
SET "line=%line::=,%"
:: ensure NAME is not defined
SET "name="
:: process LINE
:: set NAME when the string `name` is detected, use that flag to set NAME to the value following.
:: Note that LINE will not contain {:}, so any of these values can be used as a flag to detect
:: `name` as the last value.
FOR %%a IN (%line%) DO IF DEFINED name (SET "name=%%~a"&GOTO done ) ELSE IF /i "%%~a"=="name" SET "name=:"
:noname
ECHO No name value found
GOTO :eof
:done
SET name
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances. The listing uses a setting that suits my system.
I used a file named q64985945.txt containing your data for my testing.
The usebackq option is only required because I chose to add quotes around the source filename.
Unfortunately, you still keep the JSON text a er, mystery. The sample you've posted doesn't tell us a number of things - whether the first occurrence of name is the one you require, or whether there are other conditions that determine which particular value of name is to be selected. For instance, your original sample did not include nested brace-pairs. All significant in devising a solution...
The dir command show file names. You want not to process the file name, but the file contents, isn't it? So you should give "abc.txt" as parameter of findstr command.
In the line you want the sixth token separated by colon or comma, right?
This works:
For /f "tokens=6 delims=:," %%j in ('findstr /I "name" abc.txt') do echo %%j
However, if the contents of file abc.txt is just one line (as you said in the question), then you don't even need the findstr command...
For /f "tokens=6 delims=:," %%j in (abc.txt) do echo %%j
This is another way to do it that extracts the values of all variables in the line:
#echo off
setlocal
rem Read a line from abc.txt
set /P "line=" < abc.txt
rem For example: {"nature":"calm","trees":"uprooted","name":"usd","enrolled":"not yet"}
rem Remove braces -> "nature":"calm","trees":"uprooted","name":"usd","enrolled":"not yet"
set "line=%line:~1,-1%"
rem Change ":" by = -> "nature=calm","trees=uprooted","name=usd","enrolled=not yet"
set "line=%line:":"==%"
rem Change "," by " & set " -> "nature=calm" & set "trees=uprooted" & set "name=usd" & set "enrolled=not yet"
rem and *execute* such a line inserting a SET command at beginning:
set %line:","=" & set "%
rem Now all variables have their values. For example:
echo {"name":"%name%"}
New solution added
Your first request was to extract name field from abc.txt file. However, you have now changed the problem to update a line of sample.json file that in the original question have just one line.
Anyway, here it is a solution to your new problem:
#echo off
setlocal EnableDelayedExpansion
rem Get the value of "name": field in abc.txt file
for /f "tokens=6 delims=:," %%j in (abc.txt) do set "name=%%~j"
rem Get line number of "name": line minus one in sample.json file
for /F "delims=:" %%n in ('findstr /N "\"name\":" sample.json') do set /A "lines=%%n-1"
rem Process sample.json file and create sample.out
< sample.json (
rem Copy first N lines
for /L %%i in (1,1,%lines%) do set /P "line=" & echo !line!
rem Read and update the "name": line as requested
set /P "line="
for /F "delims=:" %%a in ("!line!") do echo %%a: "%name%"
rem Copy the rest of lines
findstr "^"
) > sample.out
move /Y sample.out sample.json
Note that this code is prone to get errors because Batch files are not designed to process json files. If the program fails with your real data because a detail that is different from the posted data, please do not post here a request to fix the code! :(
batch-file/cmd has no support for JSON at all, so please use a tool like xidel that does.
dot notation:
xidel -s sample.json -e "($json).requisite.name:=json-doc('abc.txt').name"
XQuery:
xidel -s sample.json -e "$json/map:put(.,'requisite',{'name':json-doc('abc.txt')/name})"
Output (to stdout) in both cases:
{
"requisite": {
"name": "usdbuebcowecy821nkwh29y2bnso3ns389ye3wnsiwsn9usj"
},
"land": {
"key": "890"
}
}
To update the input file simply use --in-place:
xidel -s --in-place sample.json -e "[...]"
I have dozens of json files, and I am trying to find two values in each of them and assign the results to two separate variables for ffmpeg processing.
An example json file looks like this:
{
"year": "2018",
"track": "12",
... other data omitted
}
I wish to extract 2018 and 12 so that I can use them in the following ffmpeg command:
ffmpeg -i "same_file_name_as_json.m4a" -metadata:s:a:0 year=2018 --metadata:s:a:0 track=12 -acodec libmp3lame "same_file_name_as_json.mp3"
Is it possible to write a single batch file to achieve the desired result? Any help would be greatly appreciated as I am a complete novice at findstr and setting variables. Thank you.
EDITED:
set "year=" & set "track="
for %%i in (*.json) do (
for /f "usebackq tokens=1,2 delims={:}, " %%a in ("%%i") do (
set "%%~a=%%~b"
if defined year if defined track goto :CONT
)
:CONT
C:\ffmpeg -i "%%~ni.m4a" -metadata:s:a:0 year=%year% -metadata:s:a:0 track=%track% -acodec libmp3lame "%%~ni.mp3"
)
pause
Windows batch scripting does not understand the JSON file format, so it is better to use a language natively supports it. It is not the best idea to treat JSON as "normal" text, because only a slight change (for instance, added, deleted, or moved line-breaks) that do not violate the JSON format can still make big troubles then.
That said, given that the JSON file exactly appears as you have shown it and it features Unix- or DOS/Windows-style line-breaks (that is, a carriage-return character followed by a line-feed character), this code could work for you:
for /F "usebackq tokens=1,2 delims={:}, " %%M in ("file.json") do set "%%~M=%%~N"
echo year = %year%
echo track = %track%
If you have got a huge JSON file you do not want to unnecessarily fully process, you could use this code instead:
set "year=" & set "track="
for /F "usebackq tokens=1,2 delims={:}, " %%M in ("file.json") do (
set "%%~M=%%~N"
if defined year if defined track goto :CONT
)
:CONT
echo year = %year%
echo track = %track%
If the (non-array) values you want to extract may also contain one of the defined delimiters ({, :, }, ,, SPACE), you could extend the code to this, given that the values do not contain the characters *, ?, <, >:
set "year=" & set "track="
for /F "usebackq tokens=1,* delims={:}, " %%M in ("file.json") do (
for %%K in (%%N) do set "%%~M=%%~K"
if defined year if defined track goto :CONT
)
:CONT
echo year = %year%
echo track = %track%
To prevent the script from assigning unwanted superfluous variables, you may try this:
for /F "usebackq tokens=1,2 delims={:}, " %%M in ("file.json") do (
if "%%~M"=="year" (set "%%~M=%%~N") else if "%%~M"=="track" set "%%~M=%%~N"
)
echo year = %year%
echo track = %track%
Or this, which prepreocesses the data by the findstr command and filters out the desired lines:
for /F "tokens=1,2 delims={:}, " %%M in ('
findstr /R /C:"^ *\"year\" *:" /C:"^ *\"track\" *:" "file.json"
') do set "%%~M=%%~N"
echo year = %year%
echo track = %track%
Based on your edit, let me suggest to use the last of the above methods, because there is no goto :CONT, which cannot be used within loops as it breaks the block context, and it does not assign additional unwanted variables. Since variables are written and read within the loop body, you have to enable and apply delayed variable expansion. I would do all that the following way:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem /* Iterate over the `*.json` files in the current working directory (`%CD%`);
rem to use the parent directory of this script, use `%~dp0*.json` instead: */
for %%I in ("*.json") do (
rem // Store name of current JSON file in variable:
set "name=%%~nI"
rem // Clear variables for later check for availability:
set "year=" & set "track="
rem // Process the current JSON file:
for /F "tokens=1,2 delims={:}, " %%M in ('
findstr /R /C:"^ *\"year\" *:" /C:"^ *\"track\" *:" "%%~I"
') do (
rem // Assign year and track variables:
set "%%~M=%%~N"
rem // Check of both year and track are available:
if defined year if defined track (
rem // Toggle delayed expansion to avoid troubles with `!`:
setlocal EnableDelayedExpansion
rem // Eventually execute `ffmpeg` tool using all the derived data:
ffmpeg -i "!name!.m4a" -metadata:s:a:0 year=!year! -metadata:s:a:0 track=!track! -acodec libmp3lame "!name!.mp3"
endlocal
)
)
)
endlocal
exit /B
I have dozens of json files...
Windows' cmd doesn't support JSON, so you'd have to resort to PowerShell, or use an external tool that does. You might find xidel interesting.
To extract the value for "year" and "track":
xidel -s input.json -e "$json/(year,track)"
#or
xidel -s input.json -e "$json/year,$json/track"
2018
12
To export to a variable %year% and %track%:
FOR /F "delims=" %A IN ('xidel -s input.json -e "$json/(year:=year,track:=track)" --output-format^=cmd') DO %A
#or
FOR /F "delims=" %A IN ('xidel -s input.json -e "year:=$json/year,track:=$json/track" --output-format^=cmd') DO %A
You don't however need variables to create the strings (ffmpeg commands) you want. xidel can do that too.
You could use a FOR-loop to iterate over all your JSON-files...
FOR %A IN (*.json) DO #xidel -s %A -e "$json/concat('ffmpeg -i \"%~nA.m4a\" -metadata:s:a:0 year=',year,' --metadata:s:a:0 track=',track,' -acodec libmp3lame \"%~nA.mp3\"')"
ffmpeg -i "name-of-json-file.m4a" -metadata:s:a:0 year=2018 --metadata:s:a:0 track=12 -acodec libmp3lame "name-of-json-file.mp3"
...but to call xidel for each and every JSON-file is very inefficient. xidel can do this much more efficiently.
xidel's equivalent for FOR %A IN (*.json) DO #ECHO %A is xidel -se "file:list(.,false(),'*.json')"
Then you can use the following query to process all your JSON-files at once:
xidel -se "for $x in file:list(.,false(),'*.json') return json-doc($x)/concat('ffmpeg -i \"',replace($x,'json','m4a'),'\" -metadata:s:a:0 year=',year,' --metadata:s:a:0 track=',track,' -acodec libmp3lame \"',replace($x,'json','mp3'),'\"')"
Prettified command/query:
xidel -se ^"^
for $x in file:list(.,false(),'*.json') return^
json-doc($x)/concat(^
'ffmpeg -i \^"',^
replace($x,'json','m4a'),^
'\^" -metadata:s:a:0 year=',^
year,^
' --metadata:s:a:0 track=',^
track,^
' -acodec libmp3lame \^"',^
replace($x,'json','mp3'),^
'\^"'^
)^
"
I have a JSON file, named "Config.json", that looks like this:
{ "RunEnvironment": "DEV"}
In a batch file under the same directory, I want to read the value of the "RunEnvironment" element.
My batch script would look like:
if [jsonElement] == 'DEV' (
:: do something
)
Can anyone show me how to do this?
In PowerShell for example, you could do:
> If((Get-Content '.\Config.json'|ConvertFrom-Json).RunEnvironment -eq 'DEV'){"is DEV:Whatever"}
is DEV:Whatever
To be on topic on cmd line
> for /f "tokens=1,2 delims=:{} " %A in (Config.json) do #If "%~B"=="Dev" #Echo (%~A = %~B)
(RunEnvironment=DEV)
In a batch file
#Echo off
for /f "tokens=1,2 delims=:{} " %%A in (Config.json) do (
If "%%~B"=="Dev" Echo (%%~A=%%~B^)
)
The Echo (%%~A=%%~B^) could be replaced with whatever you plan to do.
If all you need to do is perform a specific command only if the value of RunEnvironment is DEV then this should be all you require, (from a batch-file or the command-line):
"%__APPDIR__%FindStr.exe" /IRC:"\"RunEnvironment\":\ \ *\"DEV\"" "Config.json">NUL 2>&1&&Echo Your command here
You'd simply replace Echo Your command here with your intended command.
Yo can do in this way
#echo off
set string={ "RunEnvironment": "DEV" }
for /f "tokens=3,5" %%a in ('echo %string%') do set d=%%~a
if "%d%" == "DEV" echo %d%
pause & goto :EOF
In a batch file:
#ECHO off
SET "FilenameForJsonFile=Config.json"
SET "FilenameForRunEnvironment=RunEnvironment.txt"
powershell -Command "Select-String -Pattern 'RunEnvironment\"\: \".*?\"' .\%FilenameForJsonFile% ^| ForEach-Object { $_.Matches.Value.substring(18,$_.Matches.Value.Length-19) }>%FilenameForRunEnvironment%
FOR /f "delims=" %%x IN (%FilenameForRunEnvironment%) DO SET JsonElement=%%x
IF %JsonElement%==DEV (ECHO development environment) ELSE (ECHO abc123 environment)
Hi I'm very new in batch files. I try to do something simular to my bash script. So here is my problem:
I want to get all versions/tags from a github repository to generate a configuration file for the php documentor sami. But how can I write the JSON into a variable in batch to get the versions? In my bash script I did this and it's working fine:
function jsonDecode() {
json=$1
key=$2
echo ${json} | jq -r ${key}
}
ghUser="MisterMarlu"
ghRepo="sentence"
json=$(curl "https://api.github.com/repos/${ghUser}/${ghRepo}/tags")
versions=$(echo "${json}" | jq -c ".[]")
for version in ${versions[#]}; do
versionNumber=$(jsonDecode ${version} ".name")
echo " ->add( '${versionNumber}', '${versionNumber}' )" >> ${config}
done
# Here comes alot of code below this for loop..
This will output "v0.0.1" and "v0.0.2". Bus how can I do this in a batch file?
EDIT
Here is the JSON response where I need just the "name" as array:
[
{
"name": "v0.0.2",
"zipball_url": "https://api.github.com/repos/MisterMarlu/sentence/zipball/v0.0.2",
"tarball_url": "https://api.github.com/repos/MisterMarlu/sentence/tarball/v0.0.2",
"commit": {
"sha": "82c4b6d74cc16816104934114766f0328e77ee66",
"url": "https://api.github.com/repos/MisterMarlu/sentence/commits/82c4b6d74cc16816104934114766f0328e77ee66"
},
"node_id": "MDM6UmVmMTMzMDM1MDMxOnYwLjAuMg=="
},
{
"name": "v0.0.1",
"zipball_url": "https://api.github.com/repos/MisterMarlu/sentence/zipball/v0.0.1",
"tarball_url": "https://api.github.com/repos/MisterMarlu/sentence/tarball/v0.0.1",
"commit": {
"sha": "0cf1a83a51716da3f42915c9eab571166845bb0b",
"url": "https://api.github.com/repos/MisterMarlu/sentence/commits/0cf1a83a51716da3f42915c9eab571166845bb0b"
},
"node_id": "MDM6UmVmMTMzMDM1MDMxOnYwLjAuMQ=="
}
]
To process the output of another program you need a for /f
parsing the lines filtered by findstr
:: Q:\Test\2018\06\12\SU_50811698.cmd
#Echo off & SetLocal EnableDelayedExpansion
Set "ghUser=MisterMarlu"
Set "ghRepo=sentence"
Set "Version="
For /f "tokens=1,2 delims=:, " %%U in ('
curl "https://api.github.com/repos/%ghUser%/%ghRepo%/tags" 2^>Nul ^| findstr /i "\"name\""
') do Set "Version=!Version!,%%~V"
If defined Version (set "Version=%Version:~1%") Else (Set "Version=n/a")
Set Version
Sample output:
> Q:\Test\2018\06\12\SU_50811698.cmd
Version=v0.0.2,v0.0.1
You are aware that batch has no real arrays?
Just an alternative in PowerShell:
$ghUser="MisterMarlu"
$ghRepo="sentence"
$URL = "https://api.github.com/repos/$ghUser/$ghRepo/tags"
$Json=(curl.exe $URL)|ConvertFrom-json
$Json | Select name
name
----
v0.0.2
v0.0.1
With Xidel it's simple as:
xidel -s "https://api.github.com/repos/MisterMarlu/sentence/tags" -e "join($json()/name,',')"
This puts out: v0.0.2,v0.0.1.
To export this as $config/%config%...
Bash:
eval "$(xidel -s "https://api.github.com/repos/MisterMarlu/sentence/tags" -e '
config:=join(
$json()/name,
","
)' --output-format=bash
)"
Batch:
FOR /F "delims=" %%A IN ('xidel.exe -s "https://api.github.com/repos/MisterMarlu/sentence/tags" -e ^"
config:^=join^(
$json^(^)/name^,
'^,'
^)^" --output-format^=cmd
') DO %%A
or...
FOR /F "delims=" %%A IN ('xidel.exe -s "https://api.github.com/repos/MisterMarlu/sentence/tags" -e "config:=join($json()/name,',')" --output-format=cmd') DO %%A