Solved

Checking for strings within a variable

Posted on 2008-06-12
5
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

615 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