0

I am attempting to find the most recent files in a network folder using a wild card. The code within the batch file is provided below. When the .bat file is executed it returns an error


for /f %%f in ('dir /b /s "\\winp-xxx-123\mcr_star112_stsaxg\MDCR\FEMUR.*ohl*"') do (

    set fileDateTime=%%~tf

    set fileName=%%~nxf

    echo !fileDateTime!

    echo !fileName!
)

RunTime ERROR: The network name cannot be found.

Any assistance on what i am doing incorrectly?

Thank you in advance for your help. :)

JavaGirl

2
  • I'm not posting this as an answer since I don't know if it works for sure, but are you able to assign the path to a variable with the wilcard included and then pass it into your loop instead of hard coding it into your for loop??
    – mully
    Commented May 17, 2022 at 20:06
  • I copied the batch and used the path to a folder on my NAS. I copied the first part of the network path from a Windows cmd.exe console window, from the output of the NET USE command. I had to add this line at the start of the script setlocal enabledelayedexpansion because of the !variables! in the loop. It worked fine. I assume the path actually exists (e.g. that DIR works?) Commented May 17, 2022 at 20:20

1 Answer 1

0
@echo off 

set "_file="
set "_cmd=dir /b /a:-d /o:-d /t:w"
  
for /f "usebackq tokens=* delims=" %%i in (`
     %_cmd% "\\winp-xxx-123\mcr_star112_stsaxg\MDCR\FEMUR.*ohl*"
     `)do set "_Fdate=%%~ti" && set "_Fname=%%~nxi" && goto %:ˆ)

%:ˆ)
echo; your most recent %_Fname% %_Fdate% 

List your files in order to get the most recent ones in your folder

  [sorted]   Sorted by /O[:]sortorder
   /O:D   Date & time           /O:-D   Date & time

  [time] /T:  the time field to display & use for sorting
   /T:W   Last Written (default)

  • /o:-d reverse order, most recent first
  • /a:-d files only, exclude directories

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .