40 000 files search and delete

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
metallon
Posts: 2
Joined: 07 May 2018 03:46

40 000 files search and delete

#1 Post by metallon » 07 May 2018 03:50

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: 40 000 files search and delete

#2 Post by aGerman » 07 May 2018 11:33

metallon wrote:
07 May 2018 03:50
a string like "forwardaction=0" or "forwardaction=0.0" and a file name to an image like "imagefilename=myfile.jpeg"
Very unusual for JSON. Things like "forwardaction": 0 would look more like JSON.
metallon wrote:
07 May 2018 03:50
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
That could be probably done using Batch.
metallon wrote:
07 May 2018 03:50
as well as the image file named after "imagefilename="
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

metallon
Posts: 2
Joined: 07 May 2018 03:46

Re: 40 000 files search and delete

#3 Post by metallon » 09 May 2018 02:50

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}

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: 40 000 files search and delete

#4 Post by penpen » 16 May 2018 03:55

This might help you:

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
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

Post Reply