-2

I've tried using findstr, but it's not working properly. I need the script to search in C:\Documents and Settings for the shortcuts (.lnk).

Shortcut names

Accounts Payable
Approval Levels
Clinical Carestation
Downtime Registration
Emergency Department Management
Fixed Assets
General Ledger
Job Stream
Lab Inquiry
Lab Results Processor
Laboratory
Materials Management
Medical Records
Medication Administration
Operating Room Management
Order Management
Paragon Inquiry
Paragon Processor
Patient Management
Patient Supply
Patient View
Payroll
Pharmacy
QEM Extract
Radiology Management
Receivables Administrator
Reference Masters
Registration
Resource Scheduling
Security
Snomed Coding
State Reporting
Statistical Reports
Therapeutic Results Reporting
Utilization Review
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Xaero
  • 9
  • 3
  • But there are 35 shortcuts, and some that are named "copy of" or have (2) or (3) at the end. – Xaero Nov 13 '12 at 20:27
  • Please, post a list of the names you want to remove as we are not mind-readers... – Aacini Nov 13 '12 at 20:46
  • Updated original post with shortcut names – Xaero Nov 13 '12 at 21:07
  • Where are the shortcuts named "copy of" or that have (2) or (3) at the end that you said? To delete your previous list, just use: `del *.lnk`! (I asked for names YOU WANT TO REMOVE). Please, post names and indicate which ones must be deleted and which ones not! – Aacini Nov 13 '12 at 21:20
  • I don't know--that's why I need something that will search all the folders in C:\Documents and Settings. It needs to remove user created shortcuts in their user folders. It's impossible for me to find out who created a shortcut to what in an organization with 500+ client workstations. ALL of the shortcuts and their variants need to be removed. All the script needs to do is find at least the complete name of the shortcut and remove it, along with any variants. Can't this be accomplished with findstr? – Xaero Nov 13 '12 at 21:29
  • See my solution below. I was confused by topic title ("based on their names"); it should be "no matter their names"... Isn't it? – Aacini Nov 13 '12 at 21:51

3 Answers3

2

I wanted to answer your question, but then I realized that there was something that I didn't know how to do... so I asked my own question: Is it possible in a batch file to read from a pipe line by line? and I have learnt something new!

You can find all .lnk files under "C:\Documents and Settings" whith this command:

for /R "C:\Documents and Settings" %a in (*.lnk) do @echo %a

then you create a .txt files with part of the name of the shortcuts that you have to delete:

.*Accounts Payable[^\\]*
.*Approval Levels[^\\]*
.*Clinical Carestation[^\\]*
...

(this regexp excludes all directory names from the match) and you filter the output of the previous command with this:

findstr /r /x /g:filter.txt

this gives you a list of all of the files that you have to delete. Then you read the output of findstr and delete the filtered files:

for /F "delims=" %a in ('more') do @echo del "%a"

and this is the resulting command:

for /R "C:\Documents and Settings" %a in (*.lnk) do @echo %a | findstr /r /x /g:filter.txt | for /F "delims=" %a in ('more') do @echo del "%a"

just remove the last echo to actually remove the files.

Community
  • 1
  • 1
fthiella
  • 48,073
  • 15
  • 90
  • 106
  • Thanks! Your code removes the shortcuts, but not the variants. How can I make it so that it searches for shortcuts with (2), (3), etc., and "Copy of" in the name? – Xaero Nov 14 '12 at 14:40
  • This should remove every shortcut that matches the filter, if you have the row "Accounts Payable" every file that contains that string is matched, like "Copy Of Accounts Payable.lnk", "Accounts Payable (2).lnk", etc. you only may add /I parameter to findstr to ignore case... – fthiella Nov 14 '12 at 14:53
  • The problem was I had .lnk at the end of the names in the filter.txt file. I removed the .lnk from the names and ran it again, and it deleted all the shortcuts and their variants except for Order Management, Reference Masters and Registration. I double-checked that these three items were listed in the filter.txt file and they were. What gives? Also, what does ('more') do, and why does it always say "'DVD' is not recognized as an internal or external command, operable program or batch file" when I run the script? – Xaero Nov 14 '12 at 15:34
  • Update: It also deletes the Administrative Tools o_O – Xaero Nov 14 '12 at 16:40
  • it matches subdirectory names, not just file names, this is not correct... i need to fix this, just give me some time... – fthiella Nov 14 '12 at 16:54
  • OK. I really appreciate your help :) – Xaero Nov 14 '12 at 16:59
  • i think the easiest way to fix that problem is to use regex, i've just updated the post, you have to use findstr with /r /x, notice that some links like `Windows Firewall with Advanced Security` will still be deleted since they contain the word Security – fthiella Nov 14 '12 at 18:19
  • I tried using /x, so that the shortcut names must match what's written in filter.txt, but it doesn't seem to be working. I tried adding a .lnk at the end and parenthesis around the word. Ideas? – Xaero Nov 14 '12 at 19:47
  • did you change the filter.txt file also, like in my updated example? – fthiella Nov 14 '12 at 19:50
  • It works! It still deletes other shortcuts, like the Admin tools. I know you said it would, but shouldn't /x prevent this since that switch prints only lines that match exactly? – Xaero Nov 14 '12 at 21:23
  • @fthiella: Excuse me, how the filter.txt file is created? By hand? It must be created every time the Batch program run? – Aacini Nov 15 '12 at 08:28
  • @xaero i think it deletes some admin tools because the names of the link matches some strings in the filter.txt file (eg. Security), you could avoid that using more comples regex.. the file has to be created by hand but only the first time, because you have to decide what are the links that you want to delete, but the script has to find that file... – fthiella Nov 15 '12 at 09:27
  • @fthiella: But Xaero said that he don't know the names of the files to delete! Read his comments above. I also asked him if is there a list of shortcuts to delete in a file, and he didn't answer... – Aacini Nov 15 '12 at 10:06
  • @aacini i answered this because i wanted to learn how to do it, in the most logical way.... but if he doesn't know the names, or part of the names, or any other criteria to decide which files to keep and which one to delete... well you're right: there's no way to answer his question! thanks! – fthiella Nov 15 '12 at 10:26
1

The Batch file below delete ALL of the shortcuts and their variants located in all the folders in "C:\Documents and Settings" folder:

@echo off
for /R "C:\Documents and Settings" %%a in (*.lnk) do del %%a

EDIT: New solution based on new information added

Ok. Lets suppose that there is a text file with the base names of the shortcuts to delete. For example, this is part of delete.txt file:

Accounts Payable
Approval Levels
Clinical Carestation
. . . . . .
Security
Snomed Coding
. . . . . .

The Batch file below delete all variants of the base names in previous list:

@echo off
for /F "delims=" %%a in (delete.txt) do (
   for /R "C:\Documents and Settings" %%b in ("*%%a*.lnk") do echo del "%%b"
)

However, previous method have two problems:

  • The delete list must be created by hand every time the Batch program run because there is no way to know in advance which names the users will choose for their new shortcuts. If all shortcuts must be manually revised to select the names to delete (like before) the Batch program is just useless.

  • There is no way to separate a shortcut that must not be deleted from the variants that must be deleted. For example, "Security" base name allows to delete all these files: "Security.lnk", "Copy of Security.lnk", "Copy (2) of Security.lnk", "Security (2).lnk", et cetera, but it also delete "Windows Firewall with Advanced Security.lnk".

Now suppose that there is a text file with the full names of the shortcuts to preserve instead. For example, this is part of preserve.txt file:

Windows Firewall with Advanced Security

This file is created just once and must be modified only when a new permanent shortcut is created, so it is much easier to maintain than the previous delete list.

The Batch file below delete all existent shortcuts excepting the ones included in the preserve list.

@echo off
setlocal
for /F "delims=" %%a in (preserve.txt) do set preserve[%%a]=defined
for /R "C:\Documents and Settings" %%a in (*.lnk) do (
   if not defined preserve[%%~Na] echo del "%%a"
)

Antonio

EDIT: Comment for Xaero

Excuse me. This is just a simple recommendation for your future questions...

You must realize that better questions lead to better answers. Perhaps there is a point in your question that is clear for you, but you must realize that we have not the same background to the topic than you, so you should try to read your own explanations with critical eye and provide the missing information. For example, when I asked you: "Where are the shortcuts named "copy of"... that you said?" you should realized that I asked that because those names don't appear in the list, not because I wanted to know the location of such files in the disk (that is precisely the purpose of the Batch file!). You should answered this question with something like: "Previous list have the base names only, the variants are assembled from they; for example: "Copy of Accounts Payable.lnk" or "Approval Levels (2).lnk". This simple answer would provided you with a right solution in less time, that ultimately is what you were looking for. Isn't it?

Antonio

EDIT: Comment for fthiella

Excuse me, no offense is intended in the following paragraphs.

People at this site expect to get reliable and precise answers for their questions. If you answer a question with the purpose of learn about it, then it is probable that you provide wrong or imprecise information.

I suggest you to master a theme before you try to provide solutions for it to other people. If you provide wrong answers to people with no experience, it is probable that they learn and use inadequate techniques. As a matter of fact, this is one of the reasons that have spread multiple bad habits in Batch file community (like using echo. command to show empty lines, or for /F "tokens=* delims= " %%a in ('dir /b *.txt') instead of for %%a in (*.txt), among others). There is a popular phrase in my country: "A one-eyed man leading blind people"...

Antonio

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Won't that delete all shortcuts, not just the ones I mentioned? One of the reasons why I looked into using findstr was because it was a lot less code than having to list every possible shortcut variant. – Xaero Nov 13 '12 at 22:20
  • @Xaero: Excuse me? I asked you for the shortcuts you want to remove two times, and you had not answered _this point_. Your previous comment said: "ALL of the shortcuts and their variants need to be removed". So? Please, note that **you had not mentioned any files to delete** (reread your posts). How do you know which shortcuts be deleted and which ones not? What is the base name for the "Copy of" or (#) variants? Is there a list of shortcuts to delete or preserve in a file? You tried to use `findstr` to search **which string** in the names? If you not answer this point, we can not help you... – Aacini Nov 13 '12 at 23:50
0

Since there is no pattern to the shortcuts that you want to delete I suggest just creating a batch file to delete them individually, putting wildcards at the start and end, to delete any Copy of or (2) (3) ones.

del /s /q *shortcut*.lnk
Bali C
  • 30,582
  • 35
  • 123
  • 152