6

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
  • 63
  • 1
  • 6

2 Answers2

3

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
  • 2,748
  • 1
  • 24
  • 32
  • 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 Aug 20 '18 at 16:11
  • 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 Aug 20 '18 at 16:17
  • 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 Aug 20 '18 at 16:18
  • 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 Aug 21 '18 at 07:01
  • 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 Aug 21 '18 at 07:08
  • 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 Aug 21 '18 at 07:10
  • 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 Aug 21 '18 at 10:24
  • right, I forgot to add that. I added the quotes and indeed the program works now! Thank you for all the help :) – Mantis Tsai Aug 21 '18 at 13:32
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
  • 7,149
  • 11
  • 42
  • 72
  • 1
    -1, this does not answer the question. – LPChip Aug 20 '18 at 15:39
  • 1
    This 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 Aug 20 '18 at 15:47
  • 2
    This 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 Aug 20 '18 at 16:12