1.

Solve : Help with a batch file & date last modified?

Answer»

Wish list

I would like to be able to copy a folders contents into another computer on our network. I know that is easy but here is where I'm stuck. I want to copy the last modified document to the target folder. Here is what I have that works to move the document out of the folder in "C:"

Copy C:\PROG P:
Copy C:\_Labels L:

I have tried (date.lastmodified) in a few combos. Anyone have a solution for me? Is there a command that I can write for "last date modified & Time"?
Thanks if you can help me.Sorry you haven't received a response earlier.

This may help.

Quote

@echo off

set indir=input directory
set outdir=output directory

for /f "usebackq delims=" %%I in (`dir /b /a:-d /o:-d "%indir%"`) do (
copy "%%I" "%outdir%"
goto done
)

:done
echo.
echo Done.
pause >nul
exit

If you are still 'here', tell me if that works as expected, thanks.By looking at DeltaSlaya's code, I think it will copy all files (although in order of date) and I think it requires that the current directory be the SOURCE directory. To copy only the last modified file, I think you could do
Code: [Select]@echo off
setlocal
for /f "delims=" %%a in ('dir C:\Source /a-d /od /b') do set recentfile=%%a
copy "C:\Source\%recentfile%" "C:\Destination"Quote from: GuruGary on September 08, 2007, 07:32:00 AM
By looking at DeltaSlaya's code, I think it will copy all files (although in order of date) and I think it requires that the current directory be the source directory. To copy only the last modified file, I think you could do
Code: [Select]@echo off
setlocal
for /f "delims=" %%a in ('dir C:\Source /a-d /od /b') do set recentfile=%%a
copy "C:\Source\%recentfile%" "C:\Destination"

No, and no. As you can clearly SEE it only gets one file because the loop is only initated once and then the code at the :done label is executed. It does not require the source directory to be where you get the files from, thats why there is a clearly labeled variable that you set, aptly named indir.Quote from: DeltaSlaya on September 08, 2007, 03:22:11 PM
No, and no. As you can clearly see it only gets one file because the loop is only initated once and then the code at the :done label is executed. It does not require the source directory to be where you get the files from, thats why there is a clearly labeled variable that you set, aptly named indir.

OK, sorry. I do see that it will only copy the last modified file, but I still think it will only work if the file EXISTS in the current directory. If you change
Code: [Select]copy "%%I" "%outdir%"to
Code: [Select]copy "%indir%\%%I" "%outdir%"then I think it will work.here's a vbscript
Code: [Select]
' *****************************************************************
' * Name: CopyLastModifiedFile.vbs *
' * Function: Copy last modified file to target folder *
' * Inputs: *
' * ARGUMENT 1: Source Folder where files need to be copied *
' * Argument 2: Destination Folder to copy files to *
' * Usage : c:\> cscript /nologo CopyLastModifiedFile.vbs src dst *
' *****************************************************************
Option Explicit
Dim objArgs,objFSO
Dim I ,SrcFolder, DstFolder,temp,myFile,FileName,ShortName
Set objArgs = WScript.Arguments
SrcFolder=objArgs(0) 'first arguments is source folder
DstFolder=objArgs(1) 'second argument is destination folder
temp=0
Set objFSO=CreateObject("Scripting.FileSystemObject")
For Each myFile In objFSO.GetFolder(SrcFolder).Files
If myFile.DateLastModified > temp Then 'get the lastest modified file
temp=myFile.DateLastModified
FileName=myFile
ShortName=myFile.ShortName
End If
Next
objFSO.CopyFile FileName, DstFolder&"\"&ShortName 'copy to destination
Set objFSO=Nothing
Set objArgs=Nothing
Quote from: GuruGary on September 08, 2007, 10:40:54 PM
Quote from: DeltaSlaya on September 08, 2007, 03:22:11 PM
No, and no. As you can clearly see it only gets one file because the loop is only initated once and then the code at the :done label is executed. It does not require the source directory to be where you get the files from, thats why there is a clearly labeled variable that you set, aptly named indir.

OK, sorry. I do see that it will only copy the last modified file, but I still think it will only work if the file exists in the current directory. If you change
Code: [Select]copy "%%I" "%outdir%"to
Code: [Select]copy "%indir%\%%I" "%outdir%"then I think it will work.

Oh sorry my bad I left something out.

Quote
@echo off

set indir=input directory
set outdir=output directory

for /f "usebackq delims=" %%I in (`dir /b /a:-d /o:-d "%indir%"`) do (
copy "%%~fI" "%outdir%"
goto done
)

:done
echo.
echo Done.
pause >nul
exit
Thanks to you all. I have a simple .bat that works

Xcopy C:\PROG P: /A/Y
Xcopy C:\_Labels L: /M/Y
Xcopy C:\PROG C:CNC_OFF /A/Y
Pause

I paused the batch so we can see if the machines were off. I know this requires another tap of the key. I feel more comfortable that others will CATCH a folder that isn't available. PROG and _Labels are folders on my C drive.

I see that you all are all more at ease with this than I. Thanks for the replies.
For me, it worked with...


set sourcedir=sourcedir
set targetdir=targetdir
for /f "delims=" %%I in ('dir %sourcedir% /b /a-d /od') do set targetfile=%%I
copy %sourcedir%\%targetfile% %targetdir%


Regards.Hi,

Quote
for /f "delims=" %%I in ('dir %sourcedir% /b /a-d /od') do set targetfile=%%I
copy %sourcedir%\%targetfile% %targetdir%

i dont understand this command at all, can someone explain it to me and how to use it?
thanks.


Discussion

No Comment Found