How to check if the current batch file is in a specific folder?

4

Hi I'm fairly new to batch files, so sorry if this question seems stupid but I really need some help.

So I need my batch script to check if it is in a specified folder, say %userprofile%\Desktop for example. I don't want it to care about its own filename. I have absolutely no idea where to start. I know how to get the current path/filename etc but that's about all I know. How do I make the program compare its current path with the path that I want to see if it's in? Or is there another more efficient method?

What I want to achieve is like this:

if this file is in Desktop folder(
    echo It's here
)else(
    echo It's not here
)

Any help is appreciated, thanks everyone

Mantis Tsai

Posted 2018-08-20T14:53:50.067

Reputation: 43

Did you check

echo %cd%
 – JavaSheriff  – 2018-08-20T14:57:58.753

1@user648026 %cd% will not show the path of the batch file, but only the current directory. The current path is something like %~p0 but I forgot the exact syntax. – LPChip – 2018-08-20T15:01:45.717

Possible duplicate of Get current folder name by a DOS command?

– LPChip – 2018-08-20T15:05:06.800

My main problem was with the comparing of the paths, but thanks guys – Mantis Tsai – 2018-08-20T15:48:39.893

Answers

2

No need to apologize as the question you raised is more tricky than you probably expect.

Solution

This is how you can test that your batch file is located in a specific folder, in your case in Desktop folder:

@echo off

:: Normalize this batch script's path
set BATCH_PATH=%~dp0
set BATCH_PATH=%BATCH_PATH:~0,-1%

:: Test for equality
if "%BATCH_PATH%"=="%USERPROFILE%\Desktop" (
    echo This file is on Desktop.
) else (
    echo This file is not on Desktop.
)

Things to note

  • alternative way to normalize path (and my favorite) is:

    pushd "%~dp0"
    set BATCH_PATH=%CD%
    popd
    
    • in any case, you probably want to remove any trailing \, which is one of the things the normalization does for you
  • use %~dp0 variable instead of %cd%, because the latter semantics is the folder your scripts operates on while the first is the folder your script is located in (which is what you asked for)

    • %0 gives you your batch script location
    • by adding dp (i.e. %dp0) you ask for disk and path which omits the filename and extension (which is something like example.bat and it would be burden in next step - equality check)
    • by adding ~ you ask to remove any opening or closing " if necessary (i.e. the path contained space)
  • paths are wrapped in " to prevent errors due to possible space in the path string


Not as simple as one would thought, right?

Vlastimil Ovčáčík

Posted 2018-08-20T14:53:50.067

Reputation: 1 835

Indeed it's not, thanks for the answer and clarification on %dp / %cd%.I realized something while playing around though, it's that when I replace %USERPROFILE%\Desktop with C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp (common startup folder) it seems to execute the code no matter what. Am I doing something wrong? – Mantis Tsai – 2018-08-20T16:11:16.883

You are welcome. The script works for me correctly if I test for the StartUp folder. I would need more details - especially the code, what it does and what you expect it to do. – Vlastimil Ovčáčík – 2018-08-20T16:17:34.917

Btw there are two startup folders now a) C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp and b) C:\Users\Admin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. – Vlastimil Ovčáčík – 2018-08-20T16:18:26.213

I used the code from the answer below (I don't really see a difference other than that his answer didn't set the path as a variable). It works on all folders I've tested other than either startup folder. – Mantis Tsai – 2018-08-21T07:01:50.087

My code:

@echo off IF %~dp0 == %USERPROFILE%\Desktop\ ( ECHO It's here ) ELSE ( ECHO It's not here ) IF %~dp0 == %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\ ( ECHO It's here ) ELSE ( ECHO It's not here ) IF %~dp0 == C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\ ( ECHO It's here ) ELSE ( ECHO It's not here ) pause – Mantis Tsai – 2018-08-21T07:08:04.923

It returns It's here It's here It's not here It's here It's not here when I put it on the Desktop – Mantis Tsai – 2018-08-21T07:10:48.123

I see and I am getting the very same output as you. The reason for it is that the equality test in last two if statements are broken and both if and else block gets executed. The first if is fine. The reason for it is that the path to Startup folder contains space and you/Art didn't put the path in double quotes as I did. – Vlastimil Ovčáčík – 2018-08-21T10:24:02.107

right, I forgot to add that. I added the quotes and indeed the program works now! Thank you for all the help :) – Mantis Tsai – 2018-08-21T13:32:37.110

0

Minimal working example using %cd%:

IF %cd%==%userprofile%\Desktop (
    ECHO It's here
) ELSE (
    ECHO It's not here
)

Using intermediate variable:

FOR /F "tokens=*" %%a in ('cd') do SET CURRENT_DIR=%%a
IF %CURRENT_DIR%==%userprofile%\Desktop (
    ECHO It's here
) ELSE (
    ECHO It's not here
)

Since you said you have no idea where to start, here is a good reading material: https://en.wikibooks.org/wiki/Windows_Batch_Scripting

Art Gertner

Posted 2018-08-20T14:53:50.067

Reputation: 6 417

1-1, this does not answer the question. – LPChip – 2018-08-20T15:39:59.147

1This did solve my problem (for now), and thanks for the link! I'll be sure to read up on it. But some sources say there's a difference between %cd% and %~dp0, with the former presenting a different path sometimes? I'm somewhat confused – Mantis Tsai – 2018-08-20T15:47:58.320

2This answer was written at times that it was not clear if Mantis is asking for %cd% or %~dp0 solution. The question title asked for script's filepath, but the question body cited "current directory" twice and it was tagged with "working-directory". Hence the confusion. – Vlastimil Ovčáčík – 2018-08-20T16:12:56.850