1.

Solve : Create directorylist of the last XXX?

Answer»

Hello,

I've searched the forum first for some answers, but can't find anything that does what I want it to do.

First of all, I'm a complete newbie in DOS, so don't shoot me when I ask some stupid questions.

I wan't to write a batch file to get a directory list of the System32 folder. I'm only interested in the files created in the last 40 days. Is there anybody who can show me?

My second question (a little more difficult I think )
How can I make it possible to let the people change the number of days? I know how to do it in VBA (offering an inputbox, and working with that result ), but in DOS ,............... I totally have no IDEA.

This is the code I USE.

cd\
cd %temp%\
DIR /a:-d /o:-d > %systemdrive%\system32.txt
start %systemdrive%\system32.txt
cls
exit



greetings
Hi,

I think my question is to difficult.

So, I was thinking to do it another way. (but need some help anyway )

The code in my previous message, results in a textfile.
The most important thing to me are the first 30 lines of that textfile.

Is it possible to delete the other lines, or is it possible to write the first 30 lines to another textfile?

greetingsyou question is not difficult just that doing date/time manipulation in batch is not quite straighforward.
my solution is not batch, but done in Python

Code: [Select]import os,time
dir = os.path.join("C:\\","windows","system32") #define your directory to search

def findfiles(path,numdays):
''' Function to go into a directory to find files for the last numdays from today.'''
now = time.time() #find today's time
os.chdir(path)
print "Finding files in %s less than %d days" %(path,numdays)
for f in os.listdir(path):
if os.stat(f).st_mtime > now - numdays * 86400: #1 day is 86400 secs
if os.path.isfile(f):
print "Found file: %s" %(f)

## Main ##
userinput = int(raw_input("Enter number of days: ")) #get user input on changing num of days
findfiles(dir,userinput) #do the processing.


Hi Ghostdog,

Thanks for the reply, but ................. now it's still difficult to understand.

How do I execute this textfile in Python? I know how to handle a BAt file to excute (just doubbleclick ) , but a "Python file" ?? I totally have no idea.

greetingsYou would need a Python interpreter to run a Python SCRIPT. You mention you know how to do this in VBA. Why not use VBScript which is a subset of VBA, comes free with Windows and has the functionality to do date calculations. You can check out VBScript at The Script Center

Batch date calcs seem to be a hot topic this week. Unfortunately you need a truckload of code to handle dates. Batch code is a command language not a programming language.

8-)yes, as sidewinder says, the python code is done using any text editor, save it as a .py file, and then using the python interpreter to execute. eg after successful installation of python,
Code: [Select]
c:\python yourfile.py
Python is the most easiest and module rich programing language that i have used for my job.
Anyway, if python is difficult for you, can try out what sidewinder says, use vbscript as it comes with windows. it has date calculations too. A search on google on "vbscript date time" also gives you some hits on date calc with vbscript.

Good luck
In case you are still looking for a batch solution, it can be as easy as this:

Code: [Select]@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

cd /d "%SystemRoot%.\system32"

set /p limit=Enter number of days:

call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
if !diff! LEQ %limit% echo.%%~nxF is !diff! days old
)
PAUSE&GOTO:EOF

:: Copy functions below here - :ftime :jdate :date2jdate

You will need to get the functions [highlight]:ftime[/highlight] [highlight]:jdate[/highlight] and [highlight]:date2jdate[/highlight] from:
[highlight]http://dostips.cmdtips.com/DtTipsDateTime.php[/highlight]

Hope this helps



Discussion

No Comment Found