0

So I tried modifying the command I found here: to work with my date format: 2015-12-31 22.07.29 and using setFile -d instead of touch.

Here's my script

for f in *; do
t="${t%%.*}"
t=“${t%%-*}”
m=“${t:5:2}”
d=“${t:8:2}”
y=“${t:0:4}”
h=“${t:11:2}”
m=“${t:14:2}”
s=“${t:17:2}”
date=“$m/$d/$y $h$m$s”
setFile -d $date "$f"
done

I try running the command in the folder with jpegs that have the wrong creation date but i get this error over and over

-bash: “”“”??: command not found
ERROR: invalid date/time

I'm a total noob when it comes to using terminal so I have no idea what's wrong with the formatting and what is causing the other error. Does anyone here know what I'm doing wrong?

1
  • As mentioned in the answer by Denis Rasulev, you have some bad quotes instead of ", but that is not your only problem with the code! The first line after the for in do, t="${t%%.*}" is meaningless a the t variable has yet to be initialized with anything. Base on the linked answer it should be an f inside of the {...} not a t. That said though, we can not test further without some sample file names as well as the expected output. Commented May 6, 2019 at 19:16

2 Answers 2

2

To me it looks like the problem is with the quotes. Try to copy paste this into your script:

for f in *; do
t="${t%%.*}"
t="${t%%-*}"
m="${t:5:2}"
d="${t:8:2}"
y="${t:0:4}"
h="${t:11:2}"
m="${t:14:2}"
s="${t:17:2}"
date="$m/$d/$y $h$m$s"
setFile -d $date "$f"
done
0

Here's your script cleaned up and written as a kornshell script and then as a bash shell script. In both cases we use the shell's extended glob feature to match appropriate filenames. I'm assuming that filenames begin with 4 digits then a hyphen, have two more digits then another hyphen and then two digits followed by any other number of characters- ending with the suffix .jpg.

#! /bin/ksh

for f in {4}([0-9])-{2}([0-9])-{2}([0-9])*.jpg
do
        y=${f:0:4}
        m=${f:5:2}
        d=${f:8:2}
        H=${f:11:2}
        M=${f:14:2}
        S=${f:17:2}

        cr_date="$m/$d/$y $H:$M:$S"

        SetFile -d "$cr_date" "$f"
done


#! /bin/bash

shopt -s extglob

for f in +([0-9])-+([0-9])-+([0-9])*.jpg
do
        y=${f:0:4}
        m=${f:5:2}
        d=${f:8:2}
        H=${f:11:2}
        M=${f:14:2}
        S=${f:17:2}

        cr_date="$m/$d/$y $H:$M:$S"

        SetFile -d "$cr_date" "$f"
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .