Solved

Checking for strings within a variable

Posted on 2008-06-12
5
146 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
React or Angular? 6 80
powershell md/mkdir/New-item   -Quiet 10 99
Batch File search for Drive Letter 8 44
Convert .csv to tab delimited text 11 35
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

838 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