Solved

Checking for strings within a variable

Posted on 2008-06-12
5
143 Views
Last Modified: 2010-04-21
I'm working on a batch file that will perform a search and destroy on shortcuts. It works great, but I need it to be smarter. Here is the current script:

@ECHO OFF
SET TARGET=%1
SETLOCAL
FOR /R %%A IN (.) DO CALL :COMMAND "%%A"
GOTO :END
:COMMAND
SET TEMPLOC=%~1
CD%TEMPLOC:~2%
FOR /F "usebackq delims=" %%A in (`DIR /B *"%TARGET%"*.lnk 2^>NUL`) DO CALL :DELETE "%%A"
GOTO :EOF
:DELETE
SET LOCALVAR=%~1
echo Deleting %CD%\%LOCALVAR% >> \\tbcsrv903\office2007\logs\%COMPUTERNAME%\installstatus.txt
DEL "%LOCALVAR%"
GOTO :EOF
:END

I change to the c:\documents and settings\ folder and call the batch file
Example:     findndel.bat word
It then searches the documents and settings folder for all shortcuts with "word" in them and deletes them.

Here's the catch. When it finds them I want it to ignore shortcuts with, say, "2007" or "web" or "pad".

Is there any way to have it check to see if the LOCALVAR variable contains any of these strings, and if so, skip the delete command?
0
Comment
Question by:PaulRKrueger
  • 3
  • 2
5 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 21769919
Change your delete routine like this:
:DELETE

SET LOCALVAR=%~1

echo %LOCALVAR% | FIND /I "2007"

If [%errorlevel%]==[1] goto :eof

REM Repeat above two lines for each instance to exclude...

 

echo Deleting %CD%\%LOCALVAR% >> \\tbcsrv903\office2007\logs\%COMPUTERNAME%\installstatus.txt

DEL "%LOCALVAR%"

GOTO :EOF

:END

Open in new window

0
 
LVL 4

Author Comment

by:PaulRKrueger
ID: 21770120
That did the trick, although I had to change the if statement to

If not [%errorlevel%]==[1] goto :eof

because it was deleting the shortcut with the 2007 and leaving the other. Excellent solution.

One other quick question: is there a way to guarantee that this script will ALWAYS ONLY look within the documents and settings folder (and subfolders) no matter where it's called? I assume I would set the starting path somewhere in there but I'm not exactly sure where.
0
 
LVL 4

Author Comment

by:PaulRKrueger
ID: 21770398
nevermind... just changed directories as the first line of the script and that seems to be working. Thanks a bunch for the speedy reply!
0
 
LVL 4

Author Closing Comment

by:PaulRKrueger
ID: 31466569
You're a lifesaver! Thanks again.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 21771300
sorry -system reboot caused the delay :^)

Yes, you can change to your profile folder using:

CD /d %Userprofile%

This 'double'y ensures the right folder (if you're talking the current user's profile).
The /d ensures that it's changing to the appropriate drive as well as the folder...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now