[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch script command to search directories

Posted on 2012-09-20
8
Medium Priority
?
328 Views
Last Modified: 2012-09-27
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

0
Comment
Question by:nappy_d
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 38417572
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
 
LVL 32

Author Comment

by:nappy_d
ID: 38417647
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
 
LVL 4

Expert Comment

by:MMe12
ID: 38418233
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 32

Author Comment

by:nappy_d
ID: 38418463
MMe12, can you expand a bit more on the expression for me?
0
 
LVL 4

Expert Comment

by:MMe12
ID: 38420887
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
 
LVL 32

Author Comment

by:nappy_d
ID: 38437498
The batch script does not seem to run after the goto command for goto 7 or goto XP
0
 
LVL 4

Accepted Solution

by:
MMe12 earned 2000 total points
ID: 38439986
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
 
LVL 32

Author Comment

by:nappy_d
ID: 38442016
Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In a hurry?.. scroll down to "HERE's HOW TO DO IT" Section. Greetings All, I was going to post this as question/solution, but its seems more appropriate as an article considering its length.  I felt it important to illucidate all the details c…
Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

810 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