1

I've got a simple command I'd like to run in windows cmd:

//returns "file not found" for directories and "file exists" for files
test -f file/path/here && echo file exists || echo file not found

I've searched SO and found this Q&A: How to check if a directory exists in Windows?

the first comment in the accepted answer suggests that you can install binary files for it on windows machines. I've done this before with curl binary, but I haven't found test binary file. Is there some repository for such files? Where should I search. Google is unfortunately vague, because well, commands name is test :)

I am extremely uncomfortable with with cmd, so if you can write an alternative, I'd take it.

Note, I am aware of cygwin, but I'd like to keep this simple and in cmd.

sanjihan
  • 801
  • 4
  • 12
  • 24
  • 1
    One thing that's not clear: Why do you need a linux binary on Windows? Why not just do it the Windows way? https://stackoverflow.com/a/4340395/712526 – jpaugh Jul 25 '18 at 22:21
  • Concerning your original question, I'd say [MSYS2](http://www.msys2.org/) is a good bet. Under the install folder, under `/usr/bin`, I have a `test.exe`, which works in cmd. You can also use [`pacman`](https://www.digitalocean.com/community/tutorials/how-to-use-arch-linux-package-management) ([originally](https://wiki.archlinux.org/index.php/Pacman/Tips_and_tricks) from [Arch](https://wiki.archlinux.org/index.php/Pacman)) to install other packages, and it might not require opening the MSYS shell. – jpaugh Jul 25 '18 at 22:26
  • @jpaugh this will also return true for folders. And i am terrible with batch scripting – sanjihan Jul 25 '18 at 22:32
  • and you were spot on with msys2. feel free to post it as an answer – sanjihan Jul 25 '18 at 22:42
  • Incidentially, if you prefer the Linux shell, you can use MSYS for most day-to-day tasks. It does a pretty good job of emulating a Linux shell environment, using MinGW (i.e. Windows) executables. – jpaugh Jul 25 '18 at 22:51
  • May be I didn't get something, but if you talking about cmd, then ugly windows shell aka CMD can do what you want by simply using `IF` statement like `IF EXIST somefile ( ECHO Yes exist )` or opposite `IF NOT EXIST somefile ( ECHO Not exist )` More on this here: https://ss64.com/nt/if.html – Alex Jul 25 '18 at 23:19
  • @Alex if exists also returns true for folders. I would need some nested if statements, that I am horrified to do with cmd :) – sanjihan Jul 26 '18 at 08:06
  • Ohh, I feel your pain... When `cmd` come on scene I hopped `command.com` absurdity would gone away but it isn't happened. BTW, if you don't want 3rd party dependency on MSYS you can use new bloated way with PowerShell by utilizing `Test-Path` cmdlet like https://stackoverflow.com/a/31880042 or use VBS function FileExist (https://stackoverflow.com/a/22388077) – Alex Jul 26 '18 at 12:46
  • I don't remember, was WSL already a thing in 2018? – Egor Hans Mar 05 '21 at 15:45

1 Answers1

1

MSYS2 is a good bet. Under the install folder (in my case, C:\msys64), under usr\bin, I have a test.exe, which works in cmd.

You can also use pacman (originally from Arch) to install other packages, and it might not require opening the MSYS shell.

jpaugh
  • 1,378
  • 10
  • 20