Hi Dos Guru's,
I am running Windows 10 and need to search through about 40 000 .json files that contain a string like "forwardaction=0" or "forwardaction=0.0" and a file name to an image like "imagefilename=myfile.jpeg". I need to go through all of those .json files and whenever the string "forwardaction=0" or "forwardaction=0.0" can be found, delete the .json itself as well as the image file named after "imagefilename=".
Is that possible and if so, how would I do it?
40 000 files search and delete
Moderator: DosItHelp
Re: 40 000 files search and delete
Very unusual for JSON. Things like "forwardaction": 0 would look more like JSON.
That could be probably done using Batch.
That's tricky and the way how to extract the file name does highly depend on the structure of the JSON text (that we don't even know). It might be more reliable to parse the JSON object using a language that supports it. But as I said, without knowing the whole structure it's not possible to give any good advice.
Steffen
Re: 40 000 files search and delete
Hi aGerman,
Many thanks for your reply.
The .JSON file looks like that:
{"user/angle": 0.03399153759605006, "user/mode": "user", "cam/image_array": "1204_cam-image_array_.jpg", "forwardaction": 0.0}
Many thanks for your reply.
The .JSON file looks like that:
{"user/angle": 0.03399153759605006, "user/mode": "user", "cam/image_array": "1204_cam-image_array_.jpg", "forwardaction": 0.0}
Re: 40 000 files search and delete
This might help you:
Note, that this actually doesn't delete any files, but echoes the delete statement (so you can check if the result is right without doing any harm).
If you want it to delete the files, then just remove the "echo(" in front of the "del"-statement.
penpen
Code: Select all
@echo off
setlocal enableExtensions enableDelayedExpansion
:: assumed that the json files are encoded in utf-8, and you don't need the console to switch back to your original set codepage, and
:: assumed that the json files don't contain any exclamation mark ('!') or percentage sign ('%') characters, and also
:: assumed that no line has more than 1023 characters in it,
:: the following might help you.
>nul chcp 65001
cls
for %%a in (*.json.txt) do (
set "file="
set "line="
<%%a set /p "line="
set ^"line=!line:"=!"
set "line=!line: =!"
set "line=!line:{=!"
set "line=!line:}=!,"
for %%b in (!line!) do (
if "forwardaction:0.0" == "%%~b" set "file=!line:*cam/image_array:=!"
if "forwardaction:0" == "%%~b" set "file=!line:*cam/image_array:=!"
)
if defined file (
for %%b in ("!file:*,=!") do set "file=!file:,%%~b=!"
echo(delete "%%~a" "!file!"
)
)
goto :eof
If you want it to delete the files, then just remove the "echo(" in front of the "del"-statement.
penpen