0

Unfortunately my shell skills are very bad and I would need some help in running a simple script on my QNAP to fix some date problem on some videos.

The script I put in place is very easy:

  1. in a given folder
  2. check if there are .mp4 files starting with VID_
  3. if so, for each of them run a given exiftool command

Here is the script so far, but I guess I am not using the right way to call the variable:

#!/bin/sh

# set target directories
dir="/share/Multimedia/Pictures/"

# move to target directory
cd "$dir"

# check if there is some .mp4 file starting with "VID_" in the folder

VID=$(ls -A $dir | grep 'VID_' | grep './mp4')

if
    ["$VID"];

then

    # for each file in the list
    for f in $VID

        do

        # change all date metadata according to its filename
        exiftool "-*date<filename" -wm w $f

done

else

fi

Thanks for your help!

ps: the exiftool instruction is correct (except probably for the variable)

giopas
  • 645
  • 1
  • 6
  • 10
  • What do the desired filenames look like? Show some real examples? Also put this code into http://www.shecllcheck.net to catch some syntax/etc. issues. – Etan Reisner Oct 21 '15 at 16:45
  • Also read [Parsing ls(1)](http://mywiki.wooledge.org/ParsingLs) and [Don't read lines with `for`](http://mywiki.wooledge.org/DontReadLinesWithFor). – Etan Reisner Oct 21 '15 at 16:46

2 Answers2

0

Your code is probably failing due to use of:

grep './mp4'

Since there is no / before mp4.

Better to have your script as:

#!/bin/sh

# set target directories
dir="/share/Multimedia/Pictures/"

# move to target directory
cd "$dir"

for f in VID_*.mp4; do
   exiftool "-*date<filename" -wm w "$f"
done

No need to parse output of ls here and there is no need to use grep since glob VID_*.mp4 will do the job of finding correct files.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

There isn't a need to script this, doing so just slows you down as you have to call ExifTool for every file. ExifTool can do all the files in one pass:
ExifTool -ext mp4 '-*date<filename' -wm w /path/to/dir/VID_*

The -ext mp4 options limits the command to only mp4 files. Since you seem to be on a linux/mac system, the double quotes had to be changed to single quotes. Double quotes are needed on Windows systems, single quotes on linux/mac systems.

StarGeek
  • 4,948
  • 2
  • 19
  • 30