How can I simplify this Autohotkey v1 script (fewer lines of code) in order to replace filenames by checking their time ranges and weekdays, along with the specified strings?
; Extract the day, month, and year from the input string using SubStr
day := SubStr(str, InStr(str, "") - 8, 2)
month := SubStr(str, InStr(str, "") - 6, 2)
year := SubStr(str, InStr(str, "_") - 4, 4)
code
date := month . "/" . day . "/" . year
;MsgBox, Converted string: %DATETOFEED%
FormatTime, Weekday , % Substr(date,7)Substr(date,1,2)Substr(date,4,2), dddd
;{ Tagging based on channel and airing time
code
Tagging:
filename6 := filename5 ; Initialize filename6 with filename5
; If a filename contains the string "SKAI HD" and the number between the two underscores is between 1439 and 1740, replace that string with the new string "MSR2023"
code
if (InStr(filename5, "SKAI HD") && InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 1439 && number <= 1740)
{
filename6 := StrReplace(filename5, "SKAI HD", "MSR2023")
}
}
; If a filename contains the string "MEGA HD" and the number between the two underscores is between 0450 and 0730 replace that string with the new string "Κοινωνία Ώρα Μega"
code
if (InStr(filename5, "MEGA HD") && InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 450 && number <= 730)
{
filename6 := StrReplace(filename5, "MEGA HD", "Κοινωνία Ώρα Μega")
}
}
; If a filename contains the string "ANT1 HD" and the number between the two underscores is between 1940 and 2130,,; replace that string with the new string "Ράδιο Αρβύλα"
code
if (InStr(filename5, "ANT1 HD") && InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 1940 && number <= 2110)
{
filename6 := StrReplace(filename5, "ANT1 HD", "Ράδιο Αρβύλα")
}
}
; If a filename contains the string "ERT 1 HD" and the number between the two underscores is between 2130 and 2250,,; replace that string with the new string "Κάνε ότι Κοιμάσαι"
if (InStr(filename5, "ERT 1 HD") && InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 2130 && number <= 2250)
{
filename6 := StrReplace(filename5, "ERT 1 HD", "Κάνε ότι Κοιμάσαι")
}
}
;{ If a filename contains the string "MEGA HD" and the number between the two underscores is between 2300 and 0030, replace that string with the new string "Milky Way"
code
if (InStr(filename5, "MEGA HD") && InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 2300 && number <= 2359) or (number >= 0000 && number <= 30)
{
filename6 := StrReplace(filename5, "MEGA HD", "Milky Way")
}
}
;}
; If a filename contains the string "ALPHA HD" and the number between the two underscores is between 1740 and 1850 and it's a weekend, replace that string with the new string "Το Σόι Σου"
code
if (InStr(filename5, "ALPHA HD") && InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 1740 && number <= 1850) and (Weekday = "Saturday" or "Sunday")
{
filename6 := StrReplace(filename5, "ALPHA HD", "Το Σόι Σου")
}
}
; If a filename contains the string "ALPHA HD" and the number between the two underscores is between 1255 and 1400, replace that string with the new string "Το Σόι Σου"
if (InStr(filename5, "ALPHA HD") && InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 1255 && number <= 1400)
{
filename6 := StrReplace(filename5, "ALPHA HD", "Το Σόι Σου")
}
}
; If a filename contains the string "ALPHA HD" and the number between the two underscores is between 2050 and 2230,,; replace that string with the new string "Φόνοι Στο Καμπαναριό"
code
if (InStr(filename5, "ALPHA HD") and InStr(filename5, "_"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 2050 and number <= 2230) and (Weekday = "Saturday" or "Friday")
{
filename6 := StrReplace(filename5, "ALPHA HD", "Φόνοι Στο Καμπαναριό")
}
else if (number >= 2050 and number <= 2359) && !(Weekday = "Saturday" or "Friday")
; else if (number >= 2050 and number <= 2359 and !(Weekday = "Friday"))
{
filename6 := StrReplace(filename5, "ALPHA HD", "ALPHA HD")
}
code
}
; If a filename contains the string "ALPHA HD" and the number between the two underscores is between 1950 and 2100, replace that string with the new string "Μην Αρχίζεις Τη Μουρμούρα"
code
if (InStr(filename5, "ALPHA HD") && InStr(filename5, "_") && InStr(filename5, "2023"))
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if (number >= 1950 && number <= 2100) and (Weekday = "Sunday")
{
filename6 := StrReplace(filename5, "ALPHA HD", "Μην Αρχίζεις Τη Μουρμούρα S11E")
}
}
if (InStr(filename5, "STAR HD") && InStr(filename5, "_")) ;; this includes ranges, seems to work, not sure. OR is expressed differently.
{
number := SubStr(filename5, InStr(filename5, "_") + 1, 4)
if ((number >= 2050 && number <= 2359) || (number >= 0 && number <= 200)) && (Weekday = "Friday")
{
filename6 := StrReplace(filename5, "STAR HD", "Fame Story Live")
}
else if ((number >= 2250 && number <= 2359) || (number >= 0 && number <= 130)) && !(Weekday = "Friday")
{
filename6 := StrReplace(filename5, "STAR HD", "Fame Story")
}
else
{
filename6 := StrReplace(filename5, "STAR HD", "STAR HD")
}
}
Please make sure to check for time ranges but also for days using the Weekday variable. Also make sure that when a value for Weekday is specified i.e Friday which is compared with these lines I've provided:
code
date := month . "/" . day . "/" . year
;MsgBox, Converted string: %DATETOFEED%
FormatTime, Weekday , % Substr(date,7)Substr(date,1,2)Substr(date,4,2), dddd
Please make sure that this matches ONLY files in which their date suggests it was a Friday indeed.
For example, using the code I've showed you, the file "STAR HD-K8 Φάρμα (Ε)(02112023_233731)" should become "Fame Story - 2023-11-02_23.37." and "STAR HD-K8 Fame Story - Live(03112023_231322)" should become "Fame Story Live - 2023-11-03_23.13". NOT the other way around.
Assistant
To simplify your AutoHotkey script and ensure that the renaming logic includes the correct weekdays and time ranges, you can modularize the conditions into functions and reduce repetitive code. I will refactor your script by creating a function to extract the time number and another to handle the replacement logic. This will make the script shorter and more readable, and it will ensure that the weekday logic is correctly applied.
Here's the refactored script:
autohotkey
; Function to get the number between two underscores
GetNumber(str) {
return InStr(str, "_") ? SubStr(str, InStr(str, "_") + 1, 4) : ""
}
; Function to do the string replacement based on conditions
ReplaceFilename(original, keyword, newString, numLow, numHigh, weekdayCheck:="", weekday:="") {
number := GetNumber(original)
if (number >= numLow && number <= numHigh && InStr(original, keyword) && InStr(original, "_")) {
if (weekdayCheck = "" || weekday = weekdayCheck) {
return StrReplace(original, keyword, newString)
}
}
return original
}
; Function to format the date and get the weekday
GetWeekdayFromFilename(str) {
day := SubStr(str, InStr(str, "_") - 8, 2)
month := SubStr(str, InStr(str, "_") - 6, 2)
year := SubStr(str, InStr(str, "_") - 4, 4)
date := year . month . day
FormatTime, Weekday, %date%, dddd
return Weekday
}
; Main renaming logic
filename6 := filename5 ; Initialize filename6 with filename5
Weekday := GetWeekdayFromFilename(filename5)
; Replace conditions
filename6 := ReplaceFilename(filename6, "SKAI HD", "MSR2023", 1439, 1740)
filename6 := ReplaceFilename(filename6, "MEGA HD", "Κοινωνία Ώρα Μega", 450, 730)
filename6 := ReplaceFilename(filename6, "ANT1 HD", "Ράδιο Αρβύλα", 1940, 2110)
filename6 := ReplaceFilename(filename6, "ERT 1 HD", "Κάνε ότι Κοιμάσαι
User
Your message seems truncated
Assistant
My apologies for that. Let's continue and complete the refactoring of your script: