Solved

Checking for strings within a variable

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

9 Experts available now in Live!

Get 1:1 Help Now