Solved

check if a folder exists script.

Posted on 2013-01-15
13
370 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I was supporting a handful of Windows 2008 (non-R2) 2 node clusters with shared quorum disks. Some had SQL 2008 installed and some were just a vendor application that we supported. For the purposes of this article it doesn’t really matter which so w…
OfficeMate Freezes on login or does not load after login credentials are input.
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 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…

777 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