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

System One

  • 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

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4351]
    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).
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

System One

  • 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

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4351]
    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
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

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4351]
    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
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

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
    Mouse
    Logitech G402
    Keyboard
    Logitech K800
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
Thanks for your help everyone. Adding the "" around the full path fixed it. Works great now.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell

Latest Support Threads

Back
Top Bottom