Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
bigsanch
- Posts: 1
- Joined: 04 Oct 2013 10:24
#1
Post
by bigsanch » 04 Oct 2013 10:32
Hi
Is it possible to set a fixed defined text for a batch input? I hav a script where some files will be listet and if I want I can rename them.
So I define the new filename with:
My question is now it is possible, becouse I know from the searching befor the filename, to put it to this input to only press enter or onle change a letter
Like:
Code: Select all
set /p filename=Rename the file: filename.txt
Where filename.txt shoulb be part of the input and should be changeable.
Sry for my bad english and I hope you will understand what I'm looking for
thanks and greets Sanch
-
aGerman
- Expert
- Posts: 4743
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 04 Oct 2013 11:13
It should be possible but it's extremely tricky. I have to think about it. What I can say so far is that you can't use SET /P. Let me think about it - I'll come back as soon as I figured out what's the best way to solve your problem.
Regards
aGerman
-
Aacini
- Expert
- Posts: 1932
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#3
Post
by Aacini » 04 Oct 2013 12:50
You can not do that with pure Batch commands. You can do that using my GetKey.exe auxiliary program that you may download from
this site; look for program # 3. In that site there is :ReadLine subroutine that simulates SET /P command operation. That routine may be modified in order to accept an initial value for the input string that may be further modified.
I already answered this question before, although the requirements that time was to enhance the input field in a different color. You may review such "ReadField" subroutine at
this post.
Code: Select all
:ReadField var="prompt" /bf wide ["initial value"]
Antonio
-
aGerman
- Expert
- Posts: 4743
- Joined: 22 Jan 2010 18:01
- Location: Germany
#4
Post
by aGerman » 04 Oct 2013 13:21
OK that's the idea I had in mind
Code: Select all
@echo off &setlocal
call :PredefInput "Rename the file: " "filename.txt" filename
echo Input length is %errorlevel%
echo Your filename is %filename%
pause
goto :eof
:::::::::: END ::::::::::
:PredefInput ByVal_Message ByVal_Predefined ByRef_VarName
if "%__PRIN__%" neq "__PRIN__" (
setlocal DisableDelayedExpansion
if "%~3"=="" endlocal &exit /b 0
set "M=%~1" &set "S=%~2" &set "N=0" &set "__PRIN__=__PRIN__"
for /f %%i in ('"prompt;$h&for %%i in (1) do rem"') do set "BS=%%i"
setlocal EnableDelayedExpansion
<nul set /p "=.!BS! !BS!!M!!S!"
set "S=A!S!"
for /l %%i in (12,-1,0) do (
set /a "N|=1<<%%i"
for %%j in (!N!) do if "!S:~%%j,1!"=="" set /a "N&=~1<<%%i"
)
for %%i in (!N!) do endlocal &set "N=%%i"
)
set "C="
for /f "delims=" %%i in ('2^>nul xcopy /lw "%~f0" "%~f0"') do if not defined C set "C=%%i"
set "C=%C:~-1%"
setlocal EnableDelayedExpansion
if not defined C (
echo(
if defined S (
for /f delims^=^ eol^= %%i in ("!S!") do endlocal &endlocal &set "%~3=%%i" &exit /b %N%
) else endlocal &endlocal &set "%~3=" &exit /b 0
)
if "!BS!"=="!C!" (
set "C="
if defined S set /a "N -= 1" &set "S=!S:~,-1!" &<nul set /p "=%BS% %BS%"
) else set /a "N += 1" &<nul set /p "=.%BS%!C!"
if not defined S (
endlocal &set "N=%N%" &set "S=%C%"
) else for /f delims^=^ eol^= %%i in ("!S!") do endlocal &set "N=%N%" &set "S=%%i%C%"
goto PredefInput
I composed it from the String Length and the Hidden Input functions.
There are 3 Parameters you have to pass
- the message
- the default value
- the variable Name
Use the backspace key to delete characters. Hit enter to quit the input.
Regards
aGerman
EDIT changed the code (only slightly)