Solved

check if a folder exists script.

Posted on 2013-01-15
13
377 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 53

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 53

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 53

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 53

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 53

Expert Comment

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

~bp
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

To effectively work with Diskpart on a Server Core, it is necessary to write some small batch script's, because you can't execute diskpart in a remote powershell session. To get startet, place the Diskpart batch script's into a share on your loca…
I had a question today where the user wanted to know how to delete an SSL Certificate, so I thought that I would quickly add this How to! Article for your reference. WHY WOULD YOU WANT TO DELETE A CERTIFICATE? 1. If an incorrect certificate was …
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
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…

856 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