0

I am trying to use SetFile to change MacOS file creation and modification date (see here).

My problem is somewhat similar to this one but even by following the advice there I can't get it to work and get an error 1 in return (bad syntax)

Here is the code:

file_t = obj.decided_stamp.strftime('%m/%d/%Y %H:%M:%S')

# Use SetFile to change creation and modification date
cmd = ['SetFile', '-d', '-m', '{0}{1}{0}'.format('\'', file_t),
       '{0}{1}{0}'.format('\'', obj.path)]
try:
    check_output(cmd, shell=True)
except Exception as e:
    logger.warning('Error {} on {} using {}'.format(e, obj.path, cmd))

If I take away the shell=True I get error 2 (other error), using shell gives be the error 1.

Here is an example of warning print I get:

2018-06-04 21:06:06 main[84988] WARNING Error Command '['SetFile', '-d', '-m', "'01/13/2017 14:55:55'", "'/Volumes/Images/2017-01-12 Conference/2017-01-13 14.55.55.jpg'"]' returned non-zero exit status 2. on /Volumes/Images/2017-01-12 Conference/2017-01-1314.55.55.jpg using ['SetFile', '-d', '-m', "'01/13/2017 14:55:55'", "'/Volumes/Images/2017-01-12 Conference/2017-01-13 14.55.55.jpg'"]

Any idea what I am doing wrong there ?

Thanks

Community
  • 1
  • 1
Kodsama
  • 131
  • 1
  • 12

1 Answers1

0

Ok, it seems that check_output required adding the quotes before the call. Also a date must be provided for each argument (-d and -m)

This solved the problem:

import shlex

file_t = obj.decided_stamp.strftime('\'%m/%d/%Y %H:%M:%S\'')

# Use SetFile to change creation and modification date
op = shlex.quote(obj.path)
cmd = ' '.join(['SetFile', '-d', file_t, '-m', file_t, op])
try:
    check_output(cmd, shell=True)
except Exception as e:
    logger.warning('Error {} on {} using {}'.format(e, obj.path, cmd))
Kodsama
  • 131
  • 1
  • 12