1.

Solve : Delete text files where accessed date is different the created?

Answer»

How to read created, last accessed, and last written DATE/time stamps in a batch file.

Code: [Select]@echo off
setlocal enabledelayedexpansion

set mask=*.*

for /f "delims=" %%F in ('dir /b /a-d %mask%' ) do (

set filename=%%F

for /f "tokens=1-2 delims= " %%A in ('dir /tc !filename! ^| find "!filename!"') do set cstring=%%A %%B
for /f "tokens=1-2 delims= " %%A in ('dir /ta !filename! ^| find "!filename!"') do set astring=%%A %%B
for /f "tokens=1-2 delims= " %%A in ('dir /tw !filename! ^| find "!filename!"') do set wstring=%%A %%B

if "!cstring!" EQU "!wstring!" (set wmatch=YES) else (set wmatch=NO)
if "!cstring!" EQU "!astring!" (set amatch=YES) else (set amatch=NO)

echo File name : !filename!
echo Creation date stamp : !cstring!
echo Last accessed stamp : !astring!
echo Last written stamp : !wstring!
echo Last ACCESS = creation : !amatch!
echo Last written = creation : !wmatch!

echo.

)



good batch effort, but sad to say, it would be inefficient when run for directory with many files. imagine for 1000 files found, it makes calls to "dir" and "find" 1000*3 =3000 times (or is it??) ... And how would you eliminate the filesystem accesses?
Quote from: Dias de VERANO on May 02, 2009, 07:28:04 AM

And how would you eliminate the filesystem accesses?

get all the information you need with only 1 loop. you can take the vbscript i posted as reference. it only loop once and at the same time, you can get creation date, modified date and access date in the same loop. Too bad native batch do not come with tools equivalent to that of , eg stat in *nix. (unless of course, one can find such command line tools for batch..resource kit maybe??) stat.exe is in the GNU Core Utils

Code: [Select]S:\>stat test1.txt
File: `test1.txt'
Size: 70 Blocks: 8 IO Block: 4096 regular file
Device: 2c51aa7fh/743549567d Inode: 2251799813704926 Links: 1
Access: (0666/-rw-rw-rw-) Uid: ( 0/ Mike) Gid: ( 0/ UNKNOWN)
Access: 2009-05-02 15:41:27.942125000 +0100
Modify: 2009-05-02 12:35:08.723375000 +0100
Change: 2009-05-02 12:25:10.348375000 +0100
Code: [Select]S:\>stat --help
Usage: stat [OPTION] FILE...
Display file or file system status.

-f, --file-system display file system status instead of file status
-c --format=FORMAT use the specified FORMAT instead of the default
-L, --dereference follow links
-t, --terse print the information in terse FORM
--help display this help and exit
--version output version information and exit

The valid format sequences for files (WITHOUT --file-system):

%A Access rights in human readable form
%a Access rights in octal
%B The size in bytes of each block reported by `%b'
%b Number of blocks allocated (see %B)
%D Device number in hex
%d Device number in decimal
%F File type
%f Raw mode in hex
%G Group name of owner
%g Group ID of owner
%h Number of hard links
%i Inode number
%N Quoted File name with dereference if symbolic link
%n File name
%o IO block size
%s Total size, in bytes
%T Minor device type in hex
%t Major device type in hex
%U User name of owner
%u User ID of owner
%X Time of last access as seconds since Epoch
%x Time of last access
%Y Time of last modification as seconds since Epoch
%y Time of last modification
%Z Time of last change as seconds since Epoch
%z Time of last change

Valid format sequences for file systems:

%a Free blocks available to non-superuser
%b Total data blocks in file system
%c Total file nodes in file system
%d Free file nodes in file system
%f Free blocks in file system
%i File System id in hex
%l Maximum length of filenames
%n File name
%s Optimal transfer block size
%T Type in human readable form
%t Type in hex

Report bugs to <[emailprotected]>.

Of course, as we have seen, the "last access" stamp in NTFS is pretty useless, and is disabled on many systems.


Discussion

No Comment Found