0

I'm making a prompt loop in a batch file

I'm trying to read the input for commands and arguments.

Currently, I check the first 4 letters for open, then I want to grab the next word which should be a filename to open.

I'm wanting to grab the text between spaces at index 1.

open file.txt -> file.txt

I need something like an inverse of mid-string

mid-string: %input:~0,4% - open file.txt -> open

I honestly need %input:~5,-0% to work! haha

%input:~5,-1% works... Why not with a 0 instead of 1?

It's too bad batch has no way of getting a string's length.

I found something that would get a string's length: How do you get the string length in a batch file?

I tried it with the code:

call :strlen length input
echo %input:~5,length%

It always parsed weird.

Community
  • 1
  • 1
Tgwizman
  • 1,469
  • 2
  • 19
  • 34

3 Answers3

2
echo %input:~5%

Up to the end of the string if it is not indicated.

MC ND
  • 69,615
  • 8
  • 84
  • 126
1

a little variation to Rafaels answer (check first word for open):

@echo off
set "input=open file.txt"
for /f "tokens=1,2" %%a in ('echo/%input%') do ( if /i "%%a"=="open" set "fileName=%%b")
echo/%fileName%
pause>nul
Stephan
  • 53,940
  • 10
  • 58
  • 91
0
@echo off
set "input=open file.txt"
for /f "tokens=2" %%a in ('echo/%input%') do (set "fileName=%%a")
echo/%fileName%
pause>nul
Rafael
  • 3,042
  • 3
  • 20
  • 36