What is wrong with my If Exists command?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
leopardfist
Posts: 2
Joined: 26 Nov 2014 13:02

What is wrong with my If Exists command?

#1 Post by leopardfist » 26 Nov 2014 13:37

Code: Select all

@echo off
set /p uName=Enter User Name:
set /p server=Enter Profile Server:
set /p pcID=Enter Computer Name:
set pcID=\\%pcID%\c$\Documents and Settings\%uName%\
set uPath=\\%server%\profiles\%uName%\
set pPath=\\%server%\users\%uName%\
set old=\\%server%\profiles\%uName%.old\
If exists %old% echo YES IT EXISTS
Rem rd /s /q %old%
rem ren %uPath% %uName%.old
echo %pcID%
echo %pPath%
echo %uPath%
set /p end=Press enter key to end!
exit


If I REM the If Exists line this will run, but if I test with it, it just closes the command window and does not get past it. I have the rename line after it REM out just to make sure which one was causing the problem. I'm very new to batch files, so trying to do this by referencing online material.

What I am attempting to do here, is to check to see if a Users Profile Folder already has a .old version... and if it does to delete it (with the RD /s /q command in the IF statement) so that when I rename the normal profile folder to the .old, it works and replaces it. Would a move be better since you can use the flag to overwrite?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: What is wrong with my If Exists command?

#2 Post by foxidrive » 26 Nov 2014 13:54

Code: Select all

If exists %old% echo YES IT EXISTS


Change exists to exist and make sure that %old% contains no space or & characters etc.
It's better to use "%old%" with the quotes too, to handle spaces etc.

if exist also fails to work properly over a LAN IIRC.

leopardfist
Posts: 2
Joined: 26 Nov 2014 13:02

Re: What is wrong with my If Exists command?

#3 Post by leopardfist » 26 Nov 2014 14:03

Thanks a TON, that did it :).

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: What is wrong with my If Exists command?

#4 Post by Squashman » 26 Nov 2014 14:33

That pesky batch syntax that requires you to spell things correctly. Done that more than a few times.

Helps to read the help for the command though.

Code: Select all

H:\>if /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

  NOT               Specifies that Windows should carry out
                    the command only if the condition is false.

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.

  string1==string2  Specifies a true condition if the specified text strings
                    match.

  EXIST filename    Specifies a true condition if the specified filename
                    exists.

  command           Specifies the command to carry out if the condition is
                    met.  Command can be followed by ELSE command which
                    will execute the command after the ELSE keyword if the
                    specified condition is FALSE

Post Reply