daat99
At the moment all I can achieve is to get the file time like:
echo %%~tfile
which can return for example:
07/04/2007 01:51 PM
I want to check if this returned date and time is older than 1 week or not (if so then I want to delete the file).
Is there a way to acomplish that in a batch file?
Thanks in advanced
daat99
I always try to help wink
Sometimes I don't know how sad
----------------------------
#2 09 Jul 2007 18:37
Simon Sheppard
Use the delolder script
https://ss64.com/nt/syntax-delolder.html
delolder.cmd 7 "C:\Log Files\somefile.txt"
----------------------------
#3 09 Jul 2007 21:16
daat99
The problem I'm having is that I want to delete the entire folder if it contains a specific file (by file name) that wasn't modified in the last 7 days.Simon Sheppard wrote:
Use the delolder script
https://ss64.com/nt/syntax-delolder.html
delolder.cmd 7 "C:\Log Files\somefile.txt"
If we'll present the entire case then it'll be like this:
Consider "Documents and Settings" folder that is filled with ~100 (probably more) new user profiles on a day to day basis (usually not the same profiles).
About 25% of those were modified in the last 7 days and due to that fact I want to leave the modified profiles alone.
As for the other profiles (with the exception of profiles that are listed in file called "DoNotDelete.txt" in a safe location which can be modified from time to time) I want to delete all the user profile folders in the "Documents and Settings" folder.
At the moment I have everything working great with the exception of the "older than 7 days" part.
That means that we delete all the profiles in the directory that aren't listed in the "DoNotDelete.txt" file regardless of when they were created.
The batch file algorithm (maybe you'll have insights on how to improve it tongue) is like this:
Current:
Run on all the folders inside the "Documents and Settings" folder and for each folder do:
1. set Ignore=false
2. Run on the lines in file "DoNotDelete.txt" and for each line do:
2.1. if line equ folder do: set Ignore=true
3. if Ignore equ true do: delete folder
What I hope it'll be after the change is like this:
Expected:
Run on all the folders inside the "Documents and Settings" folder and for each folder do:
1. set Ignore=false
2. if folder\ntuser.dat older then 7 days do: set Ignore=true
3. if ignore equ false do: Run on the lines in file "DoNotDelete.txt" and for each line do:
3.1. if line equ folder do: set Ignore=true
4. if Ignore equ true do: delete folder
Note the changes in bold.
I hope you can help me accomplish that goal.
Thank you in advanced for all your great work and huge help.
daat99
Last edited by daat99 (09 Jul 2007 21:41)
I always try to help wink
Sometimes I don't know how sad
----------------------------
#4 10 Jul 2007 14:25
bluesxman
I'd use robocopy to create a list of "ntuser.dat" files over 7 days old, then work through that list comparing it to the entries in your exclusions file and delete anything that doesn't match.
Something like this:
Code: Select all
@echo off
setlocal enabledelayedexpansion
set dummydir=DUMMY_DIR_%date:/=%_%time::=%
set tempfile=%~dpn0.tmp
set exclude=%~dp0\DoNotDelete.txt
robocopy /njs /ts /njh /fp /ndl /ns /nc /s /l /lev:2 /minage:7 /IA:RASHCNETO "%allusersprofile%\.." "%temp%\%dummydir%" ntuser.dat > "%tempfile%"
for /f "usebackq tokens=2*" %%a in ("%tempfile%") do (
set _fullpath=%%~dpb
call :get_username "!_fullpath:~0,-1!"
set skip=
for /f "usebackq tokens=*" %%x in ("%exclude%") do if /i "!_username!" EQU "%%~x" set skip=1
if not defined skip (
echo:Remove: !_fullpath!
ECHO: rmdir /s /q "!_fullpath!"
) ELSE (echo:Skip : !_fullpath!)
)
del "%tempfile%"
goto :EOF
:get_username
set _username=%~nx1
goto :EOF
And you'll need to download "robocopy", if you haven't already. The above was written expecting ROBOCOPY Version XP010 ... using it other versions may not work as expected.All Users
Default User
Administrator
NB - Leave the "ECHO:" on the "rmdir" line in until you've tested thoroughly and you're happy it's "removing" and "skipping" to your satisfaction!
Last edited by bluesxman (10 Jul 2007 15:38)
cmd | *sh | ruby | chef
----------------------------
#5 10 Jul 2007 17:02
daat99
Thank you very much bluesxman, that looks great smile
Just one question though, is it possible to achieve this goal without robocopy?
We work with ghost images quite a lot and I prefer not to tell the person that make the images that he needs to change them to include robocopy sad
I always try to help wink
Sometimes I don't know how sad
----------------------------
#6 11 Jul 2007 09:03
bluesxman
Surely they are going to have to modify their images anyway to include, at the very least, the tidy-up script above and, presumably, the scheduled task you'll set up to run it?
If you're going to add these things post build instead (or use them from a shared location or something), then I see no reason why "robocopy" can't be introduced in the same fashion.
But enough poking holes in your story. wink
To address your actual question:
I'm sure you could use a modified version of Simon's DELOLDER script to produce a list without the need for robocopy.
Another way that you might be able to achieve your ends with "standard" commands would be this:
Set up a scheduled task or AT job that, once every 7 days (or whatever), runs a script to check the "ntuser.dat" file attributes for the "Archive" flag (see the FOR and ATTRIB command syntax for two possible ways to establish this information). Any which don't have it set you'd delete as per my script above. At the end of the script you'd remove the flag from any "ntuser.dat" files that remain. Any "ntuser.dat" files which are modified in the intervening period will have the flag set "on" by the operating system and thus would not qualify for removal. This idea would not be workable if you wanted to run the clean-up every day, as the period between runs controls which profiles are in line for removal.
cmd | *sh | ruby | chef
----------------------------
#7 11 Jul 2007 19:12
Simon Sheppard
OK my first thought is that this may be a bad idea - in my experience there are a surprising no. of users who login to a machine once a month or go on maternity leave etc
deleting all those profiles can become a support nightmare.
If these are roaming profiles then you can use delprof /R (which will miss out the 'All Users' etc) but a better option is to look into all the policy settings you can set to configure caching and retention of roaming profiles (thats what we do)
----------------------------
#8 11 Jul 2007 20:28
daat99
They're running from a network share manually, no scheduled tasks and no modified image files winkbluesxman wrote:
Surely they are going to have to modify their images anyway to include, at the very least, the tidy-up script above and, presumably, the scheduled task you'll set up to run it?
That sounds like a good solution, can I simply copy robocopy.exe file to the shared folder and use it from there with your script algorithm?bluesxman wrote:
If you're going to add these things post build instead (or use them from a shared location or something), then I see no reason why "robocopy" can't be introduced in the same fashion.
bluesxman wrote:
That sounds really interesting, I'll look into it regardless if I'll use this method or the one you suggested above (robocopy in the network share).Another way that you might be able to achieve your ends with "standard" commands would be this:
Set up a scheduled task or AT job that, once every 7 days (or whatever), runs a script to check the "ntuser.dat" file attributes for the "Archive" flag (see the FOR and ATTRIB command syntax for two possible ways to establish this information). Any which don't have it set you'd delete as per my script above. At the end of the script you'd remove the flag from any "ntuser.dat" files that remain. Any "ntuser.dat" files which are modified in the intervening period will have the flag set "on" by the operating system and thus would not qualify for removal. This idea would not be workable if you wanted to run the clean-up every day, as the period between runs controls which profiles are in line for removal.
The profiles aren't roaming profiles but they should not contain any information that is important for more then 2 days.Simon Sheppard wrote:
OK my first thought is that this may be a bad idea - in my experience there are a surprising no. of users who login to a machine once a month or go on maternity leave etc
deleting all those profiles can become a support nightmare.
If these are roaming profiles then you can use delprof /R (which will miss out the 'All Users' etc) but a better option is to look into all the policy settings you can set to configure caching and retention of roaming profiles (thats what we do)
At the moment we delete all the profiles regardless when the user logged in last (even profiles that were used 5 minutes before the cleanup will be deleted).
The reason that I'm trying to achieve "time delay" for the account deletions is to let users that use the same computer on a day to day basis retain their own windows settings (wallpaper, desktop links, programs settings...) which aren't vital to the operation of the accounts but can help the users a bit.
Thank you both for all your great help, I'll try to get these things rolling next week when I get back to work.
If you have any more brilliant ideas (like those you had so far) please let me know about them wink
Thanks in advanced
daat99
I always try to help wink
Sometimes I don't know how sad
----------------------------
#9 12 Jul 2007 09:36
bluesxman
Should be fine, if it's in the same directory as the script (or somewhere on the PATH). To be safe, you could modify the beginning of the robocopy line to force it to look only in the script's directory, thus:daat99 wrote:
That sounds like a good solution, can I simply copy robocopy.exe file to the shared folder and use it from there with your script algorithm?
Code: Select all
"%~dp0\robocopy.exe"
Last edited by bluesxman (12 Jul 2007 09:37)
cmd | *sh | ruby | chef
----------------------------
#10 12 Jul 2007 11:16
daat99
I will do that, thanks a lotbluesxman wrote:
Be sure to include the double quotes just in case there's a space in the path to the network location.daat99 wrote:
That sounds like a good solution, can I simply copy robocopy.exe file to the shared folder and use it from there with your script algorithm?
Should be fine, if it's in the same directory as the script (or somewhere on the PATH). To be safe, you could modify the beginning of the robocopy line to force it to look only in the script's directory, thus:
"%~dp0\robocopy.exe"