Batch script command to search directories

Hi all,

I am looking for a way to have a batch script run as the local admin or system account on my workstations.  The one catch is that the script must be able to search all user profiles to find a matching file.  When it does find this file, it will perform a reboot of the users' workstation for a specific policy to take effect.

here is what I am looking for the script to do...

@echo off
:: Find Windows XP
ver | findstr "5."

:: Uncomment this to see the 'errorlevel output' - for troubleshooting purposes
:: it happens to be '0' or zero!
:: echo Your errorlevel is:
:: echo %errorlevel%

IF %ERRORLEVEL% LEQ 0 goto xp
::if %errorlevel% 0 goto xp

:***************************************************************************************

:***************************************************************************************


:: Find Windows 7
ver | findstr "6."

:: Uncomment this to see the 'errorlevel output' - for troubleshooting purposes
:: it happens to be '0' or zero!
:: echo Your errorlevel is:
:: echo %errorlevel%

IF %ERRORLEVEL% LEQ 1 goto 7
::if %errorlevel% 1 goto 7

:xp
@echo off

if exist "c:\documents and settings\*\Application Data\timesheets\timesheet.txt" shutdown -t 1800 -r -c "Your have delinquent timesheets.  Save your work, computer will restart"
goto end

:7
@echo off

if exist "c:\users\*\Application Data\timesheets\timesheet.txt" shutdown /t 1800 /r /c "Your have delinquent timesheets.  Save your work, computer will restart"

goto end

:end

Open in new window

LVL 32
nappy_dThere are a 1000 ways to skin the technology cat.Asked:
Who is Participating?
 
MMe12Connect With a Mentor Commented:
I forgot a closing bracket at the end of line 11 of my previous post. Please try again after adding it, it worked in a test scenario I've built.
0
 
brendanmeyerCommented:
is you are using Active Directory
Setup a Group Policy

User configuration
-Windows Settings
--Logon/Logoff scripts

I believe that will run as the system user
0
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
user configuration scripts are not run as system and non admin users do not have privileges to run as system.

I need to know if my wildcard line makes sense and will search ALL user profiles with the specified path
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
MMe12Commented:
No, the wildcard in lines 32 and 38 will not work the way you want it to.

To search for a specific file within more than one directory you ycould either use a "dir /s" command or build a "for /d in (dirlist) do (..." expression.
0
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
MMe12, can you expand a bit more on the expression for me?
0
 
MMe12Commented:
Hi nappy_d,

starting at line 29 of your script try changing to this code:

:xp
@echo off
for /d %%i in ("c:\documents and settings\*") do (
    (if exist "%%i\Application Data\timesheets\timesheet.txt" shutdown -t 1800 -r -c "Your have delinquent timesheets.  Save your work, computer will restart")
)
goto end

:7
@echo off
for /d %%i in ("c:\users\*") do (
    (if exist "%%i\Application Data\timesheets\timesheet.txt" shutdown /t 1800 /r /c "Your have delinquent timesheets.  Save your work, computer will restart"
)
goto end

:end

Open in new window

0
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
The batch script does not seem to run after the goto command for goto 7 or goto XP
0
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.