Question

I need to move files from c:\prueba1 to c:\prueba99 but I don't know how to make a comparison betweeen all files in the source directory (c:\prueba99) to move all files in the directory with the exception of the last modified file in the directory. I know there is a wmic command with get InstallDate, LastModified, but I don't know the ms-dos syntaxis to asign a variable and compare it to know that one file readed is the last modified

I found an example:

for /f "delims=" %%A in ('wmic datafile where "drive = 'c:' and path='\\windows\\'"
   get LastModified^,Name /format:table^|find ":"^|sort /r') do @echo %%A

And tried to modify it with no result because it appears to just list the datafile names but not the files themselves.

This is my modified version:

for /f "skip=1 delims=" %%A  in ('wmic datafile where "drive = 'c:' and path='\\prueba1\\'"
    get LastModified^,Name /format:table^|find ":"^| sort/r') do move (%%A) c:\prueba99
Was it helpful?

Solution

for /f "skip=1 delims=" %%a in ('dir /b /tw /o-d /a-d c:\prueba1\*.*'
) do move "c:\prueba1\%%a" "c:\prueba99"

dir command get the files in descending creation date order, so the first is the latest. for command iterates over this list, skipping the first one, moving the files to the target folder.

OTHER TIPS

A way to ge the last modified file :

@echo off
set $path=c:\prueba99

for /f %%a in ('dir/b/a-d/o-d "%$path%"') do (
set $Last=%%a
goto:next)

:next
echo Last modified : [ %$Last% ]

This should work for you and it also allows you to discard as many NEWEST files as you want (for your case I've taken 1):

@echo off
setlocal

:: change the next two statements to match what you want
set srcdir=C:\prueba1
set tgtdir=C:\prueba99
if not exist %tgtdir%\*.* md %tgtdir%
set ctr=0
for /f "tokens=*" %%a in ('dir "%srcdir%" /o-d /b') do call :maybemove "%%a"
set /a ctr-=3
echo %~n0: %ctr% files were moved from %srcdir% to %tgtdir%
endlocal
goto :eof

::--------------------

:maybemove
:: increment counter and bypass the ONE newest file
set /a ctr+=1
if %ctr% leq 1 goto :eof
:: remove the double-quotes from the front and back of the filename
set fn=%~1
:: perform the move
move /y "%srcdir%\%fn%" "%tgtdir%"
goto :eof
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top