Solved

check if a folder exists script.

Posted on 2013-01-15
13
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 56

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 56

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
 

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 56

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 56

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 56

Expert Comment

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

~bp
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
Suggested Courses

632 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