Help With Batch File


Crazy Dave

New member
Local time
10:38 PM
Posts
6
OS
Windows 11
Good morning,

I hope someone can help me with this. I have the following batch file that I'm comparing the most current dates for 3 files. I want to open the location for the most current file folder. I'm not sure what the correct syntax is to open a folder path location that's held within a variable.

@echo off
setlocal

:: Set file paths
set "file1=C:\Folder1\File1.accdb"
set "file2=C:\Folder2\File2.accdb"
set "file3=C:\Folder3\File3.accdb"

:: Get timestamps
for %%F in ("%file1%") do set "date1=%%~tF"
for %%F in ("%file2%") do set "date2=%%~tF"
for %%F in ("%file3%") do set "date3=%%~tF"

:: Display timestamps
echo File 1: %file1% - %date1%
echo File 2: %file2% - %date2%
echo File 3: %file3% - %date3%

:: Compare dates
set "newest= Folder1"
set "newestDate=%date1%"

if "%date2%" GTR "%newestDate%" (
set "newest= Folder2"
set "newestDate=%date2%"
)

if "%date3%" GTR "%newestDate%" (
set "newest= Folder3"
set "newestDate=%date3%"
)

echo.

echo The most recently modified file is: %newest% (%newestDate%)

endlocal

%SystemRoot%\explorer.exe %newest%

pause
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell
explorer.exe X:\Path\To\Folder should work. What is in the variable %newest%?
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4349]
    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
Good morning,

I hope someone can help me with this. I have the following batch file that I'm comparing the most current dates for 3 files. I want to open the location for the most current file folder. I'm not sure what the correct syntax is to open a folder path location that's held within a variable.

@echo off
setlocal

:: Set file paths
set file1="C:\Folder1\File1.accdb"
set file2="C:\Folder2\File2.accdb"
set file3="C:\Folder3\File3.accdb"
I have not looked at the script but the quotes are wrong in the var definitions..
 

My Computers

System One System Two

  • OS
    Windows 11 Pro/All Channels
    Computer type
    Laptop
    Manufacturer/Model
    Acer Nitro ANV15-51
    CPU
    AMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 2
    Motherboard
    Sportage_RBH
    Memory
    32 GB DDR5
    Graphics Card(s)
    Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB GDDR6
    Sound Card
    AMD/Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (15.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9 144Hz
    Hard Drives
    Kingston OM8SEP4512Q 500 GB
    WDS500G2B0A-00SM50 500.1 GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & IGB Orange-fr
    Browser
    Edge Canary- Firefox Nightly-Chrome Dev-Chrome Dev
    Antivirus
    Windows Defender
    Other Info
    VMs of Windows 11 stable/Beta/Dev/Canary
    VM of XeroLinux- Arch based & Debian 13 (Trixie)
  • Operating System
    Windows 11 Insider Canary
    Computer type
    Laptop
    Manufacturer/Model
    ASUS X751BP
    CPU
    AMD Dual Core A6-9220
    Motherboard
    ASUS
    Memory
    8 GB
    Graphics card(s)
    AMD Radeon R5 M420
    Sound Card
    Realtek
    Monitor(s) Displays
    17.3
    Screen Resolution
    1600X900 16:9
    Hard Drives
    1TB 5400RPM
The quotes are unnecessary, but it's legal.

Code:
Microsoft Windows [Version 10.0.26100.4061]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Lady.Godiva
> set "file3=C:\Folder3\File3.accdb"

C:\Users\Lady.Godiva
> set f
file3=C:\Folder3\File3.accdb
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4349]
    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
The quotes are unnecessary, but it's legal.

Code:
Microsoft Windows [Version 10.0.26100.4061]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Lady.Godiva
> set "file3=C:\Folder3\File3.accdb"

C:\Users\Lady.Godiva
> set f
file3=C:\Folder3\File3.accdb
Thank you. Never used it that way.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro/All Channels
    Computer type
    Laptop
    Manufacturer/Model
    Acer Nitro ANV15-51
    CPU
    AMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 2
    Motherboard
    Sportage_RBH
    Memory
    32 GB DDR5
    Graphics Card(s)
    Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB GDDR6
    Sound Card
    AMD/Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (15.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9 144Hz
    Hard Drives
    Kingston OM8SEP4512Q 500 GB
    WDS500G2B0A-00SM50 500.1 GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & IGB Orange-fr
    Browser
    Edge Canary- Firefox Nightly-Chrome Dev-Chrome Dev
    Antivirus
    Windows Defender
    Other Info
    VMs of Windows 11 stable/Beta/Dev/Canary
    VM of XeroLinux- Arch based & Debian 13 (Trixie)
  • Operating System
    Windows 11 Insider Canary
    Computer type
    Laptop
    Manufacturer/Model
    ASUS X751BP
    CPU
    AMD Dual Core A6-9220
    Motherboard
    ASUS
    Memory
    8 GB
    Graphics card(s)
    AMD Radeon R5 M420
    Sound Card
    Realtek
    Monitor(s) Displays
    17.3
    Screen Resolution
    1600X900 16:9
    Hard Drives
    1TB 5400RPM
Quotes are a good habit to follow, because you could have have a path with spaces.
I always use quotes but usually after the '='
 

My Computers

System One System Two

  • OS
    Windows 11 Pro/All Channels
    Computer type
    Laptop
    Manufacturer/Model
    Acer Nitro ANV15-51
    CPU
    AMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 2
    Motherboard
    Sportage_RBH
    Memory
    32 GB DDR5
    Graphics Card(s)
    Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB GDDR6
    Sound Card
    AMD/Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (15.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9 144Hz
    Hard Drives
    Kingston OM8SEP4512Q 500 GB
    WDS500G2B0A-00SM50 500.1 GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & IGB Orange-fr
    Browser
    Edge Canary- Firefox Nightly-Chrome Dev-Chrome Dev
    Antivirus
    Windows Defender
    Other Info
    VMs of Windows 11 stable/Beta/Dev/Canary
    VM of XeroLinux- Arch based & Debian 13 (Trixie)
  • Operating System
    Windows 11 Insider Canary
    Computer type
    Laptop
    Manufacturer/Model
    ASUS X751BP
    CPU
    AMD Dual Core A6-9220
    Motherboard
    ASUS
    Memory
    8 GB
    Graphics card(s)
    AMD Radeon R5 M420
    Sound Card
    Realtek
    Monitor(s) Displays
    17.3
    Screen Resolution
    1600X900 16:9
    Hard Drives
    1TB 5400RPM
Quotes are a good habit to follow, because you could have have a path with spaces.
True. But they are still unnecessary in a set command specifically even of you have a path with spaces. The entire portion of the input string that follows the = character is taken as a new value for the variable, spaces and everything, regardless of whether it is enclosed in quotes.

In this example the quotes are placed in a rather nonsensical fashion, but it still "works" in a set command simply because they don't really affect anything.

In fact one can do set x y = v w (no quotes) and that will produce variable x y (yes, with a space at the end) with value v w (yes, with a space in front).
 

My Computer

System One

  • OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    CPU
    Intel(R) Core(TM) i7-5930K CPU @ 3.50GHz
    Motherboard
    EVGA X99 Micro
    Graphics Card(s)
    NVIDIA GeForce GTX 970
  • Helpful
Reactions: OAT
I said it's a good programming habit. I'll freely admit I'm not a CMD guru, more fluent in PS. But how many times have I been burned when writing batch and you don't put the quotes on the outside of the = expression. This is why I write real scripts in PS, none of this nonsense is required.

CMD has these weird expression rules that everyone just puts up with. And remains broken to maintain backwards compatibility to the '90's.
 

My Computer

System One

  • OS
    Windows 7
Hey, at least we're not writing in KiXtart anymore.
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4349]
    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 these weird expression rules that everyone just puts up with. And remains broken to maintain backwards compatibility to the '90's.
Isn't that because you are more fluent with Powershell though because I really do not know what you mean.
What remains broken? What weird expression rules?
Are you referring to the tilde used for files exceeding the nnnnnnnn.nnn format?
 

My Computers

System One System Two

  • OS
    Windows 11 Pro/All Channels
    Computer type
    Laptop
    Manufacturer/Model
    Acer Nitro ANV15-51
    CPU
    AMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 2
    Motherboard
    Sportage_RBH
    Memory
    32 GB DDR5
    Graphics Card(s)
    Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB GDDR6
    Sound Card
    AMD/Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (15.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9 144Hz
    Hard Drives
    Kingston OM8SEP4512Q 500 GB
    WDS500G2B0A-00SM50 500.1 GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & IGB Orange-fr
    Browser
    Edge Canary- Firefox Nightly-Chrome Dev-Chrome Dev
    Antivirus
    Windows Defender
    Other Info
    VMs of Windows 11 stable/Beta/Dev/Canary
    VM of XeroLinux- Arch based & Debian 13 (Trixie)
  • Operating System
    Windows 11 Insider Canary
    Computer type
    Laptop
    Manufacturer/Model
    ASUS X751BP
    CPU
    AMD Dual Core A6-9220
    Motherboard
    ASUS
    Memory
    8 GB
    Graphics card(s)
    AMD Radeon R5 M420
    Sound Card
    Realtek
    Monitor(s) Displays
    17.3
    Screen Resolution
    1600X900 16:9
    Hard Drives
    1TB 5400RPM
explorer.exe X:\Path\To\Folder should work. What is in the variable %newest%?
This variable holds the file location of which file has the most recent date file was changed.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell
Yes, but what is in it? What is the value?
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4349]
    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
%newest% doesn't appear to be a full path (just plain "Folder2").
 

My Computer

System One

  • OS
    Windows 7
Right, that's what I wanna know what's actually in it... to solve the problem but also show OP how something like this is debugged.
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4349]
    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
He's looking for the literal output of this echo line:
Code:
echo The most recently modified file is: %newest% (%newestDate%)

@pseymour is playing a teaching game of "What am I not looking at correctly?"
 

My Computer

System One

  • OS
    Windows 7
Right, because you're doing set "newest= Folder3", you're never putting the full path in. So when your batch script gets to echo The most recently modified file is: %newest% (%newestDate%), %newest% does not have a full path in it.
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4349]
    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
A common debug trick is to echo the line you're expecting to execute:
Code:
echo %SystemRoot%\explorer.exe %newest%
%SystemRoot%\explorer.exe %newest%

This prints out the calling arguments, so you can confirm you didn't mess up for example, the path to explorer.exe, or the passed on argument(s).
 

My Computer

System One

  • OS
    Windows 7
Back
Top Bottom