Help With Batch File


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.
Here is my full code that compares the 3 files to see which has the most current date.
@echo off
setlocal

:: Set file paths
set "file1=C:\DataBaseFolder\D_Johnsons_DB_01.04.23.accdb"
set "file2=C:\Users\David.Johnson\OneDrive - Technologies\Work\DataBaseFolder\D_Johnsons_DB_01.04.23.accdb"
set "file3=C:\Users\David.Johnson\OneDrive - Technologies\Work\DataBaseFolder\Laptop Backup Folder\D_Johnsons_DB_01.04.23.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= in the C:\DataBaseFolder\ folder on"
set "newestDate=%date1%"

if "%date2%" GTR "%newestDate%" (
set "newest=in the C:\Users\David.Allan.Johnson\OneDrive - RedSail Technologies, LLC\Work\DataBaseFolder\ folder on"
set "newestDate=%date2%"
)

if "%date3%" GTR "%newestDate%" (
set "newest=in the C:\Users\David.Allan.Johnson\OneDrive - RedSail Technologies, LLC\Work\DataBaseFolder\Laptop Backup Folder\ folder on"
set "newestDate=%date3%"
)

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

endlocal
%SystemRoot%\explorer.exe %newest% (%newestDate%)

pause
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
PC/Desktop
Manufacturer/Model
Dell
This is never going to work. Let's say folder 3 is the winner, you're setting newest to "in the C:\Users\David.Allan.Johnson\OneDrive - RedSail Technologies, LLC\Work\DataBaseFolder\Laptop Backup Folder\ folder on" and expecting Explorer to open that folder.

You need to set %newest% to just the path of the folder, and take that %newestDate% thing off the call to Explorer.
 

My Computer My Computer

At a glance

Windows 11 Enterprise 25H2
OS
Windows 11 Enterprise 25H2
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).
When I run the batch file, it just comes up with the Explorer Home and not the path that the file is located in.
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
PC/Desktop
Manufacturer/Model
Dell
When I run the batch file, it just comes up with the Explorer Home and not the path that the file is located in.
Right, because you're passing it something that doesn't exist.
 

My Computer My Computer

At a glance

Windows 11 Enterprise 25H2
OS
Windows 11 Enterprise 25H2
Powershell:
[string[]]$filePaths = @(
    'C:\DataBaseFolder\D_Johnsons_DB_01.04.23.accdb',
    'C:\Users\David.Johnson\OneDrive - Technologies\Work\DataBaseFolder\D_Johnsons_DB_01.04.23.accdb',
    'C:\Users\David.Johnson\OneDrive - Technologies\Work\DataBaseFolder\Laptop Backup Folder\D_Johnsons_DB_01.04.23.accdb'
)

Get-Item -Path $filePaths -ErrorAction SilentlyContinue |
Sort-Object -Property 'LastWriteTime' | Select-Object -Last 1 |
ForEach-Object { Start-Process 'explorer.exe' -ArgumentList @($_.DirectoryName) }

Here's the PowerShell equivalent, much easier to understand.... Get these three specific files.... Sort them by their last write (modified) time... Pick the last one... Start explorer and give it the name of that file's directory.
 
Last edited:

My Computer My Computer

At a glance

Windows 11 Enterprise 25H2
OS
Windows 11 Enterprise 25H2
True. But they are still unnecessary in a set command specifically even of you have a path with spaces.
That depends. The characters <, >, |, &, and ^ are special command shell characters, and they must be preceded by the escape character (^) or enclosed in quotation marks when used in <string> (for example, "StringContaining&Symbol"). In a folderpath, the characters <, >, and | are not allowed, but & and ^ most certainly are.

If you use quotation marks to enclose a string that contains one of the special characters, the quotation marks are set as part of the environment variable value but you can avoid this if that's what you want, i.e., by moving the first quotation mark directly in front of the variable name like @pseymour has shown (see post #4).

In short, if you want to avoid having to rely on the escape character and you want to avoid all the excruciating pains that are commonly associated with it, then quotes are very much necessary. That is, unless you can be completely sure that the folderpath does not contain any of the special characters that need to be escaped.

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 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
1748634160084.webp
 

My Computer My Computer

At a glance

Windows 11 Pro
OS
Windows 11 Pro
Thanks for your help everyone. Adding the "" around the full path fixed it. Works great now.
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
PC/Desktop
Manufacturer/Model
Dell
Back
Top Bottom