Link to home
Start Free TrialLog in
Avatar of dustock
dustockFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

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

Avatar of dustock

ASKER

Awesome, exactly what I was looking for!  Thank you.
Welcome, glad that helped.

~bp
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.