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


PMB

Member
Local time
11:46 PM
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

System One

  • 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

System One

  • 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

System One

  • 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

System One

  • 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

System One System Two

  • OS
    Windows 11 Pro 24H2 26100.4652
    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
  • Operating System
    Windows 11 Pro 24H2 26100.4061
    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 26100.4061
Scripting is very handy for screwing up a lot of things, quickly. 😂
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4652]
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    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

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    home built
    CPU
    AMD 7900x
    Motherboard
    ASUS AMD x670E ROG Strix E-A
    Memory
    64 GB
    Graphics Card(s)
    Nvidia 3060 Ti (but wanting to upgrade)
    Sound Card
    built-in
    Monitor(s) Displays
    Dell 24"
    Cooling
    AIO for CPU, fans for case
    Keyboard
    Das Keyboard 4
    Mouse
    Corsair M65 (white)
    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

System One System Two

  • 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
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • 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
    20Mbit/s up, 250Mbit/s down
    Browser
    FF

My Computers

System One System Two

  • 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
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • 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
    20Mbit/s up, 250Mbit/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

System One System Two

  • OS
    Win11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self-built
    CPU
    Intel i7 11700K
    Motherboard
    ASUS Prime Z590-A MB
    Memory
    64GB (Waiting for warranty replacement of another 64GB for 128GB total)
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    HP Envy 32
    Screen Resolution
    2560 x 1440
    Hard Drives
    1 x 1TB NVMe SSD
    1 x 2TB NVMe SSD
    1 x 4TB NVMe SSD
    3 x 512GB 2.5" SSD
    1 x 4TB 2.5" SSD
    5 x 8TB Seagate Barracuda HDD
    PSU
    Corsair HX850i
    Case
    Corsair iCUE RGB 5000X mid tower case
    Cooling
    Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Home Computer Specifications, Configuration, and Usage Notes General Specifications ASUS Prime Z590-A motherboard, serial number M1M0KC222467ARP Intel Core i7-11700K CPU (11th Gen Rocket Lake / LGA 1200 Socket) 128GB Crucial Ballistix RGB DDR4 3200 MHz DRAM (4 x 32GB) Corsair iCUE RGB 5000X mid tower case Noctua NH-D15 chromax.black CPU cooler Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Corsair LL-120 RGB Fans (Qty. 3)
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    The five 8TB drives and three 512GB SSDs are part of a DrivePool using StableBit DrivePool software. The three SSDs are devoted purely to caching for the 8TB drives. All of the important data is stored in triplicate so that I can withstand simultaneous failure of 2 disks.

    Networking: 2.5Gbps Ethernet and WiFi 6e
  • Operating System
    Win11 Pro 23H2
    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

Latest Support Threads

Back
Top Bottom