Ricerca…


Sintassi

  • [comando] [[> | >> | <| 2> | 2 >>] file]
  • [[> | >> | <| 2> | 2 >>] file] [comando]

Parametri

Parametro Dettagli
comando Qualsiasi comando valido.
> Scrivi STDOUT su file.
>> Aggiungi STDOUT al file.
< Leggi il file su STDIN .
2> Scrivi STDERR su file.
2>> Aggiungi STDERR al file.
file Il percorso di un file.

Osservazioni

  • È possibile aggiungere tutti i reindirizzamenti desiderati, a condizione che il simbolo e il file di reindirizzamento rimangano insieme e nell'ordine corretto.

Un esempio...

@echo off
setlocal
set /p "_myvar=what is your name?"
echo HELLO!>file.txt
echo %_myvar%!>>file.txt
echo done!
pause
type file.txt
endlocal
exit

Ora file.txt ha il seguente aspetto:

HELLO!
John Smith!

(supponendo che tu abbia battuto John Smith come tuo nome).

Ora la console del file batch è simile a:

what is your name?John Smith
done!
Press any key to continue...
HELLO!
John Smith!

(e dovrebbe uscire così rapidamente che potresti non essere in grado di vedere nulla dopo il prompt Press any key to coninue... )

Reindirizza carattere speciale con espansione ritardata abilitata

Questo esempio fa eco al personaggio speciale ! in un file. Funzionerebbe solo quando DelayedExpansion è disabilitato. Quando l'espansione ritardata è abilitata, dovrai utilizzare tre segni di omissione e un punto esclamativo come questo:

@echo off
setlocal enabledelayedexpansion


echo ^^^!>file
echo ^>>>file

goto :eof    

    ^> is the text
    >> is the redirect operator

pause
endlocal
exit /b

Questo codice farà eco al testo seguente nel file

!
>

come

^^^ escapes the ! and echos it into the file
^> escapes the > and echos it into the file

Scrivi su un file

@echo off
cls
echo Please input the file path, surrounded by "double quotation marks" if necessary.
REM If you don't want to redirect, escape the > by preceding it with ^
set /p filepath=^> 

echo Writing a random number
echo %RANDOM% > %filepath%
echo Reading the random number
type %filepath%

REM Successive file writes will overwrite the previous file contents
echo Writing the current directory tree:
> %filepath% tree /A
echo Reading the file
type %filepath%

REM nul is a special file. It is always empty, no matter what you write to it.
echo Writing to nul
type %windir%\win.ini > nul
echo Reading from nul
type nul

echo Writing nul's contents to the file
type nul > %filepath%
echo Reading the file
type %filepath%


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow