set /p day=<date.dat set dd=%day:~9,2% set mm=%day:~6,2% set yyyy=%day:~1,4% :: Mapping…
- User
set /p day=<date.dat set dd=%day:~9,2% set mm=%day:~6,2% set yyyy=%day:~1,4% :: Mapping month numbers to abbreviations if "%mm%"=="01" set mmm=Jan if "%mm%"=="02" set mmm=Feb if "%mm%"=="03" set mmm=Mar if "%mm%"=="04" set mmm=Apr if "%mm%"=="05" set mmm=May if "%mm%"=="06" set mmm=Jun if "%mm%"=="07" set mmm=Jul if "%mm%"=="08" set mmm=Aug if "%mm%"=="09" set mmm=Sep if "%mm%"=="10" set mmm=Oct if "%mm%"=="11" set mmm=Nov if "%mm%"=="12" set mmm=Dec del date.dat
how to make it shorter
- Assistant
You can simplify your script using a single array or a simpler conditional structure to handle the month-to-abbreviation mapping. Here's one way to do it by using an array-like syntax in
cmdwith delayed expansion:@echo off setlocal enabledelayedexpansion set /p day=<date.dat set dd=%day:~9,2% set mm=%day:~6,2% set yyyy=%day:~1,4% :: Define the month abbreviations set mmm[01]=Jan set mmm[02]=Feb set mmm[03]=Mar set mmm[04]=Apr set mmm[05]=May set mmm[06]=Jun set mmm[07]=Jul set mmm[08]=Aug set mmm[09]=Sep set mmm[10]=Oct set mmm[11]=Nov set mmm[12]=Dec :: Access the month abbreviation using delayed expansion set mmm=!mmm[%mm%]! del date.dat :: Optionally, display the result echo Date: %dd% !mmm! %yyyy%Explanation:
setlocal enabledelayedexpansion: Enables delayed variable expansion, allowing you to use!variable!syntax to access updated variable values within the same block.- The month abbreviations are stored in a pseudo-array format using the structure
set mmm[MM]=Abbreviation. - To get the abbreviation, you can access it with , reducing the number of lines and making it easier to manage.