tags:

views:

7200

answers:

7

I need to copy the newest file in a directory to a new location. So far I've found resources on the forfiles command, a date-related question here, and another related question. I'm just having a bit of trouble putting the pieces together! How do I copy the newest file in that directory to a new place?

+1  A: 

I know you asked for Windows but thought I'd add this anyway,in Unix/Linux you could do:

cp `ls -t1 | head -1` /somedir/

Which will list all files in the current directory sorted by modification time and then cp the most recent to /somedir/

Swish
+2  A: 

This will open a second cmd.exe window. If you want it to go away, replace the /K with /C.

Obviously, replace new_file_loc with whatever your new file location will be.

@echo off
for /F %%i in ('dir /B /O:-D *.txt') do (
    call :open "%%i"
    exit /B 0
)
:open
    start "window title" "cmd /K copy %~1 new_file_loc"
exit /B 0
mos
A: 

Bash:

 find -type f -printf "%T@ %p \n" \
     | sort  \
     | tail -n 1  \
     | sed -r "s/^\S+\s//;s/\s*$//" \
     | xargs -iSTR cp STR newestfile

where "newestfile" will become the newestfile

alternatively, you could do newdir/STR or just newdir

Breakdown:

  1. list all files in {time} {file} format.
  2. sort them by time
  3. get the last one
  4. cut off the time, and whitespace from the start/end
  5. copy resulting value

Important

After running this once, the newest file will be whatever you just copied :p ( assuming they're both in the same search scope that is ). So you may have to adjust which filenumber you copy if you want this to work more than once.

Kent Fredric
+7  A: 

Windows shell, one liner:

FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT
PabloG
That should probably be exit /b or goto :eof to avoid closing the cmd window ...
Joey
Cheburek
+3  A: 

The accepted answer gives an example of using that newest file in a command and then exiting. If you need to do this in a bat file with other complex operations you can use the following to store the file name of the newest file in a variable:

FOR /F "delims=|" %%I IN ('DIR "*.*" /B /O:D') DO SET NewestFile=%%I

Now you can reference %NewestFile% throughout the rest of your bat file.

For example here is what we use to get the latest version of a database .bak file from a directory, copy it to a server, and then restore the db:

:Variables
SET DatabaseBackupPath=\\virtualserver1\Database Backups

echo.
echo Restore WebServer Database
FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\WebServer\*.bak" /B /O:D') DO SET NewestFile=%%I
copy "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"

sqlcmd -U <username> -P <password> -d master -Q ^
"RESTORE DATABASE [ExampleDatabaseName] ^
FROM  DISK = N'D:\%NewestFile%' ^
WITH  FILE = 1,  ^
MOVE N'Example_CS' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example.mdf',  ^
MOVE N'Example_CS_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example_1.LDF',  ^
NOUNLOAD,  STATS = 10"
ObligatoryMoniker
Excellent...I was searching for how to get the most recent file, and the code example you give is the exact reason I'm searching for this, restoring a sql server backup!
tbone
Your welcome, glad I could help =).
ObligatoryMoniker
A: 

hi experts,

i wanted to do something similar, the different is i want to get the directory/path from other text file. So this will make 2 for loop. but my nested for is not working.. :-

@echo off

For /F "tokens=1,2,3,4" %%a in (Tools.txt) Do ( rem trying to get C:\test (%%b) directory from Tools.txt

for /f "delims=" %%m in ('dir /od /a-d /b %%b*.txt') do set recent=%%m echo %recent% >> recent.txt
rem trying to see what value it got here-should be the path dir from Tools.txt )

but this didnot work, all i got in the for the %recent% value is Echo is off. the value should contain the latest filename in c:\test diretory :( thanks in advance

maya
A: 

This does not work. The 'DIR . /B /O:-D' command lists all the files in descending date order, not the newest file. As a result the For command copies ALL files to the new directory.

Dems
Yes, but when first file will be copied EXIT command will break circle
Cheburek