Windows CMD shell and variable expansion with substring (~), question.


PMB

Member
Local time
4:08 AM
Posts
9
OS
Windows 11
Hi. I am trying to learn about Windows batch files and I am looking for some more complete documentation. I have found coding examples for pulling substrings out of longer strings and saving them in environment variables. See below example which demonstrates the substring syntax to extract the year, day, and month from the date.

set my_year=%date:~0,4%
set my_day=%date:~8,2%
set my_month=%date:~5,2%

While this is useful I would like to read and learn more about this syntax but I can't seem to find anything that more fully describes (i.e. what else can is possible with this syntax).
Can anyone point me towards a book or Microsoft documentation that goes into more detail about this substring syntax?

Thanks,
Peter
 
Windows Build/Version
Windows 11 Home, 24H2, OS Build 26100.3194

My Computer My Computer

At a glance

Windows 11i7-13700K 3.40 GHz64 GBGIGABYTE RTX 3070 Ti
OS
Windows 11
Computer type
PC/Desktop
Manufacturer/Model
ASUS ROG STRIX Z790 E
CPU
i7-13700K 3.40 GHz
Motherboard
ASUS ROG STRIX Z790 E
Memory
64 GB
Graphics Card(s)
GIGABYTE RTX 3070 Ti
Sound Card
n/a
Monitor(s) Displays
BenQ SW2700 inch & Dell P1917S
For beginners, a good place on CMD scripting basics is SS64.com:
variable substring - Windows CMD - SS64.com

A common gotcha is assuming the date/time will always be displayed in the same format. This may be true if you're writing a script for yourself, but it may not work for someone who uses a different date/time setting. Just be aware.
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
Thanks. I had found this link and yes, it does describe in more detail what I was looking for. I guess it also made me wonder what else is possible. It's a case of I don't know what I don't know. I was hoping to find more context around this subject. I was surprised that I couldn't find a least this kind of explanation from Microsoft, but then again maybe my search skills are not what they could be.

Thanks again,
Peter
 

My Computer My Computer

At a glance

Windows 11i7-13700K 3.40 GHz64 GBGIGABYTE RTX 3070 Ti
OS
Windows 11
Computer type
PC/Desktop
Manufacturer/Model
ASUS ROG STRIX Z790 E
CPU
i7-13700K 3.40 GHz
Motherboard
ASUS ROG STRIX Z790 E
Memory
64 GB
Graphics Card(s)
GIGABYTE RTX 3070 Ti
Sound Card
n/a
Monitor(s) Displays
BenQ SW2700 inch & Dell P1917S
CMD has fallen out of favor, since PowerShell is now universal on W10/11 platforms. CMD has too many limitations as an older scripting language, it's better to learn PowerShell. I'm definitely more productive writing PS compared to CMD.

The only reason today to learn CMD from scratch is to debug someone's legacy CMD script you're stuck with.
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
Much respect and kudos for you wanting to learn @PMB . Scripting has always been an extremely gray area for me, as is the registry, and both are real mental blocks for me. I figured out right quick it was somethining I would never learn for myself at this late stage of my life. Luckily, I have @garlin @Brink and a few others I can rely on to do it right rather than me royally mess up things myself. Love those guys for always sharing their abilities with others.
 

My Computers My Computers

  • At a glance

    Windows 11 Pro 25H2 26200.8655i9-10900 10 core 20 threads32 gbnone-Intel UHD Graphics 630
    OS
    Windows 11 Pro 25H2 26200.8655
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 7080
    CPU
    i9-10900 10 core 20 threads
    Motherboard
    DELL 0J37VM
    Memory
    32 gb
    Graphics Card(s)
    none-Intel UHD Graphics 630
    Sound Card
    Integrated Realtek
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    2x1tb Solidigm m.2 nvme /External drives 512gb Samsung m.2 sata+2tb Kingston m2.nvme
    PSU
    500w
    Case
    MT
    Cooling
    Dell Premium
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    so slow I'm too embarrassed to tell
    Browser
    #1 Edge #2 Firefox
    Antivirus
    Defender+MWB Premium
  • At a glance

    Windows 11 Pro 24H2 26200.8457AMD Ryzen 7 6800U32 gbintegrated
    Operating System
    Windows 11 Pro 24H2 26200.8457
    Computer type
    PC/Desktop
    Manufacturer/Model
    Beelink Mini PC SER5
    CPU
    AMD Ryzen 7 6800U
    Memory
    32 gb
    Graphics card(s)
    integrated
    Sound Card
    integrated
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1TB Crucial nvme
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    still too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender
    Other Info
    System 3 is non compliant Dell 9020 i7-4770/24gb ram Win11 PRO 26200.8457
Scripting is very handy for screwing up a lot of things, quickly. 😂
 

My Computer My Computer

At a glance

Windows 11 Pro 25H212th Gen Core i7-1260P64 GB Micron PC4-25600Intel Iris Xe Graphics
OS
Windows 11 Pro 25H2
Computer type
PC/Desktop
Manufacturer/Model
Intel NUC12WSHi7
CPU
12th Gen Core i7-1260P
Motherboard
NUC12WSBi7
Memory
64 GB Micron PC4-25600
Graphics Card(s)
Intel Iris Xe Graphics
Sound Card
on-board Realtek HD Audio
Monitor(s) Displays
Dell U3219Q
Screen Resolution
3840 x 2160
Hard Drives
Samsung SSD 990 PRO 1TB
Crucial MX500 2 TB
Antivirus
Microsoft Defender
CMD has fallen out of favor, since PowerShell is now universal on W10/11 platforms. CMD has too many limitations as an older scripting language, it's better to learn PowerShell. I'm definitely more productive writing PS compared to CMD.

No doubt. How long did it take you to get that proficient?
The only reason today to learn CMD from scratch is to debug someone's legacy CMD script you're stuck with.
But some of us old-timers grew up with CMD. I have tried to learn PS on several occasions, but it's a steep learning curve when i look at a lot of the online documentation. Is there a source for learning just enough PS to find my way around the documentation? I know i can always rely on ChatGPT, but I'll never learn PS if all I do is cut-and-paste.
 

My Computer My Computer

At a glance

Windows 11 2H25AMD 9900X64 GBAMD 9070 XT
OS
Windows 11 2H25
Computer type
PC/Desktop
Manufacturer/Model
DIY
CPU
AMD 9900X
Motherboard
MSI X870E Carbon
Memory
64 GB
Graphics Card(s)
AMD 9070 XT
Sound Card
built-in
Monitor(s) Displays
Dell 24"
Hard Drives
Sabrent 1 TB NVMe, 4 x SSD (need to check models), 4 x 3.5" HDD, 8-16 TB, all WD
PSU
Seasonic 850
Case
Fractal Design North XL (which I likw)
Cooling
Corsair AIO for CPU, fans for case
Keyboard
Das Keyboard 4
Mouse
Corsair M65 (white)
Internet Speed
1 TB download
Browser
Firefox
Antivirus
Bitdefender
Other Info
Also have Lenovo T14S laptop (me) and Lenovo Slim 71 (wife)
I guess it also made me wonder what else is possible. It's a case of I don't know what I don't know. I was hoping to find more context around this subject. I was surprised that I couldn't find a least this kind of explanation from Microsoft, but then again maybe my search skills are not what they could be.
See: https://superuser.com/a/1569219

Known bug since 2012: https://stackoverflow.com/a/13843928
Still not fixed in 2025: How to get my own path in a batch-file. 🥱
 

My Computers My Computers

  • At a glance

    11 Homei7 13650HX16GB DDR5GeForce RTX 4060 Mobile
    OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • At a glance

    11 Homei5 1135G716GB DDR4Intel Iris Xe
    Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    30Mbit/s up, 500Mbit/s down
    Browser
    FF

My Computers My Computers

  • At a glance

    11 Homei7 13650HX16GB DDR5GeForce RTX 4060 Mobile
    OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • At a glance

    11 Homei5 1135G716GB DDR4Intel Iris Xe
    Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
CMD has fallen out of favor, since PowerShell is now universal on W10/11 platforms. CMD has too many limitations as an older scripting language, it's better to learn PowerShell. I'm definitely more productive writing PS compared to CMD.

The only reason today to learn CMD from scratch is to debug someone's legacy CMD script you're stuck with.

You are absolutely right about this. I keep wanting to start learning PowerShell but it seems a bit daunting of a task.

I guess that I should finally get my butt in gear and start doing this.
 

My Computers My Computers

  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-14650HX32 GBNo GPU - Built-in Intel Graphics
    OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-1255U16 GBIntel Iris Xe Graphics
    Operating System
    Win11 Pro 25H2 (RTM+)
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
Back
Top Bottom