I have directories full of files that contain dates and times in their names in the format YYYYMMDDhhmm. So like, one of the files is named 199407271609 with no file extension.
I have this script searches a given directory and changes all creation dates of the files to match the date in their name:
#! /bin/zsh
for filename in `ls $1`
do
touch -t $filename $1/$filename;
done
The script changes the creation dates correctly, but the only problem is, it's ignoring the hours and minutes part and making all the creation times 12:00 AM. What am I missing here? Any help greatly appreciated.
I'm not sure why you're getting the behavior you're seeing, but
the first thing I do when having trouble in a 'for' loop, is to
make sure the for variable has my expected result in it.
So if you do this:
for $filename in `ls $1`
do
echo $filename
done
is $filename what you expect for all files? Is it preceded
with the directory information? That might make using it
as your time parameter for the -t in 'touch' rather difficult.
Bingo! I'm an idiot, thanks for pointing that out. I was indeed looking at output that was saying 00:00 for the times. I tested this by creating a file like the one in my original example with a random date and time from like a month ago, and it shows up correctly.