1.

Solve : copy by date time stamp?

Answer»

Stumped and here for ASSISTANCE... Trying to figure out a way to just copy files of a specific date no matter the time stamp, and I am thinking I can do this through parsing a DIR output for files of the specific date no matter the time stamp, write this to a file, and then import line for line which ones to copy although I am stumped in making this happen in batch without resorting to a programming language.

So can this be done in batch and if so, how can I accomplish this by example to learn from?

I was thinking of using SET to set the date in the batch for it to target, and then use a FOR statement to parse through the DIR output.

Thanks for the help in advance Quote from: DaveLembke on January 16, 2014, 03:40:20 PM

I am stumped in making this happen in batch without resorting to a programming language.
Often this is a policy issue. What is your workplace policy ? Does your workplace prohibit using programming languages to do the job? And if you really want to solve this real world problem its still more appropriate to sell the idea of using a programming language (benefits vs costs) to the management. Or is this just some "pet hobbyists" task that you just thought of doing with just using batch? Programming languages does provide convenient apis for stat-ing files and getting the date of files. If you really wish to do it natively, the DIR command can display creation/modified times but you will have to spend more time parsing out the date portion. vbscript (powershell) may lighten the task for you.Look at 'xcopy'.

Or you could loop through 'dir', but that would take a while if you had more than a hand full of files.

Not tested: (will not work if you're files have spaces in the name)
Code: [Select]
:: This is your destination folder's pathway
set "copy_destination=myDestination"

:: This is the date it will look for (formatted just like in 'dir')
set "copy_date=myDate"

:: Loops through the output of 'dir' while ignoring directories until the date equals %copy_date%
:: Then it copies the 5th segment to %copy_destination%
for /f "delims= " %%A in ('dir /a:-d') do (
if "%%A"=="%copy_date%" do copy "%%E" "%copy_destination%\%%E"
)
EDIT: I posted the info below based on only SEEING a response initially of

Quote
Look at 'xcopy'

After I posted this response below I saw you added more info to above post.

Hello Lemonilla ... I looked at xcopy before posting for assistance, and the problem with it is that the only switch that is close to what I want to do that I know of is /D:m-d-y , but this switch does not just copy the files on that date that is entered but it completes the following process: Copies files changed on or after the specified date. And I just need to fetch the files for a specific date instead of all files from that date onwards.


Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

Code: [Select]C:\Users\dave>xcopy/?
Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
[/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
[/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B]
[/EXCLUDE:file1[+file2][+file3]...]

source Specifies the file(s) to copy.
destination Specifies the location and/or name of new files.
/A Copies only files with the archive attribute set,
doesn't change the attribute.
/M Copies only files with the archive attribute set,
turns off the archive attribute.
[b] /D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.[/b]
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
/P Prompts you before creating each destination file.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/V Verifies the size of each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/G Allows the copying of encrypted files to destination that does
not support encryption.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal Xcopy will reset read-only attributes.
/N Copies using the generated short names.
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.
/B Copies the Symbolic Link itself versus the target of the link.
/J Copies using unbuffered I/O. Recommended for very large files.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

C:\Users\dave>Thanks Lemonilla for showing a potential method to use. I will give that a try and report back.

I am guessing that:

Quote
:: Loops through the output of 'dir' while ignoring directories until the date equals %copy_date%

...means that it will continue to loop after the files matching the date are copied from the specific date requested until DIR is exhausted of all files which the others would be a date not equal to the date to fetch, and if that is the case then this is a perfect solution to making this happen in batch without over complicating it by using a language to interface with command shell/DOS.

I was starting to think that I was going to have to echo out the DIR and > to file, and then use C++ or Perl to then read in the DIR output file and look line for line for files of that specific date and build a list of files to copy in another file. And then process the list of files that were generated to complete the copying process until EOF.

Your method as it is appears to be the best method without over complicating it as I would have ended up doing without your help... THANKS SO MUCH !!!! The format of the last modified timestamp varies from region to region but forfiles is one way to do it.

This copies *.txt files in the current folder to "d:\target folder\" if the modified date is today (it just echoes the copy commands atm)

Put an echo before the if command to see what format the date is in for your region.

Code: [Select]@echo off
FORFILES /M *.txt /c "cmd /c if "@fdate"=="17/01/2014" echo copy @file 0x22d:\target folder\0x22" 2>nul
Code: [Select]@echo off
FORFILES /M *.txt /c "cmd /c if "@fdate"=="17/01/2014" echo copy @file [b]0x22[/b]d:\target folder\[b]0x22[/b]" 2>nul
Curious as to what 0x22 is? Is this a misinterpreted character or is 0x22 intentional? Found the answer to this looking at forfiles/? as seen below.

Quote
To include special characters in the command
line, use the hexadecimal code for the character
in 0xHH format (ex. 0x09 for tab).


Never worked with forfiles command before. Very Interesting!!!

My date format is MM/DD/YYYY so I am assuming that "@fdate"=="17/01/2014" is just set to 01/17/2014 if I wanted files last edited on today to be copied for example.


Code: [Select]C:\Users\dave>forfiles/?

FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description:
Selects a file (or set of files) and executes a
command on that file. This is helpful for batch jobs.

Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
directory (.).

/M searchmask SEARCHES files according to a searchmask.
The default searchmask is '*' .

/S Instructs forfiles to recurse into
subdirectories. Like "DIR /S".

/C command Indicates the command to execute for each file.
Command strings should be wrapped in double
quotes.

The default command is "cmd /c echo @file".

The following variables can be used in the
command string:
@file - returns the name of the file.
@fname - returns the file name without
extension.
@ext - returns only the extension of the
file.
@path - returns the full path of the file.
@relpath - returns the relative path of the
file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in
bytes.
@fdate - returns the last modified date of the
file.
@ftime - returns the last modified time of the
file.

To include special characters in the command
line, use the hexadecimal code for the character
in 0xHH format (ex. 0x09 for tab). Internal
CMD.exe commands should be preceded with
"cmd /c".

/D date Selects files with a last modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"MM/dd/yyyy" format; or selects files with a
last modified date greater than or equal to (+)
the current date plus "dd" days, or less than or
equal to (-) the current date minus "dd" days. A
valid "dd" number of days can be any number in
the range of 0 - 32768.
"+" is taken as default sign if not specified.

/? Displays this help message.

Examples:
FORFILES /?
FORFILES
FORFILES /P C:\WINDOWS /S /M DNS*.*
FORFILES /S /M *.txt /C "cmd /c type @file | more"
FORFILES /P C:\ /S /M *.bat
FORFILES /D -30 /M *.exe
/C "cmd /c echo @path 0x09 was changed 30 days ago"
FORFILES /D 01/01/2001
/C "cmd /c echo @fname is new since Jan 1st 2001"
FORFILES /D +1/16/2014 /C "cmd /c echo @fname is new today"
FORFILES /M *.exe /D +1
FORFILES /S /M *.doc /C "cmd /c echo @fsize"
FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

C:\Users\dave>Quote from: DaveLembke on January 16, 2014, 08:01:14 PM
Never worked with forfiles command before. Very Interesting!!!

My date format is MM/DD/YYYY so I am assuming that "@fdate"=="17/01/2014" is just set to 01/17/2014 if I wanted files last edited on today to be copied for example.

It's a useful utility, but a bit slow to TRADE off it's usefulness.

Put echo before the if and run it, you will see the strings echoed to the console.

The funny part is that I used 0x22 because of the quotes, but totally missed the fact that I'd used normal quotes for the string compare - and it works.
I didn't know I could embed quotes inside the cmd tail - but now that I tested further if I put quotes where 0x22 are then it doesn't work. bizarre.In reference to Lemonilla's batch:
Code: [Select]:: This is your destination folder's pathway
set "copy_destination=myDestination"

:: This is the date it will look for (formatted just like in 'dir')
set "copy_date=myDate"

:: Loops through the output of 'dir' while ignoring directories until the date equals %copy_date%
:: Then it copies the 5th segment to %copy_destination%
for /f "delims= " %%A in ('dir /a:-d') do (
if "%%A"=="%copy_date%" do copy "%%E" "%copy_destination%\%%E"
)

I edited it as:
Code: [Select]@echo off
:: This is your destination folder's pathway
set copy_destination=c:\test123

:: This is the date it will look for (formatted just like in 'dir')
set copy_date=01/01/2013

:: Loops through the output of 'dir' while ignoring directories until the date equals %copy_date%
:: Then it copies the 5th segment to %copy_destination%
for /f "delims= " %%A in ('dir /a:-d') do (if "%%A"=="%copy_date%" do copy "%%E""%copy_destination%\%%E")

And I get the following error?


Quote
C:\test321>copytest
'do' is not recognized as an internal or external command,
operable program or batch file.
'do' is not recognized as an internal or external command,
operable program or batch file.


C:\test321>

Googled this error and nothing on this problem with 'do' just other stuff following same error BTW I am using Windows XP Home SP3 for OS.


Also tried out foxidrives solution and got a SIMILAR internal/external command problem for 'forfiles' similar to 'do'problem with:

Code: [Select]FORFILES /M *.* /c "cmd /c if "@fdate"=="01/01/2013" echo copy @file 0x22C:\test123\0x22">nul



Quote
C:\test321>forfiletest3.bat

C:\test321>FORFILES /M *.* /c "cmd /c if "@fdate"=="01/01/2013" echo copy @file
0x22C:\test123\0x22" 1>nul
'FORFILES' is not recognized as an internal or external command,
operable program or batch file.

C:\test321>

Attached pic shows source of C:\test321 attempting to copy 2 files with same date as 01/01/2013 to C:\test123. Both of these batches have been run from the C:\test321 location where the files exist to be copied from.


[recovering disk space, attachment deleted by admin]Quote from: DaveLembke on January 18, 2014, 12:59:50 PM
'FORFILES' is not recognized as an internal or external command,
operable program or batch file.

Forfiles is not a native command in Windows XP - but is a free download from Microsoft.Ok cool. I will download it then. The other day when I looked at it I was on Windows 7 Pro, and today I am on Windows XP Home system, so I assume its bundled with Windows 7 Pro maybe but not XP. The "DO" error is because the IF command does not require a "DO".Removed 'do' and its not happy


Code: [Select]C:\test321>test10.bat
%Ec:\test123\%E
The filename, directory name, or volume label syntax is incorrect.
0 file(s) copied.
%Ec:\test123\%E
The filename, directory name, or volume label syntax is incorrect.
0 file(s) copied.

C:\test321>
with using this:

Quote
@echo off
:: This is your destination folder's pathway
set "copy_destination=c:\test123"

:: This is the date it will look for (formatted just like in 'dir')
set "copy_date=01/01/2013"

:: Loops through the output of 'dir' while ignoring directories until the date equals %copy_date%
:: Then it copies the 5th segment to %copy_destination%
for /f "delims= " %%A in ('dir /a:-d') do (if "%%A"=="%copy_date%" copy"%%E""%copy_destination%\%%E")


Also tried rev 11 on this with :

Quote
@echo off
:: This is your destination folder's pathway
set copy_destination=c:\test123

:: This is the date it will look for (formatted just like in 'dir')
set copy_date=01/01/2013

:: Loops through the output of 'dir' while ignoring directories until the date equals %copy_date%
:: Then it copies the 5th segment to %copy_destination%
for /f "delims= " %%A in ('dir /a:-d') do (if "%%A"=="%copy_date%" copy"%%E""%copy_destination%\%%E")

and got this error:

Code: [Select]C:\test321>test11.bat
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.

C:\test321>Missing the Tokens option with the for command.


Discussion

No Comment Found