Solved

check if a folder exists script.

Posted on 2013-01-15
13
349 Views
Last Modified: 2013-02-01
I have a big size P:\USERS folder in my WIN2K8 server which has a lot of subfolders.  

I need to write a script to check each subfolder and report the name of the subfolder if the config folder is missing inside.  Hence, there is a C:\USERS\USERX\CONFIG folder for each user.  

Please advise.

Thanks.
0
Comment
Question by:nav2567
13 Comments
 
LVL 9

Expert Comment

by:David Carr
ID: 38779550
What language do you want the script in?
0
 
LVL 26

Expert Comment

by:farhankazi
ID: 38779627
Try this batch script if this works for you.
@ECHO OFF
IF EXIST "C:\NoConfigUsers.txt" DEL "C:\NoConfigUsers.txt"
FOR /F "delims=" %%F IN ('DIR /AD /B C:\Users') DO (
	ECHO Checking %%F
	IF NOT EXIST "C:\Users\%%F\Config" ECHO %%F>>C:\NoConfigUsers.txt)

Start "" Notepad C:\NoConfigUsers.txt

Open in new window

0
 

Author Comment

by:nav2567
ID: 38780479
Thanks a lot ; )

Under each user subfolder, there is a file called "devicetree.txt".  We want to export the subfolder names which has this file's last modified date more than a year.  

Would modify your script?

Thanks again.
0
 

Author Comment

by:nav2567
ID: 38782310
Hi,

I tried to use the following right before the "if not exist" command", but it is still having error.

forfiles /m devicetree.txt /c "cmd /c echo %%F>>noconfigusers.txt" /D -365

Would you advise again?

Thanks.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38782358
So you want a report that shows both conditions? (1) If the CONFIG folder is missing, and then also (2) if the devicetree.txt file is older than a year?

Also, if you go to a DOS prompt and enter the following command, what does it display?

ECHO %DATE%

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38782405
With regard to the FORFILES approach, I think your line would need to be:

forfiles /p "c:\users\%%~F" /m devicetree.txt  /d -365 /c "cmd /c echo @file>>noconfigusers.txt"

so the whole thing might look like:

@echo off
setlocal

set BaseDir=C:\Users
set CheckFolder=Config
set CheckFile=devicetree.txt
set ReportFile=%TEMP%\report.txt

(
for /f "delims=" %%F in ('dir /ad /b "%BaseDir%"') do (
	if not exist "%BaseDir%\%%F\%CheckFolder%\" echo Missing folder: [%BaseDir%\%%~F\%CheckFolder%]
    forfiles /p "%BaseDir%\%%~F" /m "%CheckFile%"  /d -365 /c "cmd /c echo Missing file: [@file]"
) > "%ReportFile%"

start notepad "%ReportFile%"

Open in new window

~bp
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.

 

Author Comment

by:nav2567
ID: 38784919
It is getting closed......

I have

for /f "delims=" %%F in ('dir /ad /b "%BaseDir%"') do (
forfiles /p "%BaseDir%\%%~F" /m "%CheckFile%"  /d -365
) >> "%ReportFile%"

all I see is a list of "devicetree.txt" in the %reportfile%.   Can the script displays only the user folders (%%F) that contains the devicetree.txt older than 365 days only??
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38785351
Give this a try, I did a bit of testing here.  It only displays the full path to the user folder when an "error" is logged.  We could easily make this just the user name rather than the full path if that was useful?

@echo off
setlocal

set BaseDir=C:\Users
set CheckFolder=Config
set CheckFile=devicetree.txt
set ReportFile=%TEMP%\report.txt

(
  for /f "delims=" %%F in ('dir /ad /b "%BaseDir%"') do (
	if not exist "%BaseDir%\%%F\%CheckFolder%\" echo Missing config folder in: [%BaseDir%\%%~F]
    if exist "%BaseDir%\%%~F\%CheckFile%" (
      forfiles /p "%BaseDir%\%%~F" /m "%CheckFile%"  /d -365 /c "cmd /c echo Old device file in: [%BaseDir%\%%~F]" 2>NUL
    ) else (
      echo Missing device file in: [%BaseDir%\%%~F]"
    )
  )
) > "%ReportFile%"

start notepad "%ReportFile%"

Open in new window

~bp
0
 

Author Comment

by:nav2567
ID: 38785566
Thank you.

Would you change the script to export(echo) ONLY the name of the subfolders in USERS that contains the old devicetree.txt file to the %reportfile%?
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38785593
Can you be more specific just so I get it right.  I created a small set of test folders, and ran the script.  Below is the files and folders in my test, and the resulting output.  Please adjust the output to be what you actually want and report.

C:\ee\EE27996314\Users
C:\ee\EE27996314\Users\User-1
C:\ee\EE27996314\Users\User-2
C:\ee\EE27996314\Users\User-2\Config
C:\ee\EE27996314\Users\User-2\devicetree.txt         (date=01/16/2013 07:39 PM)
C:\ee\EE27996314\Users\User-3
C:\ee\EE27996314\Users\User-3\Config
C:\ee\EE27996314\Users\User-3\devicetree.txt         (date=01/16/2010 09:41 AM)
C:\ee\EE27996314\Users\User-4
C:\ee\EE27996314\Users\User-4\devicetree.txt         (date=01/16/2011 09:41 AM)

Open in new window

Missing config folder in: [C:\EE\EE27996314\Users\User-1]
Missing device file in: [C:\EE\EE27996314\Users\User-1]"
Old device file in: [C:\EE\EE27996314\Users\User-3]
Missing config folder in: [C:\EE\EE27996314\Users\User-4]
Old device file in: [C:\EE\EE27996314\Users\User-4]

Open in new window

~bp
0
 

Author Comment

by:nav2567
ID: 38785657
I would like my %reportfile% only contains the following:

User-3
User-4

as those folders contain devicetree.txt file older than 365 days.
0
 

Author Comment

by:nav2567
ID: 38786739
Bill, I think I can use your output as it is detail.  

I will try this out this morning and let you know.

Thanks again for all you have done and sorry for the trouble.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38786856
Okay, let me know.

~bp
0

Featured Post

The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

Join & Write a Comment

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…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

746 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

12 Experts available now in Live!

Get 1:1 Help Now