Split Filename Up to Define Variables - date

I have a script I created to help with converting a video then uploading it to our website. Our videos all have a standard format for their filename to help with setting them up correctly (day, month, year; i.e. 09OCT2013.m4v). They get filed into directories from year to month to day (i.e. 2013/oct/09OCT2013/09OCT2013.m4v). Right now, my script opens by asking for user input for the year then month then the actual file name for the folder. What I want to do is take the file that has already been created, drop it into the script, then have the script take it apart and put it in the appropriate file (i.e. drop the file 12JUN2012.m4v into the script and the script automatically puts it into 2012/jun/12JUN2012/). Is there any possible way to do this in terminal? Please let me know if any part of my question is unclear.

Assuming that you're using bash:
for file in "$#"
do
dd=${file:0:2}
mm=${file:2:3}
yy=${file:5:4}
mv "$file" "$yy/$mm/$file"
done
If the file needs to be moved further, or is supplied with more pathname, you can adjust the script, but the basic idea of splitting the last component of the file name up using the substring notation is good.

Related

Batch file to move a download to new folder

I used the tampermonkey extension to download an excel file every time I visit a certain URL. I want to write a batch file that will visit that URL everyday, cause the file to download, and then move that downloaded file into a new folder.
I know how to get to the url and initiate the download with a batch script
The downloaded filename is in the format "websitename_DDMonYYY," except if the date is 1 digit the leading zero is removed.
For example, today's downloaded file would be "websitename_8Feb2023"
How can I write a script that will match the downloaded file name to today's date, so that I can then move it to a new folder?
I've been trying to use the %date:~#,#% syntax to extract the day, year, and month, but I need help removing the leading zero in the date, converting the month number into a 3 letter code, and then concatenating all of these into a single string.
Thank you!

Rename a group of .txt files based on content that appears after a specific string in the text file?

I have a folder with hundreds of text files in it. The file names have zero meaning. I am trying to extract a variable number that appears after a string in each file and and rename the files using it. I'm somewhat of a Powershell newbie (an old timer form the DOS world) but I have written many useful scripts with it. I've searched high an low, this one has me stumped.
Any and all suggestions are welcome, or I'd happily pay someone for the snippet of code. -Ed

including current date in printfile names

I am using a macro to obtain data from a website and save to a print file using the print function on the specific website page.
In the print window i can select "print to pdf" but do not know how to format the output filename to reflect the current date.
The macro will be run every month to save a snapshot of the website's data.
I have tried several suggestions from the forums but haven't found a solution that works.
Filename required is of the form "yyyymmdd_account_summary.pdf"
Using Kantu as the macro recorder in firefox 68 on fedora 29.
i tried setting the print file name to
$(date +"%y%m%d")_account_summary.pdf
but this only created a file named
$(date +"%y%m%d")_account_summary.pdf
no variables were substituted.
obviously i am doing something simple wrong but cannot see it.
Expected resulting filename
20190731_account_summary.pdf
actual name that is created
$(date +"%y%m%d")_account_summary.pdf which is clearly wrong
We are not very familiar with Kantu but I suggest we do it in three steps.
Step 1. Let your macro runs periodically to a fixed file name (/tmp/my_temp_file.pdf).
Step 2. Write a script (bash or python) to monitor the file modify/overwrite timestamp and whenever it detects changes, copy the file (/tmp/my_temp_file.pdf) into "yyyymmdd_account_summary.pdf"
Step 3. Make a cronjob that invoke the script periodically.

Batch: Create a Folder and Name it by Today's Date

How can I make a batch file that will create a folder (in a specified directory) that will create a folder and label it with today's date? Can I also make it delete folders that are one week old or older?
One question at a time. Addressing your first one:
cd \YourSpecificDirectory
SET Today=%Date:~10,4%%Date:~4,2%%Date:~7,2%
REN Creates folder in format 20050812 (depending on regional settings and language)
md %Today%
Answers to your second question (how to delete a folder before a certain date) already exist here. A search should find them for you pretty easily.
Please see this post how to get data value independent of data/Time format (zone):
batch file to copy some files and changing their name

WinSCP time based file download

I would like to write WinSCP script to download a file that is placed onto the remote server every morning between 4-4:30am. Is there a way to do this with time-stamping?
I want to pseudocode:
get file.txt where timestap<1 hour from 4 am
First, I assume your file does not have fixed name (contrary to your question with fixed name file.txt). If not, please explain, why do you need timestamp-based solution.
Anyway, you can use a file mask with a time constraint:
get "*.txt>2014-07-19 4:00"
To dynamically inject today's date, use the %TIMESTAMP% syntax:
get "*.txt>%TIMESTAMP#yyyy-mm-ss% 4:00"
Simply, the above means, get all files created later than 4:00 today (the %TIMESTAMP#yyyy-mm-ss% resolves to today's date in format yyyy-mm-ss, as needed for the time constraint).
When passing the get on WinSCP command-line in a batch file (using /command switch, as opposite to using /script switch to specify a separate script file), you have to double the % to avoid the batch-file trying to interpret the %TIMESTAMP%:
winscp.com /command ... "get ""*.txt>%%TIMESTAMP#yyyy-mm-ss%% 4:00"""
Another solution is a static script that rely on a relative time: E.g. you know your script runs at 6am. So you let WinSCP download all files updated/created in the last 2 hours (6am – 4am):
get *.txt>2h
See also WinSCP article on downloading the most recent file.