Question

Need your help I need create a batch file (command prompt) to ⁃ Show a list of folders and sub folders ⁃ within them are exe files ⁃ Only show the 2 most up to date exe files ⁃ display specific folders not all

And export information in a txt file

I'm using XP if that helps

update

I have the below commands

first one works and orders by most recent file, but doesn't give me time and date

second shows time and date but doesn't order by most recent


@ECHO OFF
setlocal EnableDelayedExpansion
set j=0
Echo Test
echo\
FOR /f "delims=" %%i IN ('dir C:\test\ /o-n-d /b') DO (
    echo %%i
    set /A j=j+1
    if !j! geq 2 (
        goto :end1
    )
)

:end1

@ECHO OFF
setlocal EnableDelayedExpansion
set j=0
echo\
Echo Test
echo\
FOR /f "delims=" %%i IN ('forfiles /p C:\testmove /s  /m *.* /C "cmd /c echo @file @fdate @ftime" ') DO (
    echo %%i
    set /A j=j+1
    if !j! geq 2 (
        goto :end2
    )
)

:end2 

pause
Was it helpful?

Solution

@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\sourcedir"
SET "lastdir="
(
 FOR /f "delims=" %%a IN (
  'dir /s /b /a-d "%sourcedir%\*.exe" '
  ) DO (
  IF "%%~dpa" neq "!lastdir!" (
   SET "lastdir=%%~dpa"
   SET /a count=0
   FOR /f "delims=" %%i IN ('dir /s /b /a-d /o:d "%%~dpa\*.exe"') DO IF !count! lss 2 (
    SET /a count+=1
    ECHO %%~ti %%~fi
   )
  )
 )
)>newfile.txt

GOTO :EOF

Produces newfile.txt. You would need to set your required directory name in sourcedir. I showed the data as date/time fullfilename because fullfilename is of variable-length whereas date and time are fixed. Might have been easier if you'd shown us the format you expect - saves guesswork and revisions.


To show the two most recently modified files, change .../b /a-d /o:d "%%~... to .../b /a-d /o:-d "%%~... (note - between the o: and d)

OTHER TIPS

Uses Robocopy to show the two latest modified .exe files in the current folder tree
It also displays the UTC date and time of the two files.

@echo off
setlocal enabledelayedexpansion
set "folder=%cd%"
set c=0
for /f "tokens=1,2,*" %%a in (
    'robocopy "%folder%" "%folder%" "*.exe" /s /is /nocopy /nc /ns /ts /fp /np /ndl /njh /njs /xjd /r:0 /w:0 /l ^| sort /r '
) do echo "%%a %%b" - "%%c" & set /a c+=1 & if !c! EQU 2 goto :done
:done
pause
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top