santeri pohjaranta
santeri pohjaranta

Reputation: 29

Read from command prompt to batch file

everyone, I need to solve a problem.

I need to write to a command prompt like this.

с:>convert.bat "Ao äia õe uue oaõieaia õueaua ööau"

And the script needs to show to the user string which I write to there.

How I can do this?

Upvotes: 0

Views: 74

Answers (2)

ScriptKidd
ScriptKidd

Reputation: 841

@Santhosh J's answer breaks with parameters like &, ", |... But this one doesn't.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

::Redirect to file
>args.tmp (
SETLOCAL DISABLEEXTENSIONS
prompt #
@echo on
FOR %%a in (%%a) do REM # %1#
@echo off
ENDLOCAL
)

::Read file using SET /P
<args.tmp (
set /p arg=
set /p arg=
)

::Delete temp file
del /f /q args.tmp

::Trim prompt and surrounding #s
set "arg=!arg:~7,-2!"

::Print
ECHO(!arg!
ENDLOCAL

Based on Foolproof counting of arguments.

SETLOCAL DISABLEEXTENSIONS disables modifiers like %%~dpa.

FOR %%a in (%%a) do ...

  1. Replaces the FOR-variable %%a with the string %%a
  2. echoes the ... when ECHO is on.

REM # %1# protects it from parameters like /?

At last, SET /P provides a reliable way to read the file.

Upvotes: 0

Santhosh J
Santhosh J

Reputation: 368

Run time arguments in a batch file are managed with %1, %2....

That means, if you access %1 inside the batch file, you can get the first argument passed in.

welcome.bat :

@echo Hello, %1

Run welcome.bat "World" will give the output:

Hello, World.

Upvotes: 1

Related Questions