@echo off
set "input=open file.txt"
for /f "tokens=2" %%a in ('echo/%input%') do (set "fileName=%%a")
echo/%fileName%
pause>nul
I need to get the filename from a command in batch input loop
-
30-06-2023 - |
문제
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.
해결책 3
다른 팁
echo %input:~5%
Up to the end of the string if it is not indicated.
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