0

I'm trying to create a batch file that runs through several folders in a for /d listing and then echoes the oldest file in the folder. Ultimately, this will be used to move files to another folder. The problem I have is I can't get the for /f to escape without ending the entire command.

@echo off
setlocal enableextensions disabledelayedexpansion

set basefolder=M:\somefolder\*
for /D %%D in (%basefolder%) do (

FOR /F %%I IN ('DIR %%D\* /B /O:-D') DO echo "%%I" & goto moved
:moved
)

This get me ") was unexpected"

@echo off
setlocal enableextensions disabledelayedexpansion

set basefolder=M:\somefolder\*
for /D %%D in (%basefolder%) do (

FOR /F %%I IN ('DIR %%D\* /B /O:-D') DO echo "%%I" & exit /b
)

This, of course, exits the command at the first folder that processed.

Can someone suggest something that might actually work?

GaryD
  • 3
  • 2

1 Answers1

2
for /D %%D in (%basefolder%) do (
 set "show=Y"    
 FOR /F %%I IN ('DIR %%D\* /B /O:-D') DO if defined show (
  echo "%%I"
  set "show="
 )
)

Using the fact that cmd uses the current value of a variable for if defined.

Note that labels cannot be used within a code-block (parenthesised series of commands)

Magoo
  • 77,302
  • 8
  • 62
  • 84