Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am trying to create a batch file that searches for all lnk files that contain the word "rica" in its path.
It works fine. The problem is that if the file namehas spaces, it doesn´t work.
Example: if the file "hi.lnk" has the word "rica" it works fine and it is deleted, but if the file name is "hi 2.lnk" it does not worl and takes "hi" as a folder.
Can you help me with that please?
Thanks!

This is the code I have
@echo off
for /f "delims= " %%a in ('findstr /M /I /S "rica *.lnk') do (
::echo deleting %%a
del %%a
echo Shortcut deleted
pause
):fin
Thanks!

Try this instead:
for /f "delims=," %%a in ('findstr,/M,/I,/S,"rica,*.lnk') do (
You defined a space as the delimiter so the spaces in the filenames were also processed as delimiters instead of part of the filenames.
If we use a comma as the delimiter, when a space is encountered in the filename it's just ignored and included as part of the path\filename...
-- kptech

Thanks for the response.
I tried it and the same happens :SIs that the way you say?
@echo off
for /f "delims=, " %%a in ('findstr, /M, /I, /S, "rica", *.lnk') do (
::echo Borrando %%a
del %%a
echo Shortcut borrado
pause
)
:finSorry to disturb you. Do you have any idea why the same could be happening?
Maybe I explained it wrong. What I want to look for is the word "rica" in its target path, and the problem occurs when the file name has a space.
Thanks in advance kptech!

I could fix it!
@echo off
for /f "delims=," %%a in ('findstr,/M,/I,/S,"rico",*.lnk') do (
::echo Borrando %%a
del "%%a"
echo Shortcut borrado
pause
):fin
I would like to search upwards. For example, if a shortcut containing "rico" is located in C:/ when I run it in desktop shpuld also be deleted. Is that posible?
Thanks!

Sounds like you just want to search the entire drive. To do that, just start your search at the root of the drive like this:
@echo off
c:
cd \
for /f "delims.......-- kptech

Thanks kptech!
What I want to do now is to display a message if it founds nothing. I tried with ERRORLEVEL but it always returns a 0.
How can I do this?
Thanks in advance!

This is one way...
@echo off
c:
cd\
set filesfound=0
for /f "delims=," %%a in ('findstr,/M,/I,/S,"rico",*.lnk') do (::echo Borrando %%a
set filesfound=1&del "%%a"
echo Shortcut borrado
pause
)if [%filesfound%]==[0] echo No files were found to delete!
:fin

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |