Jump to content

Listing all files from a date interval “Batch file”


Recommended Posts

I need to do a batch file wich order all files by a date interval, example:

 

@echo off echo Input the date(dd/mm/yyyy):
set /p compDate=

 

::After that i will compare from the actual day (%date%), example:

set interval = %compDate% - %date%... Something like that

::After that i need to list all files from a specific directory, example:

 

echo Input the directory:
set /p directory=
SET Exit= %UserProfile%\Desktop\test.txt

 

::After that i might i need the dir /tc to get the creation date, example:

 

pushd "%directory%"

dir /s /tc /a-d > %Exit%

 

::After that, i dont know how to get only the lines wich the date interval are, example:

 

Today is 19/08/2014, but i want to search all files created from day 10/07/2014. So i have to copy all lines which have the date 10/07/2014, 11/07/2014, 12/07/2014 and so on until stop on today created files.

I tried with findstr but i can't set the date interval, just a specific date to search in the .txt created.

Somebody know how to do that ?

Edited by fudeboy
Link to comment
Share on other sites


Well, if you use "ISO" date format it might be easier.

 

Something *like* this:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=1,2,3,* delims=/ " %%A IN ('DIR /tc /OD C:\batches\') DO (IF %%B leq 12 IF %%C%%B%%A lss 20110322 ECHO %%A/%%B/%%C %%D)

In the above I am using my local settings, where dates are "dd/mm/yyyy" you will need to change order of variables and/or separators to suit your local settings.

 

jaclaz

Link to comment
Share on other sites

Sometimes it's more fun to use powershell!

 

InBTween.ps1

$ScriptPath = Split-Path $MyInvocation.MyCommand.Path$ScriptName = $MyInvocation.MyCommand.NamePush-Location $ScriptPath$ToDate = [DateTime]::TodayWrite-Host 'Today is' $ToDate.ToShortDateString()$FromDate = Read-Host 'Enter a Search From Date'If (($FromDate -as [DateTime]) -ne $null) {    $FromDate = [DateTime]::Parse($FromDate)    GCI . -exclude $ScriptName -rec | Sort CreationTime | Where {        $_.CreationTime -ge $FromDate -and $_.CreationTime -le $ToDate    } | % {$_.CreationTime.ToShortDateString() + "`t" + $_.FullName    } | Tee results.txt} Else {    'You did not enter a valid date!'}Write-Host "Press any key to exit . . . " -ForegroundColor Yellow$X = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")Pop-Location

This script is designed to search from the root of its own directory tree and the output will be written to a new file in that directory named results.txt.

 

If the script doesn't run, you may need to type the following into a powershell window first:

Set-ExecutionPolicy RemoteSigned
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...