Copying last created file in a directory
-
This works:
FOR /F "delims=" %%I IN ('DIR . /B /O:-D') DO COPY "%%I" .\t.xls & GOTO :END
:ENDhowever, it takes the date of modification instead of creation, hence, if an old file in that folder got edited, it takes it! Any ideas?? Thanks.
Look into Robocopy, if your target is Vista and above. Else, there might be freeware utilities that can do what you need. Finally, you could probably code it yourself, using the FileInfo and related classes. SetCreationTime could be used to set the creation date on the copied file to the creation date of the source file.
modified on Tuesday, August 9, 2011 8:06 PM
-
This works:
FOR /F "delims=" %%I IN ('DIR . /B /O:-D') DO COPY "%%I" .\t.xls & GOTO :END
:ENDhowever, it takes the date of modification instead of creation, hence, if an old file in that folder got edited, it takes it! Any ideas?? Thanks.
Hi You've missed the timefield switch /T So I reckon you want
DIR . /B /O:-D /T:C
DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written -
Hi You've missed the timefield switch /T So I reckon you want
DIR . /B /O:-D /T:C
DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written -
Hi You've missed the timefield switch /T So I reckon you want
DIR . /B /O:-D /T:C
DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last WrittenThank you mate, you've helped a lot and you sound like an expert in DOS so I thought maybe you can assist with this.. The file to be copies has a name format of "PO 123.2011.txt" Now it is copied, is it possible to generate the next name (i.e. "PO 124.2011") probably using an expression for the math, concatenation and storing it into a temporary variable?? I'm using this for now..
FOR /F "delims=" %%I IN ('DIR . /B /O:-D /T:C') DO COPY "%%I" .\new.txt & GOTO :END
:END
START /MAX new.txtMany thanks mate!
-
Thank you mate, you've helped a lot and you sound like an expert in DOS so I thought maybe you can assist with this.. The file to be copies has a name format of "PO 123.2011.txt" Now it is copied, is it possible to generate the next name (i.e. "PO 124.2011") probably using an expression for the math, concatenation and storing it into a temporary variable?? I'm using this for now..
FOR /F "delims=" %%I IN ('DIR . /B /O:-D /T:C') DO COPY "%%I" .\new.txt & GOTO :END
:END
START /MAX new.txtMany thanks mate!
Yes, I'd say it's possible to generate the next filename using batch commands but probably not advisable as it is going to be difficult to guarantee any kind of robustness. Here's something that will do it but you'll find out that it's a bit of a train wreck with anything other than the expected input format.
@echo off
REM generate next PO NNN.2011.txt where NNN is a numberSET FILENAME=PO 156.2011.txt
REM SET FILENAME=PO ABC.2011.txt
ECHO Initial filename %FILENAME%REM get 2nd token using '.' and ' ' delimiters
for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%aREM what did we get
ECHO NNN %NNN%REM Increment the value
SET /a NNN=%NNN% + 1
ECHO NNN+1 = %NNN%SET FILENAME=PO %NNN%.2011.txt
ECHO New filename %FILENAME%
PAUSEWe can do a little bit better by attempting to check if the extracted token is actually numeric and then bailing out if something really bad has happened.
@echo off
REM generate next PO NNN.2011.txt where NNN is a numberSET FILENAME=PO 156.2011.txt
REM SET FILENAME=PO ABC.2011.txt
ECHO Initial filename %FILENAME%REM get 2nd token using '.' and ' ' delimiters
REM Store token as both number and text
for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%a & SET TEXT=%%aREM what did we get
ECHO NNN %NNN% TEXT %TEXT%REM Should be the same if the token is a decimal number
IF %NNN%==%TEXT% (
ECHO Yippee, got a numberREM Increment the value
SET /a NNN=%NNN% + 1ECHO NNN+1 = %NNN%
SET FILENAME=PO %NNN%.2011.txtECHO New filename %FILENAME%
) ELSE (
ECHO Darn it, got a non numeric token
)PAUSE
Have fun! Alan.
-
Yes, I'd say it's possible to generate the next filename using batch commands but probably not advisable as it is going to be difficult to guarantee any kind of robustness. Here's something that will do it but you'll find out that it's a bit of a train wreck with anything other than the expected input format.
@echo off
REM generate next PO NNN.2011.txt where NNN is a numberSET FILENAME=PO 156.2011.txt
REM SET FILENAME=PO ABC.2011.txt
ECHO Initial filename %FILENAME%REM get 2nd token using '.' and ' ' delimiters
for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%aREM what did we get
ECHO NNN %NNN%REM Increment the value
SET /a NNN=%NNN% + 1
ECHO NNN+1 = %NNN%SET FILENAME=PO %NNN%.2011.txt
ECHO New filename %FILENAME%
PAUSEWe can do a little bit better by attempting to check if the extracted token is actually numeric and then bailing out if something really bad has happened.
@echo off
REM generate next PO NNN.2011.txt where NNN is a numberSET FILENAME=PO 156.2011.txt
REM SET FILENAME=PO ABC.2011.txt
ECHO Initial filename %FILENAME%REM get 2nd token using '.' and ' ' delimiters
REM Store token as both number and text
for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%a & SET TEXT=%%aREM what did we get
ECHO NNN %NNN% TEXT %TEXT%REM Should be the same if the token is a decimal number
IF %NNN%==%TEXT% (
ECHO Yippee, got a numberREM Increment the value
SET /a NNN=%NNN% + 1ECHO NNN+1 = %NNN%
SET FILENAME=PO %NNN%.2011.txtECHO New filename %FILENAME%
) ELSE (
ECHO Darn it, got a non numeric token
)PAUSE
Have fun! Alan.