0

My requirement is to check if the file exists in a folder only when case matches the search.

I have searched the internet, also the basic command prompt help guide. I have no accurate answer. However, in the StackOverflow post: REF-Post the solution is provided by sending FileName as a variable to batch file. However, I would like to search the file by not using any variable sent to the Batch file.

@echo off 
dir /b /a-d "%~1"|find "%~1" >nul
if %errorlevel% == 0 (echo found) else (echo fail)

This code takes %~1 value and I want to substitute %~1 with a path directly! Please help with the solution

For example: a file named testfile.txt exists in folder C:\Files\ Logic should be something like below:

IF EXIST C:\Files\TESTFILE.txt (
  echo file of case exists
) else (
  echo file of this case does not exist
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • I'd be interested to know the reason for this, given that you cannot have `string.ext` and `sTrInG.ext` in the same location. What would make more sense is to ensure that any created file has a specific case. – Compo Oct 10 '19 at 11:42
  • Consider the situation when there is only one file named: TESTFILE.txt Then the code should recognize that filename is not same as testfile.txt – ajaygandhari Oct 10 '19 at 11:43
  • `if exist testfile.txt` would find it, as would `if exist TeStFiLe.TxT`! because as I've already stated, Windows does not allow `string.ext` and `sTrInG.ext` in the same directory. – Compo Oct 10 '19 at 11:44
  • I understand that it will recognize if the text is in any case. However, my requirement is to check if the file with specific case exists? – ajaygandhari Oct 10 '19 at 11:54
  • 2
    I know what you've asked for, my question is that you explain the reason, i.e. exactly what the purpose is, not the task. Writing good code requires that you have a clear understanding of the overall task. – Compo Oct 10 '19 at 11:58
  • What you have said is absolutely right. I believe with time I will be able to be precise and clear in my understanding. I am new to the Batch scripting and also StackOverflow, your suggestions will do good for me. Thanks, Compo. – ajaygandhari Oct 10 '19 at 12:03
  • ajaygandhari, so please explain the purpose, that's what the question area is for. Why do you need to know if the case of a string is an exact match for that used in a potential file's name? – Compo Oct 10 '19 at 12:09

2 Answers2

1

Here is a corrected form of Gerhard's original answer using FINDSTR:

dir "C:\files" /b /a-d | findstr /x /c:"TESTFILE.txt" && echo Found || echo Not found

The /c:"string" option is needed to force a literal interpretation (not regex), and to allow spaces in the name. The /X option is needed to force an exact match. You don't want to mistakenly match "the_TESTFILE.txt".

The above is probably the most straight-forward technique.

This can also be solved with a FOR statement. If %%F contains the name of an existing file (case insensitive), then %%~nxF will expand to the actual case sensitive name on disk.
But if %%F does not exists, then %%~nxF will never change the case. So IF EXIST must also be used.

The command must be carefully constructed to give the correct result. My goal is to have a self contained construct that allows use of && and || at the end to conditionally take action depending on whether the file exists or not.

pushd C:\files
(
  for %%F in ("TESTFILE.txt") do (
    if exist %%F if %%~F==%%~nxF popd & (call)
  ) && popd
) && echo NOT FOUND || echo FOUND

or on a single line

pushd C:\files&(for %%F in ("TESTFILE.txt") do (if exist %%F if %%~F==%%~nxF popd&(call))&&popd) && echo NOT FOUND || echo FOUND

Note the inverse logic. If the file exists with matching case, then (call) is the last executed command within the parentheses, resulting in a non-zero return code. If the file does not exists or does not match case, then popd is the last executed command within parentheses, resulting in a zero return code.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Yeah true, I will change my answer seeing as you now have the correct version of mine, no use I add the same again. – Gerhard Oct 10 '19 at 13:49
0

To search for the file with the prefered case:

@echo off
set "res=" && for /f "delims=" %%i in ('dir "C:\Files" /b /a-d') do if "%%i"=="TESTFILE.txt" set "res=%%i"
if defined res (echo %res% Found) else (echo File not found)

still using for /f but without setting a variable:

@echo off
for /f "delims=" %%i in ('dir "C:\files" /b /a-d') do if "%%i"=="TESTFILE.txt" echo File found & goto :eof
echo File not found

or simply without /f but you would need to

@echo off
for %%i in (C:\Files\*) do if "%%i"=="TESTFILE.txt" echo File found & goto :eof
echo File not found
Gerhard
  • 22,678
  • 7
  • 27
  • 43