How to change timestamps of files from .json fields

Off-topic posts of interest to the "Everything" community.
Post Reply
anmac1789
Posts: 680
Joined: Mon Aug 24, 2020 1:16 pm

How to change timestamps of files from .json fields

Post by anmac1789 »

Hi guys, need urgent help, I want to use a linux command line such as this:

touch -m -d -@"1609103679" "IMG-20201227-WA0006.jpg"

where -@"1609103679" is the unix epoch timestamp
"IMG-20201227-WA0006.jpg" is the filename that I want to change the date modified for

The problem is that the accurate timestamps are in a .json file. Sample json entry looks like:

Code: Select all

{
"files":[
{"FileName":"IMG-20201227-WA0008.jpg","FilePath":"/mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20201227-WA0008.jpg","Length":179522,"Taken":1609116233000,"GrpType":0,"DateModified":1609116233000,"Id":1199,"type2":"MEDIA"},
{"FileName":"IMG-20201227-WA0009.jpg","FilePath":"/mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20201227-WA0009.jpg","Length":135679,"Taken":1609118293000,"GrpType":0,"DateModified":1609118293000,"Id":1275,"type2":"MEDIA"}
]
}
i want to use use touch -m -d -@"1609103679" "IMG-20201227-WA0006.jpg" in such a way that it auto-populates the timestamp and filename placeholder fields from the json for the respective path: /mnt/sdcard/WhatsApp/Media/WhatsApp Images/ such that


touch -m -d -@"1609116233" "IMG-20201227-WA0008.jpg" timestamp truncated to 10-digit unix epoch timestamp in milliseconds
touch -m -d -@"1609118293" "IMG-20201227-WA0009.jpg"

Can I do this with excel ? wsl2 or linux ? on windows ? There are over 580+ files that need to change the timestamps like this. Can someone please help ?
Last edited by NotNull on Wed Jul 03, 2024 8:13 pm, edited 1 time in total.
Reason: Added coe tags ( the </> button )
anmac1789
Posts: 680
Joined: Mon Aug 24, 2020 1:16 pm

Re: How to change timestamps of files from .json fields

Post by anmac1789 »

void wrote: Wed Jul 03, 2024 2:13 am echo $null >> filename
this just creates a new file, but I want to stdout into the touch command
therube
Posts: 5056
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to change timestamps of files from .json fields

Post by therube »

pseudo-code

for i in *.jpg
do
grep %i jsonfile.json | sed -e s/.*"DateModified":// -e s/,"Id".*// > timestamp

- find the particular jpg in the .json file & parse it's data, removing all except for the time, writing the time to a file named "timestamp"


then, heh, it's a matter of passing that timestamp - from within the file to touch
you can do something like

(& i'm not thinking too clearly now, so the rest is just kind of a ramble)

cat timestamp | clip
- that will place the timestamp into Windows clipboard

Everything can then use the clipboard: contents (i.e., the timestamp)

can you write that to an (Windows) Environment Variable
if so, you can then use that variable as the -d parameter to touch

so if you
> set timestamp=1609103679
you can then
> touch -d -@%timestamp% %i
NotNull
Posts: 5517
Joined: Wed May 24, 2017 9:22 pm

Re: How to change timestamps of files from .json fields

Post by NotNull »

anmac1789 wrote: Wed Jul 03, 2024 1:28 am Hi guys, need urgent help,
Is this stiil an emergency?
anmac1789
Posts: 680
Joined: Mon Aug 24, 2020 1:16 pm

Re: How to change timestamps of files from .json fields

Post by anmac1789 »

uhh no, but I found a solution using excel...im surprised linux was more complicated and windows even more complicated than linux XD
NotNull
Posts: 5517
Joined: Wed May 24, 2017 9:22 pm

Re: How to change timestamps of files from .json fields

Post by NotNull »

JSON and XML files are hierarchically structured and handling them as simple text files can get chaotic real fast.
You need a tool to parse them correctly, Internet says "jq" is the tool to use (no personal experience).

On Windows, PowerShell can handle JSON file structures, using ConvertFrom-Json:

Code: Select all

PS C:\Temp\deleteme> cat .\filenames.json
{
"files":[
{"FileName":"IMG-20201227-WA0008.jpg","FilePath":"/mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20201227-WA0008.jpg","Length":179522,"Taken":1609116233000,"GrpType":0,"DateModified":1609116233000,"Id":1199,"type2":"MEDIA"},
{"FileName":"IMG-20201227-WA0009.jpg","FilePath":"/mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20201227-WA0009.jpg","Length":135679,"Taken":1609118293000,"GrpType":0,"DateModified":1609118293000,"Id":1275,"type2":"MEDIA"}
]
}


PS C:\Temp\deleteme> ( ConvertFrom-Json (gc .\filenames.json -raw) ).Files | % { "touch -m -d -@`"$($_.Taken)`" `"$($_.FileName)`""}
touch -m -d -@"1609116233000" "IMG-20201227-WA0008.jpg"
touch -m -d -@"1609118293000" "IMG-20201227-WA0009.jpg"
PS C:\Temp\deleteme>



Your JSON file has a quite simple structure, so it is possible to handle it as plain text using AWK (short for awkward ;) ) or SED (search/replace based on regular expressions). But as said, it gets chaotic quickly ...

Untested example with SED:

Code: Select all

grep {\"FileName\": ./filenames.json | sed -E 's|\{\"FileName\":(.+)\,\"FilePath.*Taken\":(.+)\,\"GrpType\":.*$|touch -m -d -@\2 \1|'

anmac1789
Posts: 680
Joined: Mon Aug 24, 2020 1:16 pm

Re: How to change timestamps of files from .json fields

Post by anmac1789 »

That's really complicated but I can send you the original .json file that was created by samsung smart switch somehow. This forum doesn't allow sending .json files :( but so what What I did was I imported the .json file with excel and then changed it to making a table. Afterwards, I did a custom excel format using quotes " " and &'s to appear as a touch command format and then subsititute char(34) for double quotes and then simply copy and paste all those commands as touch into powershell and then it ran each one. But rather awkwardly, pasting those touch commands makes it enter a new line after the end of the last command so powershell runs those commands twice. But still, I changed all those date modified dates within a few minutes rather than going one by one. I think this is a satisfactory solution even though it took a few more steps
therube
Posts: 5056
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to change timestamps of files from .json fields

Post by therube »

This forum doesn't allow sending .json files
.json is simply a (structured) text file.
So rename it from filename.json to filename.json.txt & you should be good.
anmac1789
Posts: 680
Joined: Mon Aug 24, 2020 1:16 pm

Re: How to change timestamps of files from .json fields

Post by anmac1789 »

therube wrote: Thu Jul 04, 2024 2:50 pm
This forum doesn't allow sending .json files
.json is simply a (structured) text file.
So rename it from filename.json to filename.json.txt & you should be good.
ahh my mistake, let me try again
NotNull
Posts: 5517
Joined: Wed May 24, 2017 9:22 pm

Re: How to change timestamps of files from .json fields

Post by NotNull »

You already changed the dates on your files. Why post your json file?
therube
Posts: 5056
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to change timestamps of files from .json fields

Post by therube »

(To maybe make the next 500 easier to deal with ;-).)
anmac1789
Posts: 680
Joined: Mon Aug 24, 2020 1:16 pm

Re: How to change timestamps of files from .json fields

Post by anmac1789 »

I changed the dates using excel although it was a painstakingly long process but it ended up being done through ubuntu wsl2 and powershell on windows
Post Reply