0

In a batch script, I need to redirect the script flow according to whether one variable matches the beginning of another. Particularly, I want to do something like this:

IF %CD% BEGINSWITH %USERPROFILE% (Echo You can do stuff here!) ELSE (Echo You don't belong here!)

I've had a hard time finding anything solving this problem online, or at least anything that makes sense to me. If this is doable, please demonstrate how and why the workable solution functions.

2 Answers 2

1

The Iszi answer will often appear to work, but it can give the wrong result under many circumstances.

  • The FINDSTR search could fail to match properly if there is a difference in case. Solved by using the /I option.

  • FINDSTR will treat search string as two or more search strings if the profile name includes a space. Better to use the /C:"searchString" option.

  • FINDSTR will do a regex search if %USERPROFILE% includes a regex meta-character like .. This can be solved by forcing a literal search using either the /L option, or the /C:"searchString" option.

  • Backslashes can be a bit tricky with FINDSTR, since it is used by FINDSTR as an escape character. Safer to escape all backslashes as \\.

  • The ECHO command can fail if the current directory name contains a special character like &. The CD command will safely print the current directory without having to worry about special characters.

Also, since FINDSTR is being used as a test, and there is no interest in seeing the matching line, then the output should be redirected to NUL.

And finally, the solution can be simplified by replacing the IF statement with the conditional && and || operators.

The following code should always work:

cd|findstr /bic:"%userprofile:\=\\%" >nul&&echo You can do stuff here!||echo You don't belong here!

Update - Example of varying drive letter case

Here is a Windows 7 cmd.exe session that demonstrates how the case of the drive letter reported by CD (or %CD%) can vary:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>set userprofile
USERPROFILE=C:\Users\Dave

C:\>cd c:\users\Dave

c:\Users\Dave>cd
c:\Users\Dave

c:\Users\Dave>echo CD=%CD%
CD=c:\Users\Dave

c:\Users\Dave>cd..

c:\Users>cd C:\users\Dave

C:\Users\Dave>cd
C:\Users\Dave

C:\Users\Dave>echo CD=%CD%
CD=C:\Users\Dave

C:\Users\Dave>
5
  • For this particular script, the /I option is not needed. Since we're matching command output and/or environment variables, without user input, there is effectively zero chance of a case mis-match. Thanks for the clarification, though.
    – Iszi
    Commented Dec 17, 2013 at 16:53
  • @Iszi - Actually, you do need the /I switch because CD may report the drive letter in lower or upper case, depending on how the current directory was specified. Also, the %USERPROFILE% value most likely matches the case of the actual folder names, but it does not have to match the case.
    – dbenham
    Commented Dec 17, 2013 at 17:44
  • I'm confused. No matter how I CD to a folder, if I subsequently run CD without any arguments, it returns the path in its proper (as stored in the file system) case. %USERPROFILE% also returns the user's profile path in its proper case. Ergo, if I CD /D c:\users\me then the output of my next CD (with no args) will be C:\Users\Mewhich will exactly match %USERPROFILE% - case and all - if I'm running commands as "Me".
    – Iszi
    Commented Dec 17, 2013 at 19:50
  • @Iszi - See the updated answer
    – dbenham
    Commented Dec 17, 2013 at 20:11
  • Interesting. I'd never observed, or at least never noticed, that before. Again, for my uses (users will just be double-clicking the script from an Explorer window) the point is still moot - but this is very good information to have. Thanks!
    – Iszi
    Commented Dec 17, 2013 at 20:17
0

I ended up figuring it out on my own. I'm not quite sure why I haven't noticed this solution anywhere else.

ECHO %CD%|FINDSTR /B "%USERPROFILE%"
IF %ERRORLEVEL% EQU 0 (Echo You can do stuff here!) ELSE (Echo You don't belong here!)

Normally, FINDSTR searches for strings in a given text file. However, it also can accept input from the pipeline instead. Here, we use ECHO to put the current directory (%CD%) into the pipeline and then tell FINDSTR to look for the user's profile folder ("%USERPROFILE%" - quotes mandatory) at the beginning (/B) of the piped data.

Since FINDSTR returns an ERRORLEVEL, we can check this with a later IF statement and then proceed accordingly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .