Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checking for strings within a variable

Posted on 2008-06-12
5
Medium Priority
?
151 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

916 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