Solved

Search multiple folders in windows

Posted on 2012-03-19
7
292 Views
Last Modified: 2012-03-20
We have our users personal drives stored on one server. Each user has a top level folder named for their username. Within this they store their files, and also their roaming profile. I would like to search within the username\profile\cookies folder to find files older than a specific date (to delete)
How can is seach just the muliple cookies folders to find these files?
0
Comment
Question by:SYPTE-IT
  • 3
  • 2
  • 2
7 Comments
 

Expert Comment

by:TwynFeyr
Comment Utility
VB script would probably be the easiest.  Kind of like this:

'------------------------------------------------------------
' Root folder
strRoot= "C:\temp\"
' The common subfolder for each directory
CommonSubFolder="\profile\Cookies"
' maximum file age in days
maxFileAge = 5

' With the current settings this will delete all files equal to or older than 5 days
' in the sub folder \profile\cookies\ for each Sub directory of c:\temp
'
' E.g.
' c:\temp\user1\profiles\cookies
' c:\temp\user2\profiles\cookies
'------------------------------------------------------------


set objFSO = createobject("Scripting.FileSystemObject")

set objFolders = objFSO.GetFolder(strRoot)

for each strFolderName in objFolders.SubFolders
      if objFSO.Folderexists(strFolderName + CommonSubFolder) then
            DeleteFiles strFolderName.path + CommonSubFolder, maxFileAge
      end if
next

sub DeleteFiles(strDirectory,maxAge)
      set objFolder = objFSO.GetFolder(strDirectory)
      for each objFile in objFolder.Files
            IF objFile.DateLastModified <= (Now - MaxAge) THEN
                  objFile.Delete
            END IF
      next      
end sub

'------------------------------------------------------------
0
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
Questions

1) Can you confirm your folder structure:

if your servername is 'Server' and,
if your driveletter is 'C:' and,
if you have say, 3 users 'Tom', 'Dick' and 'Harry' then,
is the following structure correct?

    \\Server\$C\Tom\profile\cookies\
    \\Server\$C\Dick\profile\cookies\
    \\Server\$C\Harry\profile\cookies\
    etc...

2) What's the output format of your date?
You can find it by entering the following command:

    ECHO %DATE%

3) Do you want files to be deleted:
    a) on a specific date or,
    b) when files are a specific number of days old

4) Where will you be getting your date from?
0
 
LVL 1

Author Comment

by:SYPTE-IT
Comment Utility
TwynFeyr
I can follow the structure of your code and edit to suit me, but I don't know how use it. What sort of file do I need to put the code?

paultomasi
1: structure is \\server\share\Tom\profile\cookies\
2: DD/MM/YYYY
3: b) when files are a specific number of days old
4: An admin server that any script or whatever could run on. This is ntp synched with a DC, as is the server (which is actually a VFiler) containing the files.

I'm not too worried about the to-the-second accuracy or frequency of deletion. Something that runs once a week or even once a month would be OK. I'm aware that cookies can be useful to users, but we do have some dating back to 2002!
0
Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

 
LVL 11

Accepted Solution

by:
paultomasi earned 500 total points
Comment Utility
The following batch file will delete (the specified) cookie files older than a month from when the batch file is run.

Change line 14 to the name of your 'share' location if this is not correct.
Lines 17 and 19 can be removed if you don't require a visual output of what's going on.

@echo off
for /f "tokens=1-3 delims=/" %%a in ('date /t') do (
  set day=%%a
  set /a month=%%b -1
  set year=%%c
)

if %month% lss 1 (
  set month=12
  set /a year-=1
  if %day% gtr 28 set day=28
)

pushd "\\server\share\"

for /d %%a in (*) do (
  echo Processing user: %%a
  for /f "tokens=*" %%b in ('xcopy "%%a\profile\cookies\*.*" "%temp%\" /d:%month%-%day%-%year% /c /l /y ^| find /v "File(s)"') do (
    title %%a: %%~nxb
    del /f /q "%%b"
  )
)

popd
exit /b

Open in new window

NOTE: Don't be mislead by the use of XCOPY in line 18 - it's use is purely to select files after a specific date as specified by %day%, %month% and %year% in the format m-d-y.
0
 
LVL 1

Author Comment

by:SYPTE-IT
Comment Utility
That seems to do the trick, I've used to thin out the recent document folder too.
0
 

Expert Comment

by:TwynFeyr
Comment Utility
Howdy,

Copy and paste into a plain text file, save and rename the file's extension to .vbs.

Double click to execute
Or, execute on the command prompt
Or, schedule and set it to run "wcript.exe [yourfilename].vbs" (or use cscript.exe if you fancy that one)

I know your probably going to anyway, but please remember to test thouroughly before you deploy.  :)

Cheers!
TwynFeyr

MSDN VBScript language reference
0
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now