Q:

If statement trouble

Not entirely sure if the IF statement works properly.  I want it to identify if Desktop.ini exists, if it has the right attributes to be re-written and then reset the attributes. 
Does this IF statement do what I want it to?  As sometimes it works sometimes it doesn't whether I have the IF / IF not regardless.  I think I need to check whether the file exists first but I am unsure how. 
@ECHO OFF

attrib +s "%CD%"
set ICODIR=%CD%\Icon\

for %%F in ("%ICODIR%"*.ico) do set ICO=%%~nxF
echo %ICO%
echo %ICO:~0,-4%

set ICOINI=Desktop.ini
if exist %ICOINI% attrib -s -h %ICOINI%

echo [.ShellClassInfo] > %ICOINI%
echo IconResource=%ICODIR:~2%%ICO%>>%ICOINI%
echo InfoTip=%ICO:~0,-4%>>%ICOINI%

if not "%2"=="" (
    echo FolderType=%2 >> %ICOINI%
)

attrib -a +s +h %ICOINI%

Pause

The code without the if statement. 
@ECHO OFF

attrib +s "%CD%"
set ICODIR=%CD%\Icon\

for %%F in ("%ICODIR%"*.ico) do set ICO=%%~nxF
echo %ICO%
echo %ICO:~0,-4%

set ICOINI=Desktop.ini
attrib -s -h %ICOINI%

echo [.ShellClassInfo] > %ICOINI%
echo IconResource=%ICODIR:~2%%ICO%>>%ICOINI%
echo InfoTip=%ICO:~0,-4%>>%ICOINI%

attrib -a +s +h %ICOINI%

Pause

EDIT: It seems the reason it sometimes works and sometimes doesn't is more to do with Windows than the batch file.  As if I delete a file within the folder it will work, no wonder I couldn't find the fault >.<

A:

Try this to get an idea what parameters you are passing in.   Your problems could be caused by a number of things. 

You may have spaces or special characters in your input parameters. 
If you 'Run as Admin' the Current Directory will be changed and will not be what you think it is. 
Does ICODIR contain spaces? 

I modified your code below to

Add some debugging code
Change quotes around %ICODIR%*.ico
Handle %2 properly (or maybe not... not sure if you should have quotes in what you echo)

`   
@ECHO OFF
echo.1=%1
echo.2=%2
echo. CD=%CD%

attrib +s "%CD%"
set ICODIR=%CD%\Icon\

for %%F in ("%ICODIR%*.ico") do set ICO=%%~nxF
echo %ICO%
echo %ICO:~0,-4%

set ICOINI=Desktop.ini
if exist %ICOINI% attrib -s -h %ICOINI%

echo [.ShellClassInfo] > %ICOINI%
echo IconResource=%ICODIR:~2%%ICO%>>%ICOINI%
echo InfoTip=%ICO:~0,-4%>>%ICOINI%

if not "%~2"=="" (
    echo FolderType=%~2 >> %ICOINI%
)

echo. ICOINI=%ICOINI%
attrib -a +s +h %ICOINI%

Pause

