Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

check if a folder exists script.

Posted on 2013-01-15
13
Medium Priority
?
405 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:Farhan Kazi
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 

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 59

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 59

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 59

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 59

Accepted Solution

by:
Bill Prew earned 1500 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 59

Expert Comment

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

~bp
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…

886 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