[Question]
  [
At runtime, keep prompting for *a line of input* until the user input is **not** the name of an existing file or directory or other file system item, relative to the current working directory. Then return/print that last inputted filename. You may assume that all user inputs will be valid filenames.
### Pseudo-code 1
```
myform = new form("GUI")
myform.mytxt = new editfield("")
myform.ok = new button("OK")
repeat
  waitfor(myform.ok,"click")
until not filesystem.exists(myform.mytxt.content)
return(myform.mytxt.content)
```
### Pseudo-code 2
```
LET TEXT = "."
WHILE HASFILE(TEXT) DO
  TEXT = PROMPT("")
ENDWHILE
RETURN TEXT
```
### Examples of user input which will cause re-prompting when on TIO:
```
.
..
.env.tio
/
/bin/[
/lost+found
```
### Examples of user input which will return when on TIO:
```
...
env.tio
../../bin/]
/lost/found
```
  
[Answer]
## Batch, 37 bytes
```
@set/ps=
@if exist %s% %0
@echo %s%
```
(For some reason current Windows 10 `CMD.EXE` corrupts the title when it executes the `%0`.)
[Answer]
## Mathematica, ~~33~~ 28 bytes
```
f:=Input[]/._?FileExistsQ:>f
```
This assumes Mathematica's notebook environment where we can query input from the user with `Input[]`. The user input should be [an actual string literal](https://codegolf.meta.stackexchange.com/a/7634/8478), so e.g. `"ab/cd.ef"` instead of just `ab/cd.ef`. The upside is that the input can be an arbitrary Mathematica expression that computes the input string.
This defines a symbol `f` which, when evaluated performs the required computation and ultimately evaluates to the first non-existent user input. It's essentially a nullary function, where we don't have to include `...[]` to call it.
We can also save a bunch of bytes over a traditional `If` expression by making use of the pattern substitution operator `/.`.
[Answer]
# [Perl 5](https://www.perl.org/) `-ln`, ~~12~~ 10 bytes
-2 bytes thanks to [@DomHastings](https://codegolf.stackexchange.com/users/9365/dom-hastings)
```
#!/usr/bin/perl -ln
-e||1/!say
```
[Try it online!](https://tio.run/##NcoxCoAwDEDRPbdwliY6eAxP4FAqRizUtNiqCJ7d6CL87f3EW@hUDd93S1V2lyoCfrEcWHwEAhq90AAUYi71HHeZ4MeysE0bH/Z0WYqdfWBxKwPCE9M3SFYTRE3fYdtg8wI "Perl 5 – Try It Online")
[Answer]
# Bash, 29
```
read f
[ -e $f ]&&$0||echo $f
```
[Answer]
# PowerShell 2 (through 6), 35 bytes
```
while(Test-Path($x=Read-Host)){};$x
```
`Read-Host` waits for input (if given a string as a parameter, uses the string as a prompt). If the provided input is a filename (or folder name) for one that exists, `Test-Path` returns `$true`, and the do-nothing block `{}` executes, and it re-prompts for input. If `Test-Path` returns `$false` because the input is not an extant file or folder, the do-nothing block does not execute, and the input name is printed.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 62 bytes
```
main(){char b[99];while(scanf("%s",b)&&!access(b,0));puts(b);}
```
[Try it online!](https://tio.run/##S9ZNT07@/z83MTNPQ7M6OSOxSCEp2tIy1ro8IzMnVaM4OTEvTUNJtVhJJ0lTTU0xMTk5tbhYI0nHQFPTuqC0BMjUtK79/1@PSw@IUvPK9Eoy87n0ufSTMvP0o4GCelz6OfnFJdpp@aV5KRC2PoQNUw0A "C (gcc) – Try It Online")
```
main(){
    char b[99]; // Declare buffer b
    while (scanf("%s",b)&&!access(b,0));    // Take one line of input, and test if file is accessible (exists)
    puts (b);   // If doesn't exist, loop ends and print file
}
```
[Answer]
# [Funky](https://github.com/TehFlaminTaco/Funky), 40 bytes
```
tryfor)io.open(s=io.read())catchprint(s)
```
In true funky style, this uses keywords jammed against eachother, unmatching brackets and implicit keywords. Cleaned up, this looks like:
```
try{
    while(true){
        s = io.read()
        io.open(s)
    }
}catch(e){
    print(s)
}
```
# Breakdown
```
try                                     // Try statement, this one is expected to fail.
   for)                                 // for) is a for loop with no arguments, which is functionally equivilent to a while(true) loop, much like for(;;)
       io.open(                         // Try to open a file relative to the CWD. If this fails to find a file, it will throw an error and escape the try/catch
               s=io.read()              // Read a line from STDIN and store it as s, this will still pass it to the arguments of the call.
                          )
                           catch        // When io.open fails
                                print(s)// Print out the last entered line.
```
[Answer]
# [Haskell](https://www.haskell.org/), 76 bytes
```
import System.Directory
f=do x<-getLine;b<-doesPathExist x;last$pure x:[f|b]
```
[Try it online!](https://tio.run/##HYqxDoIwFEX3fsUb3AztLrDpxmDCSBwKfZXG0kfahymJ/17Fm7Ockzvr9ELvS3HLSpGh3xPjIq8u4sQUd2FbQ5Cb6oncuYD12FSGMN01z7fsEkOuvU58WreIkC@D/YyPsmgXoAVDAn6LmKCpwP5l3bjn2IWjFinkDwxvyY6EEmp0QQ1CeUp8trQFI7SQExk8Dl8 "Haskell – Try It Online")
Returns `IO x` where `x` is the inputted name of the file that does not exist.
## Ungolfed
```
import System.Directory
insist = do { file <- getLine;
              exists <- doesPathExist file;
              if exists then insist else pure file }
```
[Answer]
# [R](https://www.r-project.org/), ~~66~~ 51 bytes
```
while((s=readline())%in%list.files(a=T)){};print(s)
```
*-15 bytes thanks to plannapus*
Runs a potentially infinite loop, where on each iteration
1. A single line of user input is stored in the variable `s`
2. We check if the input is in the list of filenames for the working directory (the `a=T` option for `list.files()` must be used to pick up things like `..`)
3. If `s` is in that list, we go to the next iteration; if not, we break the loop and print `s`.
[Answer]
# [Python 3](https://docs.python.org/3/), 55 bytes
```
import glob
s="."
while glob.glob(s):s=input()
print(s)
```
[Try it online!](https://tio.run/##HYm7DoAgDAD3foVxgsEyuJnwMyREm2hLoD74ekSTyw13qeomPLdGR5Ksw7pLgOJHHOHeaI9/wE@m2KV44nSqsZAysfbUGgJ2Il@oJODABWJgmeJDRSHU2P8L "Python 3 – Try It Online")
-4 bytes thanks to ManfP  
-6 bytes thanks to Rick Rongen
[Answer]
# C#, 101 bytes
```
()=>{var s="";try{for(;;System.IO.File.GetAttributes(s=System.Console.ReadLine()));}catch{}return s;}
```
For each of the 4 valid return values:
* [Try it online!](https://tio.run/##RY7BSgQxDIbvfYqwpw5i@wB1FkRYERRBBQ/iodvJaGFsockMLKXPPmbXXQw/OeT/Pkig65ALrmHyRPCGxKCqAiD2HAMsOQ7w5GPSnRwryJJ5PRDjj9nNKdwQl5i@tjBCD6vu@m1dfAHqNxvH5VDHXLRzZ@Hh2ezihOYe@ZbF28@MpKk/13c5UZb6Bf3wGBPqrutcC57Dd20FeS4JyLVVPnDq/42L9l4i48kbxTwSTbXVKCPBtBiOWVll9zHZD2WnTHw15jkNAhh1AYyxkiPz@cfYE/ML "C# (.NET Core) – Try It Online") (returns '...')
* [Try it online!](https://tio.run/##RY7BSgMxEIbveYqhp4Ri8gBxCyJUBEVoBQ/SQ5qd1cCaQGZ2oYQ8@5rWFoefOcw3H/ye7nzKuPjREcE7EoMoAoDYcfAwp9DDqwtRqnYs0Fab/YkYf/R2iv6eOIf4tYEBOlik6jZldhmoW60s51MZUpbWXoXnN70NI@on5Adu3nFiJEndFT@mSKnhHbr@JUSUSilbvWP/XWpGnnIEsnVpDaz4r3HTPnJgvHhDM88fVdRFC92CcdYckjDCHEM0n8KMiXg9pCn24ga1Ni1nfvjj5sJ/AQ "C# (.NET Core) – Try It Online") (returns 'env.tio')
* [Try it online!](https://tio.run/##RY7BSgQxEETv@YpmTxnE5APiLIiwIiiCCh7EQzbTswbGBNI9A0vIt4@964pN0YeqelCBrkMuuIbJE8EbEoOqCoDYcwyw5DjAk49Jd2JWkCf3eiTGb7ObU7ghLjEdtjBCD6vu@m1dfAHqNxvH5VjHXLRzF@Dh2ezihOYe@ZaF28@MpKm/xHc5UZb4Bf3wGBPqrutcC57DV20FeS4JyLVVFjj1P@MPey@R8cyNQp4aTbXVKCPCtBiOWVll9zHZD2WnTHw15jkNUrCik//569uz/wM "C# (.NET Core) – Try It Online") (returns '../..bin/])
* [Try it online!](https://tio.run/##RY7BagMxDETv/gqRk02p/QHOBkohJdBSaAI99OR4talhY4OlXQjG375x05QKIYYZPRhPjz5lXPzoiOCAxCCKACB2HDzMKfTw5kKUqpkF2mmzvxDjWW@n6NfEOcTTBgboYJGq25TZZaButbKcL2VIWVp7B3bvehtG1C/IT9y448RIkrp7/JwipRZ/oOtfQ0SplLLVO/bfpWbkKUcgW5fWwIr/Gn/YZw6MN25o5M9HFXXRQrfFOGsOSRhhjiGaL2HGRPwwpCn2v9rc9BU "C# (.NET Core) – Try It Online") (returns '/lost/found)
# Ungolfed
```
() =>
{
    var s = "";
    try
    {
        for(;; System.IO.File.GetAttributes(s = System.Console.ReadLine()));
    }
    catch {}
    return s;
}
```
## Explanation
relies on the fact that File.GetAttributes() throws an exception if file system object specified in its argument doesn't exist.
[Answer]
# Powershell 3.0, 75 bytes
```
$x=1;while($x){$i=Read-Host;$x=Test-Path("$PSScriptRoot\$i")};Write-Host $i
```
First attempt; I'm sure there are a few optimizations I could make. 
A slightly more readable form:
```
$x=1;                                                                       # Make sure we enter our while loop.
     while($x){                                                             # While we keep getting file names,                   
               $i=Read-Host;                                                # Get input from the user
                            $x=Test-Path("$PSScriptRoot\$i")};              # Combine our path with the user input, and see if it already exists.
                                                              Write-Host $i # Return the final (valid) file name.
```
[Answer]
# Java 9, 87 bytes
```
v->{String s;for(;new java.io.File(s=System.console().readLine()).exists(););return s;}
```
## Ungolfed
[TIO's JVM apparently has no system `Console`](https://tio.run/##JcoxDoMwEETROpxiSlyEPsoZqCijFBtjIRPjtdgFKYo4u7MK1Ujz30w7XbmEPI/vW60@kQh6ihnfBijbK0UPUVKbneOIxVo76Brz9HiC1kncnwLDRzQsHW/aFcuazJ2X5yycQuvc3eTRHJdafw), so it's not testable there (see [`System.console()`](https://docs.oracle.com/javase/9/docs/api/java/lang/System.html#console--)).
```
import java.util.function.*;
class Main {
  public static void main(String[] args) {
    Function<Void,String> f =
v->{
  String s;
  for(;new java.io.File(s=System.console().readLine()).exists(););
  return s;
}
;
    System.out.println(f.apply(null));
  }
}
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~158~~ 118 bytes
```
require('readline').createInterface({input:process.stdin}).on('line',s=>require('fs').existsSync(s)||--console.log(s))
```
[Try it online!](https://tio.run/##VYy9DgIhEIR7XuQgCvQm2ltbWnGwpxiye7LcxZ/z2REtTJxMJpNJvrm42bHPcSwaKUCtGa5TzCC7DC6kiNAp41svsMcCeXAe5DPiOJXNmMkDs@ESIr6UIZTdl1jzdvf7Gbg9wC1y4cMdvWS1LFp7QqYEJtGpLapWI0wz4GxKJGGF7SPao7CJuKwGmjCIPrmz/oR4fCX@mTc "JavaScript (Node.js) – Try It Online")
Credit to @ConorO'Brien for coming up with shorter version. Inlined objects instead of using consts and utilizing error exit condition instead of explicitly exiting.
[Answer]
# [Clean](https://clean.cs.ru.nl), ~~100~~ 94 bytes
```
import System.IO,System.File
Start w#(s,w)=evalIO getLine w
#(b,w)=fileExists s w
|b=Start w=s
```
[Try it online!](https://tio.run/##LYtBCoNAEATvvmLRiwH1B96SgCAY8AWjGc3C7K44E42Qt2digrfuqu6eELy6cH8SGgfWq3VTmMW0Gwu6omqyI10tYdQK7G5NUs7WU4kLUNWYEaW2Hs0aJWn348M@vbwsCxve6bsrj1/JqgX6pRAboviBRCH@9APByJp3mle1njcPzvb8LzcCGcLsvg "Clean – Try It Online")
**single-expression version:**
```
import System.IO,System.File
Start w=(\(s,v)=(\(b,w)|b=Start w=s)(fileExists s v))(evalIO getLine w)
```
[Try it online!](https://tio.run/##NYpBCsIwEEX3PUXoKoHaG3SnQqFQoVs3aZ3WwCSRzpha8OyOVXT13@P9AcEG8fFyR1DeuiDO3@LMqluJwZd1W/zo6BCyju3WlkqfNRXJfLYvFvPsq38ho8fteXg4YlKkkjEaksW6VRNw4wKoxYiUEFLJLmb5FRBj/hpGtBPJrpdd3ch@Dda7gb5yQstjnP0b "Clean – Try It Online")
[Answer]
# Perl 6, 39 bytes
```
my$f=".";while $f.IO.e {$f=get};say $f;
```
This works in the REPL, but it doesn't seem to work properly in TIO.
[Answer]
# PHP, 43 bytes
```
<?for(;file_exists($f=readline()););echo$f;
```
Run as CLI. Quite easy to understand.
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~40 39~~ 37 bytes
```
{}while File.exist? gets.chomp
$><<$_
```
[Try it online!](https://tio.run/##KypNqvz/v7q2PCMzJ1XBDUjopVZkFpfYK6SnlhTrJWfk5xZwqdjZ2KjE//@vx6UHRKl5ZXolmflc@lz6SZl5@tFc@jn5xSXaafmleSlcUEkA "Ruby – Try It Online")
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 17 bytes
```
{⍞}⍣{~⎕NEXISTS⍺}⍬
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT/8PBNWPeufVPupdXF33qG@qn2uEZ3BI8KPeXUChNVx6XHpAlJpXpleSmc@lz6WflJmnH82ln5NfXKKdll@al8IFkyzJSI0vKEotiy9PLM4riU/LzEnNS8xNBQA)
[Answer]
# [Kotlin](https://kotlinlang.org), 67 bytes
```
val f={var s="."
while(java.io.File(s).exists()){s=readLine()!!}
s}
```
[Try it online!](https://tio.run/##XUsxDsIwENvvFWmnRIjLjtSViY2RKVXT9iBcUJIGpKpvDwExIdmWLds3nxxxGRcWd0MsswkmTKIyHsQ5BeJJiRUe1STHcpRKwVaycWLs1joWsWuxhedMzsqryQbJ4/ETokL7ophivayxC9YMJ2IrVdNsELdSELDCcsZEHjTonvgr@gLa@Zh2o194gN6Zef@R3@avRnwD "Kotlin – Try It Online")
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/attache), 35 bytes
```
{While[FileExists[x:=Prompt[]],0]x}
```
[Try it online!](https://tio.run/##JYexCoAgFAB3f6XwNQeONbc1PByslB6URr5CiL7dhOA47gyzmVebXavyM660WeyLukSRI6ZWDWfYD0at60anNw8neUZXPkshC9bfkikIEDCRBxSwhciVC5df/oa/TXTLBw "Attache – Try It Online")
## Alternative solutions
**35 bytes:** `{If[FileExists[x:=Prompt[]],$[],x]}`, recursive function.
**37 bytes:** `{NestWhile[p:=Prompt,p[],FileExists]}`, iterative function.
[Answer]
# [Min](https://min-lang.org), 38 bytes
```
"." :a (a exists?) ("" ask @a) while a
```
Leaves last entered filename on the stack.
## Explanation
```
"."         ; Put . on the stack. Every directory should contain this...
:a          ; Assign to a
(a exists?) ; A quot that checks if a exists in current directory
("" ask @a) ; Read line from stdin, assign to a
while       ; Do the second quote while the first leaves true on the stack
a           ; Leave a on the stack
```
[Answer]
# SmileBASIC, 27 bytes
```
INPUT S$EXEC!CHKFILE(S$)?S$
```
]