1

Assume I want to run

>foo bar

but bar changes depdending on where I am in my filesystem. What I'd like to do, so I don't have to remember the proper value of bar, is to keep a file fizz in the respective folders so I can do something like call

>foo fizz

which of course doesn't work out of the box. Neither does

>foo < fizz

because foo doesn't expect input, just a parameter. I found a solution for Linux, but nothing for Windows.

Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56
Raketenolli
  • 743
  • 8
  • 24

2 Answers2

2

Assuming fizz contains a single line with no whitespace, the one-liner

for /F %i in (fizz) do @foo %i

will do what you want. If the argument you're putting into fizz may contain whitespace, use

 for /F "delims=" %i in (fizz) do @foo "%i"

to pass the argument in one piece. Double the % characters if putting this into a batch file.

Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56
1

Anton gave exactly what you wanted, and should be marked as correct answer. I can however add, (from what I have experienced before, especially in java versions) that perhaps foo could have a different version, similarly to how bar could differ, based on where in the file system you run it from. So fizz will have both executable and parameter. i.e

Content of fizz in path 1 :

foo bar

Content of fizz in path 2 :

foo1 bar2

then just call the content of the file, without specifying a command (as it is already present in the file fizz).

for /f "delims=" %i in (fizz) do %i

This method, can also run more commands in next lines that exists in fizz for instance if fizz contains:

ping localhost
ping 127.0.0.1
nslookup hostname1

Each will be ran in sequence with above for loop.

PS! Reminder to double up on the % to %% if you need to run it from a batch file.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • If I understand your first two examples correctly, I might as well just write a folder-specific batch file that includes both the command and the parameters. While reading the first answer it occurred to me that that is actually the better solution for my specific application. – Raketenolli Aug 22 '18 at 06:43
  • @Raketenolli almost like a batch file for each, but not quite, I will add some explanation to why it is not the same. :) Yes, like I said, the first should be marked as correct, as that is exactly like you requested and I do not expect you to mark mine as correct. Thanks for the upvote though, I do appreciate it. – Gerhard Aug 22 '18 at 06:49