Batch file help

I am creating a batch file to loop through the URL links on a users desktop and delete specific ones.  Everything is working fine except for the if conditions in my loop.  I thought maybe I had to add an escape character for the = sign for strOldurl and strNewurl but even putting in ^=PRD didn't seem to help.  

What I want is to write the path to strOldPath and strNewPath so that I can delete them.  When I debug by adding echo statements I can see that strOldPath and strNewPath have no value
 
rem @echo off
SETLOCAL
set strOldUrl = "https://url.com/logindisp?tenant=PRD"
set strNewUrl = "https://url.com/logindisp?tenant=TST"
set strOldPath = ""
set strNewPath = ""
set url = ""
FOR %%f in (dir "C:\Users\Public\Desktop\*.url") DO (
	call:findurl %%f	
	if url == strOldUrl (
		strOldPath = %%f
	)	
	if url == strNewUrl (
		strNewPath = %%f
	)
)

rem Clean up shortcuts
del strOldPath 
del strNewPath
copy "C:\Temp\NEW-Site.url" "C:\Users\Public\Desktop" /Y

:findurl inputfile
set url=
for /f "tokens=2 delims==" %%i in ('findstr URL %1') do set url=%%i

Open in new window

LVL 9
dustockAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
See if this gets you any closer, noticed a few problems and tried to address them.  When doing the SET statements, there can't be any spaces before or after the equals sign.  Also, stay away from including quotes around the variable strings, DOS doesn't strip those out the way you think when doing the SET, so they end up part of the variable.  Also, you weren't using %variable% syntax when trying to get the value of a variable.  Missing EXIT statements at the end of main code, and at the end of subroutine.

@echo off
SETLOCAL EnableDelayedExpansion

set strOldUrl=https://url.com/logindisp?tenant=PRD
set strNewUrl=https://url.com/logindisp?tenant=TST
set strOldPath=
set strNewPath=
set url=

FOR %%f in (dir "C:\Users\Public\Desktop\*.url") DO (
	call :findurl "%%~f"	
	if "!url!" == "%strOldUrl%" (
		set strOldPath=%%~f
	)	
	if "!url!" == "%strNewUrl%" (
		set strNewPath=%%~f
	)
)

rem Clean up shortcuts
del "%strOldPath%"
del "%strNewPath%"
copy "C:\Temp\NEW-Site.url" "C:\Users\Public\Desktop" /Y

rem Exit main script
exit /b

:findurl inputfile
    set url=
    for /f "tokens=2 delims==" %%i in ('findstr "URL" "%~1"') do set url=%%i
    exit /b

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oBdACommented:
Since all you seem to want to do is delete URLs containing some matches, this should work; it's in test mode and will only echo out the "del" and "copy" commands it would normally run. Remove the uppercase ECHOs in lines 9 and 16 to run it for real.
@echo off
setlocal
set MatchUrl[1]=https://url.com/logindisp?tenant=PRD
set MatchUrl[2]=https://url.com/logindisp?tenant=TST
for %%a in ("%Public%\Desktop\*.url") do (
	echo Processing '%%~nxa' ...
	call :TestUrl "%%a"
)
ECHO copy "C:\Temp\NEW-Site.url" "%Public%\Desktop" /Y
goto :eof
:TestUrl
for /f "tokens=1* delims==" %%t in ('set MatchUrl[') do (
	find.exe /i "%%~u" "%%a" >NUL
	if not errorlevel 1 (
		echo ... found '%%~u', will delete.
		ECHO del "%~1"
		goto :eof
	)
)
echo ... no matching URL found.
goto :eof

Open in new window

dustockAuthor Commented:
Awesome, exactly what I was looking for!  Thank you.
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that helped.

~bp
oBdACommented:
Note that this script has two major errors:
1. It will delete a maximum of one URL file with strOldUrl in it, and a maximum of one URL file with strNewUrl in it. If two or more of either type are present, the first ones  found will be left.
2. If not both strOldUrl and strNewUrl are found in URL files, the script will try to delete everything in the current folder, because there will be a 'del ""' statement.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.