You are not logged in.

#1 20 Aug 2016 15:30

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Simplify change directory and verify

Hello.
I have the following script, used as temp file cleaner:

call :rc %localappdata%\Temp
call :rc "D:\temp test\"
exit /b 0


:rc
cd /d "%~1"
if /i "%cd%" neq "%~1" goto :eof
del *.* /s /q /f
goto :eof

The idea is to make universal, used path with and without spaces in it. Once the function :rc is called, I'd like to make sure the path is changed before any deletion occurs. Is there a better way to do this, other that what I've wrriten?
Thank you

Last edited by Eehixohw (20 Aug 2016 15:33)

Offline

#2 20 Aug 2016 18:01

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: Simplify change directory and verify

You may use the fact that CD command returns an ERRORLEVEL of 1 when the current directory was not changed:

cd /d "%~1"
if errorlevel 1 goto :eof

You may review further details on this point at the Table 2 of this answer.

Antonio

Offline

#3 21 Aug 2016 04:34

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Simplify change directory and verify

Aacini wrote:

You may use the fact that CD command returns an ERRORLEVEL of 1 when the current directory was not changed:

cd /d "%~1"
if errorlevel 1 goto :eof

You may review further details on this point at the Table 2 of this answer.

Antonio

Could you take it further and say

cd /d "%~1"||goto :eof

Offline

#4 21 Aug 2016 11:09

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Simplify change directory and verify

Both suggestions work great.
Thank you guys.

Offline

Board footer

Powered by