File name with yyyymmddhhmi

Hi Masters,

I have script which is having filename like this

fileName=Device_manumodel_DATETIME.csv .
in another server client will put files like Device_manumodel_yyyymmddhhmi.csv format. every 30 mins script will run and took the file from client server.

Device_manumodel_201807110900.csv
Device_manumodel_201807110830.csv
Device_manumodel_201807110800.csv

fileName=Device_manumodel_DATETIME.csv .. this is not working fine. how to set filename in Device_manumodel_yyyymmddhh24mi.csv format?

Please advise ..

BR
Prabhakar

you don't specify the scripting language, so I'm going to assume you're using bash...

fileName="Device_manumodel_$( date +%Y%m%d%H%M).csv"

If it will be in a script perhaps

datetime=`date +%Y%m%d%H%M`
date >myFile.$datetime.csv

You also need to ensure that your file creation and all the output being put into it is Atomic, i.e. a single operation that takes a single IO. Often this is done by creating the file and building it up with content as another name and then when it is ready, renaming it to it appears as a complete file instantly.

If you have a file that takes even a whole second to build, then you may sometimes be unlucky and fetch a partial file.

Something like this would illustrate it:-

((random_delay=$RANDOM%9))

( sleep $random_delay ; cat /tmp/myfile ) &      # Display the file at some undetermined time

date                             > /tmp/myfile
echo "First data line of file"  >> /tmp/myfile
sleep 3
echo "Second data line of file" >> /tmp/myfile
sleep 3
echo "Last data line of file"   >> /tmp/myfile
wait                                             # In case the random delay is long, wait for the background sleep/cat to finish

If you run this repeatedly, you may get 1, 2 or 3 lines of output. You can compensate by the cat reading a file that is always the finished article, so more like:-

((random_delay=$RANDOM%9))

( sleep $random_delay ; cat /tmp/finalfile ) &   # Display the file at some undetermined time

date                             > /tmp/myfile
echo "First data line of file"  >> /tmp/myfile
sleep 3
echo "Second data line of file" >> /tmp/myfile
sleep 3
echo "Last data line of file"   >> /tmp/myfile

mv /tmp/myfile /tmp/finalfile                    # Atomic file write
wait                                             # In case the random delay is long, wait for the background sleep/cat to finish

It may fail first time if the file does not exist, but afterwards it would only ever read a completed file.

Doing this, you could also make a link as a "latest" file, so you always know which one to pick up.

I hope that this helps,
Robin