Crontab Creating Files with Dynamic File Names - file

How can I set the crontab to create, say for example, text files with dynamic file names? What I mean is, for instance, I will set the crontab that a text file will be created every minute in a directory. Then the filename that will be assigned to a text file will be depending on what time of the day it is.
Illustration:
0001.txt
0002.txt
0003.txt
0004.txt
...
and so on and so forth.
(Though the example above does not show text file names of the time of the day, you will get the point)

The date command can be used to get the current date and time in the format you want for the files.
filename="`date +%Y-%m-%d_%H%M.txt`"
echo "File created" > filename
Will, right now for me, create a file named "2011-11-12_0905.txt".

Related

Batch Name Change Based on Current File Name

I have a bunch of files named with the following structure.
BIOSTD_2022-05-11_08-07-29_LastName_FirstInitial_70061643.pdf
The First set of numbers if obviously the date and the second set is the time the file was created. I'd like to batch change the names to the following file structure
LastName_FirstInitial.pdf
and then move it to a new folder with the date in the original file name. There will be multiple files with the same date.
Can anyone help me out?
Thanks
Greg
I really don't know where to start.

How can I find the newest files in a specific folder and all of it's subfolders in a cmd?

I want to keep my database updated, so every time I get the latest files from TFS I want to find the changed files (newest in date) in order to run them and not run ALL of the files.
I want to create a batch file to search, find and copy them to a temp folder so I'll be able to run only them.
A creative solution would be you can enter dir results into a text file and then whichever language you are using just do string slicing which can give u date, memory and name of the file. This is command- dir/a>file replace file with the text file. if u want the text to be entered into the file in append mode then dir/a>>file

Getting a file name into a variable using a batch file

I've got a batch file which needs to perform operations on a specific file each day. So far, the file names have followed the pattern EX_2017-08-30.DAT which means I could use the following to get the exact filename for the day:
set today=%date:~-4,4%-%date:~-10,2%-%date:~-7,2%
set filename=EZ_%today%.DAT
Now I'm being told the filenames will change to include a timestamp, such as EX_2017-08-30-231859.DAT. However, the exact time won't be known beforehand (it gets set when a certain process completes).
I can't use a wildcard throughout the batch file because the filename is being written to an external file for another application to use, so I have to know the exact filename. Is there anyway that I can do a search with a wildcard and store the resulting complete filename into a variable?
If you can list the files in the directory your EX_* file is in, you can do:
for %%i in (EX_%today%-*.DAT) do (
set filename=%%i
)
The first line lists all files in the directory matching the date and the extension, and then it sets the last file to the filename variable. Be careful as this does not throw any warnings should there be more than one file matching the expression.
If you cannot list the directory, your only chance is bruteforce. There are only 24*60*60 possibilities of the filename, and if you go backwards in time, you should reach the desired file in just a couple of thousands of iterations, providing the task is usually completed close to midnight.

Cannot load files from a concatenated path

I am working on a script that will run daily. The script will compare individual configuration files from multiple host on one day with individual configuration files from a previous day. I am working on a CentOS host I have limited access to - meaning I can't make major changes.
Details
My hosts are running a cron job that uploads their configuration file to an sftp server in a generic stable directory (/var/log/Backups) by hostname (hostname.txt).
A cron job on the server creates a date stamped directory and moves the files from /var/log/Backups to /var/log/Backups/ddmmyyyy.
Later, well after all sftp and file move operations I want to load an array with the file names in a current directory and I load an array with matching file names from the previous days directory.
I want a script to diff the matching file names and output the information to a single text file.
I can't get the array to load the current days files and echo them to the terminal. I get a file operation error.
Script:
#!/bin/bash
# Set current date
now=`date +%d-%m-%Y`
echo $now
base=/var/log/GmonBackups/
loc=$base$now
echo $loc
# Load files from /var/log/GmonBackups/$now into an array
t_files=`ls loc`
echo $t_files
Something along these lines might help you get further:
today=$(date +%d%m%Y)
yesterday=$(date --date=yesterday +%d%m%Y)
base=/var/log/GmonBackups
today_dir=$base/$today
yesterday_dir=$base/$yesterday
today_files=( $today_dir/* )
yesterday_files=( $yesterday_dir/* )
A few points:
prefer $() to ``
don't use ls to get your list of files because it's not robust
I didn't put quotes around the variables because there are no spaces in your directory names.

Naming file in batch script to test.yyyy.mm.dd.hh.mm.ss

I am trying to format the date and time in using batch scripting so that my file corresponds to current date time.
if current date time is:2015.09.02.13:21:22
I would like the file to be called: TEST.2015.09.02.13:21:22
Is this doable?
I got it:
echo TEST.%date:~-4%.%date:~4,2%.%date:~7,2%.%time:~0,2%.%time:~3,2%.%time:~6,2%
and it should give:
TEST.2015.02.09.14.00.35

Resources