I have following code, asking for user to fill in filename:
echo Type in your filename (path + filename):
set userinp=
set /p userinp= ^>
What I would like:
already 'prefill' fullpath+filename, which the user can then edit.
In most cases, this would be: %cd%+filename (most common file selected)
Type in your filename (path + filename):
> D:\Download\MyFile.txt
(the string 'D:\Download\MyFile.txt' should be editable)
NB: must be achieved strictly with Windows batch commands; no use of the other languages and/or programs.
This method is simple and have the advantage that you may use the standard command-line navigation keys, that is, besides the edition you may enter the first letters of a folder/file and browse through the existent folders/files with TAB key. You may even put "{TAB}"
in the prefill value to automatically prefill with the first file in current directory, or "Dat{TAB}"
for the first file that start with "Dat", etc.
@if (@CodeSection == @Batch) @then
@echo off
echo Type in your filename (path + filename):
cscript //nologo //E:JScript "%~F0" "%cd%\commonFile.ext"
set "userinp="
set /P "userinp=>"
echo Value read: "%userinp%"
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
Note that the cscript
command, used here to execute a line of JScript code, is a standard "DOS" command provided with all Windows versions since XP on.
EDIT: Include the path of first file
You may put "%cd%\{TAB}"
in the prefill value in order to get the first file in current folder including the path; however, this method fail if the path contain any space. The new code below fix this point, although it still may fail if at any point in the path exist two folders with same name until the first space and the wanted folder is not the first one.
@if (@CodeSection == @Batch) @then
@echo off
setlocal EnableDelayedExpansion
set "curdir="
for %%a in ("%cd:\=" "%") do (
for /F "tokens=1*" %%b in (%%a) do (
set "curdir=!curdir!\%%b"
if "%%c" neq "" set "curdir=!curdir!{TAB}"
)
)
echo Type in your filename (path + filename):
cscript //nologo //E:JScript "%~F0" "%curdir:~1%\{TAB}"
set "userinp="
set /P "userinp=>"
echo Value read: "%userinp%"
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
Code taken from aGerman on Dostips.com
@echo off &setlocal
call :PredefInput "Type in your filename (path + filename):>" "D:\Download\MyFile.txt" filename
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