China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-23 09:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » What does 2>nul 1>nul in batch processing statements mean? DigestI View 29,423 Replies 57
Original Poster Posted 2005-09-12 11:31 ·  中国 山东 济南 莱芜区 联通
高级用户
★★
Credits 736
Posts 210
Joined 2004-04-12 00:00
22-year member
UID 22336
Gender Male
From 中國
Status Offline
The two >NUL in the statement "rmdir /S /Q %mhnet% 2>NUL 1>NUL" are used to redirect error output and standard output respectively to the null device. Specifically, 2>NUL redirects the error output (standard error) to the null device, and 1>NUL redirects the standard output to the null device, which means that the error and output information generated by the rmdir command will not be displayed on the screen.

The following is the translated content of the entire batch file related part:

I downloaded an automatic upgrade batch file from Huada Technology. What does the two >NUL in the statement like rmdir /S /Q %mhnet% 2>NUL 1>NUL mean? I don't understand very well. Please ask experts for advice, thank you!

---------- Edited by willsort ----------
Modified original title: Ask: Batch file statement problem?
---------- Edited by willsort ----------



Ask: Batch file statement problem?
The original file is as follows:
@echo offrem Usage introduction
rem Function: Try to set up automatic connection to Huazhong University of Science and Technology Windows Update server
rem First perform service check, for the machine without the installed service, install the service first, restart and then modify the settings

echo For Windows 2000,Windows XP,Windows 2003 only.
echo Only applicable on Windows 2000,Windows XP,Windows 2003
echo For Normal user only (computer that runs every day)
set Server=0
rem Server=1
pause
cls

rem Create temporary folder
set mhnet=%temp%\mhnet
rmdir /S /Q %mhnet% 2>NUL 1>NUL
mkdir %mhnet%

rem System language environment judgment
reg query HKLM\SYSTEM\CurrentControlSet\Control\Nls\Language >%mhnet%\Language.log
find "Default" %mhnet%\Language.log |find "0804" >NUL
IF NOT ERRORLEVEL 1 (
set Language=Chinese
) else (
set Language=English
)

if %Language%==Chinese (
echo This program is specially designed to set up Huazhong University of Science and Technology Windows Automatic Updates service
) else (
echo This will setup HUST Windows Automatic Updates Services
)

start readme.txt

rem Check the integrity of supporting files
call :AllFiles

rem Check if it belongs to NT series
call :CheckSystem

rem Check if it has administrator privileges
call :CheckAdmin

rem Check if the Automatic Updates service is normally installed
call :CheckService

call :HUSTRegImport


if %Language%==Chinese (
echo For program discussion, welcome to bbs.whnet.edu.cn security version
echo Program ends
) else (
echo We welcome you to press your opinion on bbs.whnet board:security
echo Program Exit
)

pause
set mhnet=
set patchfile=
set downloadurl=
goto :eof
exit
:ends
rem Main function ends




rem *************************
rem *************************
rem Import: HUSTRegImport
rem *************************
rem *************************
:HUSTRegImport
ECHO Backup registry (Backup Reg) %temp%
pushd %temp%
set BakFile1=AutoUpdateBak1.reg
set BakFile2=AutoUpdateBak2.reg
if exist %BakFile1% (
ECHO This patch has been installed, press any key to delete the original backup
PAUSE
delete %BakFile1% /f /q >NUL 2>NUL
delete %BakFile2% /f /q >NUL 2>NUL
)
popd

pushd %mhnet%
reg EXPORT "HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" %BakFile1% 1>NUL
reg EXPORT "HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate" %BakFile2% 1>NUL
move %BakFile1% %temp% >NUL
move %BakFile2% %temp% >NUL
set BakFile1=
set BakFile2=
popd

ECHO Stopping WindowsUpdate service (Stopping Update service)......
net stop wuauserv >NUL 2>NUL

ECHO Modifying registry operation (Modifying Registry)......
ECHO Y |reg DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" /va /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate /v WUServer /t REG_SZ /d "http://windowsupdate.hust.edu.cn" /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate /v WUStatusServer /t REG_SZ /d "http://windowsupdate.hust.edu.cn" /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v NoAutoRebootWithLoggedOnUsers /t REG_DWORD /d 1 /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v NoAutoUpdate /t REG_DWORD /d 0 /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v RescheduleWaitTime /t REG_DWORD /d 1 /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v ScheduledInstallDay /t REG_DWORD /d 0 /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v UseWUServer /t REG_DWORD /d 1 /f >NUL



if %Server%==1 (
ECHO Automatic Updates Service installed for server
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v AUOptions /t REG_DWORD /d 4 /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v ScheduledInstallTime /t REG_DWORD /d 11 /f >NUL
) else (
ECHO Automatic Updates Service installed for non-server
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v AUOptions /t REG_DWORD /d 2 /f >NUL
reg add HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU /v ScheduledInstallTime /t REG_DWORD /d 12 /f >NUL
)

rem Set the service to start automatically
ECHO Set service to automatic start (Set service to automatic start)
reg add HKLM\SYSTEM\CurrentControlSet\Services\wuauserv /v Start /t REG_DWORD /d 2 /f >NUL
reg add HKLM\SYSTEM\CurrentControlSet\Services\BITS /v Start /t REG_DWORD /d 2 /f >NUL

ECHO Restarting WindowsUpdate service (Starting Update Service)......
net start bits >NUL 2>NUL
net start wuauserv >NUL 2>NUL

goto :eof





rem *************************
rem *************************
rem Stop/delete service function
rem *************************
rem *************************
:StopService
set SrvName=%1
net stop %SrvName% >NUL >NUL2
reg query HKLM\SYSTEM\CurrentControlSet\Services\\%SrvName% >NUL
if not errorlevel 1 (
reg delete HKLM\SYSTEM\CurrentControlSet\Services\\%SrvName% /f >NUL
) ELSE (
if %Language%==Chinese (
echo System service: %SrvName% *not found*
) else (
echo System Service: %SrvName% Not Found
)
)
goto :eof





rem *************************
rem *************************
rem Judge whether it is an NT series system, and exit for win9x system
rem *************************
rem *************************
:CheckSystem
if NOT %OS%==Windows_NT (
if %Language%==Chinese (
echo This program is specially designed for Chinese/English version of Win2000, WinXP, Win2003
echo Users of Windows 95, Windows 98, Windows ME do not need to run
echo Program exit.
) else (
echo Design for Win2k,WinXP,Win2003 with Chinese or English version only.
echo Windows 95,Windows 98, Windows ME will not be troubled by this bug.
echo Thank you for checking. Please deliver this to someone who need it.
)
pause
exit
) else (
if %Language%==Chinese (
echo The user is using a system based on WindowsNT kernel,
) else (
echo You are using a system based on WindowsNT kernel,
)
)
goto :eof



rem *************************
rem *************************
rem Judge whether it is an NT series system, and exit for win9x system
rem *************************
rem *************************
:CheckAdmin
net user %username% 1> %mhnet%\ifadmin.log
rem If it is not a system administrator
find "*Administrators " %mhnet%\ifadmin.log >NUL
IF ERRORLEVEL 1 (
if %Language%==Chinese (
echo The user is not a member of the system administrator group, and cannot execute the corresponding program
echo Please change to a user with sufficient privileges to re-run this program
) else (
echo This program require administrator right to continue
echo Please switch user and try again
)
pause
exit
goto :eof
) else (
if %Language%==Chinese (
echo The user belongs to the system administrator group,
) else (
echo You have got the right of administrator,
)
)
goto :eof





rem *************************
rem *************************
rem First perform patch check, for the machine without installed patch, install the patch first, restart and then kill the virus
rem *************************
rem *************************
:CheckService
rem if %Language%==Chinese (
rem echo Checking Automatic Updates
rem ) else (
rem echo Checking Automatic Updates Service
rem )
rem Check the registry value of the service

reg query "HKLM\SYSTEM\CurrentControlSet\Services" >%mhnet%\Services.log
find "wuauserv" %mhnet%\Services.log >NUL

IF ERRORLEVEL 1 (
rem Your computer has not installed the Automatic updates service
call :InstallService
pause
exit
rem Abort execution
) ELSE (
rem if %Language%==Chinese (
rem echo Registry check shows that your system has installed the wuauserv service.
rem ) else (
rem echo reg reports that your system has wuauserv service installed
rem )
)
goto :eof





rem *************************
rem *************************
rem Clear a \Run value in the registry
rem *************************
rem *************************
:CleanRun
reg delete HKLM\Software\Microsoft\Windows\CurrentVersion\Run\\%1 /f >NUL
goto :eof





rem *************************
rem *************************
rem Stop unsafe service
rem *************************
rem *************************
:StopUnSafeSrv
rem net stop %1 >NUL
reg add HKLM\SYSTEM\CurrentControlSet\Services\\%1 /v Start /t REG_DWORD /d 0x3 /f >NUL
goto :eof





rem *************************
rem *************************
rem Import the execute file association of the normal registry
rem *************************
rem *************************
:Regdefault
reg import regdefault.reg >NUL
goto :eof




rem *************************
rem *************************
rem Install wuauserv package
rem *************************
rem *************************
:InstallService
rem reg query HKLM\SYSTEM\CurrentControlSet\Control\Nls\Language >%mhnet%\Language.log

rem Chinese system
find "InstallLanguage" %mhnet%\Language.log |find "0804" >NUL
IF NOT ERRORLEVEL 1 (CALL :CNService )

rem English system
find "InstallLanguage" %mhnet%\Language.log |find "0409" >NUL
IF NOT ERRORLEVEL 1 (CALL :ENService )

rem Other language packs, although the above language packs are called by call, but do not want to return
if %Language%==Chinese (
echo You are not using a Chinese/English operating system,
echo Please write down the displayed InstallLanguage and Default values below, and download the corresponding service from the Microsoft website
echo Program exit
) else (
echo You are not using windows with Chinese/English language
echo We suggest you download the service from microsoft website.
echo Please write down the InstallLanguage & Default value for later use.
echo program terminated
)
find "InstallLanguage" %mhnet%\Languate.log
start http://www.microsoft.com/downloads/details.aspx?FamilyID=799432fb-c196-4f01-8cce-4f9ea58d6177&DisplayLang=en
rem Microsoft service installation web page
pause
exit
goto :eof





rem *************************
rem *************************
rem Chinese service package download and installation
rem *************************
rem *************************
:CNService
set patchfile=WUAU22CHS.msi
set downloadurl=http://windowsupdate.hust.edu.cn/patch/%patchfile%
call ownload
start %mhnet%\%patchfile%
if %Language%==Chinese (
echo Restart the computer after installing the service, and then run this program
) else (
echo Restart your computer when Service installation is done. And run again.
)
pause
exit
goto :eof





rem *************************
rem *************************
rem English service package download and installation
rem *************************
rem *************************
:ENService
set patchfile=WUAU22ENU.msi
set downloadurl=http://windowsupdate.hust.edu.cn/patch/%patchfile%
call ownload
start %mhnet%\%patchfile%
if %Language%==Chinese (
echo Restart the computer after installing the service, and then run this program
) else (
echo Restart your computer when Service installation is done. And run again.
)
pause
exit
goto :eof





rem *************************
rem *************************
rem Check the integrity of some files
rem *************************
rem *************************
:AllFiles
if not exist reg.exe (
echo reg.exe NOT FOUND
pause
exit
) else (
copy reg.exe %mhnet% /y >NUL
)

if not exist wget.exe (
echo wget.exe NOT FOUND
pause
exit
) else (
copy wget.exe %mhnet% /y >NUL
)
goto :eof





rem *************************
rem *************************
rem Download patch, pass in file name with %patchfile%
rem *************************
rem *************************
ownload
if %Language%==Chinese (
echo Automatically download %downloadurl%
) else (
echo Downloading %downloadurl%
)
pushd %mhnet%
if exist %patchfile% (
del %patchfile% /f /q >NUL 2>NUL
)
wget %downloadurl%
IF NOT ERRORLEVEL 1 (
if %Language%==Chinese (
echo Download successful.
) else (
echo Download Success
)
) else (
if %Language%==Chinese (
echo Download failed. Program exit
) else (
echo Download Failed. Terminated
)
pause
exit
)
popd
goto :eof



rem ----- this is the end of this batch file -------

[ Last edited by willsort on 2006-5-13 at 18:28 ]
Floor 2 Posted 2005-09-12 13:06 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xjhlhb:

The detailed code is still being learned. Only a simple explanation is given for the statement you mentioned: rmdir /S /Q %mhnet% 2>NUL 1>NUL.

The general meaning of this code is to delete the directory specified by %mhnet%. /s means to delete the subdirectories within it. /q means not to prompt for confirmation when deleting the directory tree. 1>nul means to prohibit outputting the information of correctly deleting the directory tree. 2>nul means to prohibit outputting the error information during the deletion process.

Among them, 1 and 2 are both representing the address of a certain data flow input and output (NT CMD calls it a handle, and MSDOS calls it a device). The following table (quoted from the "Using command redirection operators (Redirection Operators" section of the WinXP help document) will list the available handles.


Handle Handle number code Description
STDIN 0 Keyboard input
STDOUT 1 Output to the command prompt window
STDERR 2 Error output to the command prompt window
UNDEFINED 3-9 These handles are defined separately by applications and various specific tools.



───────────────── Moderator's reminder ─────────────────
The content shown in red font is currently identified as incorrect remarks. Please pay attention!
───────────────── Moderator's reminder ─────────────────


[ Last edited by willsort on 2006-3-24 at 08:48 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2005-09-14 16:39 ·  中国 山东 济南 莱芜区 联通
高级用户
★★
Credits 736
Posts 210
Joined 2004-04-12 00:00
22-year member
UID 22336
Gender Male
From 中國
Status Offline
Thanks to the expert in floor 2 for the solution. It seems that this command is completely correct!
Through such a batch processing, we summarize as follows: 1 REG can read registry information; 2 CALL can call the command segment in this file; 3 GOTO can transfer to the end of the file; 4 >NUL can formulate the direction of information flow

In addition: Please teach: How to judge whether the system is 95/98 or 2000/XP through batch processing?
Floor 4 Posted 2005-12-13 11:54 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
This program is great!
It has taught me a lot!
I hope there can be such posts frequently. It is suggested to establish a camp similar to this program. People who are interested can add comments to programs without comments, so that we can ask fewer questions.
Floor 5 Posted 2006-03-24 12:01 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re All:

First of all, correct and supplement some remarks mentioned in floor 2, for your information. The numbers such as 1, 2 called "handles" in CMD in the above text are not defined under MSDOS and 9x COMMAND, and have nothing to do with the concept of "devices".
==============================================
"Redirection" is a command-line feature supported since MSDOS, which is responsible for transferring the input and output requests generated by the specified command or statement from the default "console" to other "devices" to complete. The starting sign of it is that the "redirection symbols" (including three symbols >, >>, <, their respective meanings are shown in ) appear in the sentence.

The input and output requests of general command-line programs are usually completed through three internally defined "ports" (called "handles" under NT, not defined under DOS), namely standard input stdin, standard output stdout, and standard error stderr. The devices they usually point to are the console (console, code is CON), where stdin points to the keyboard of the console, and stdout/stderr point to the monitor of the console. Therefore, the console usually refers to the combination of the keyboard and the monitor, which is the concept reflected on the terminal of the early mainframe. Among them, stdin can be redirected by <, stdout can be redirected by >, >>, and stderr cannot be directly redirected under DOS. Only by transferring the system control to other devices through commands such as ctty or other methods can it be indirectly completed.

A "device" refers to a device driver or port code that can control PC hardware or ports, which is usually implemented and supported by the system bottom layer or hardware driver. For example, CON implemented by IO.SYS, system clock CLOCK$, unknown device CONFIG$, first serial port AUX, first parallel port PRN, all serial ports COM1~COM4, all parallel ports LPT1~LPT3, available drive letters A:-X: and the empty device NUL mentioned above. There are many other devices, such as XMSXXXX0 implemented by HIMEM.SYS, EMMXXXX0 implemented by EMM386.EXE, IFS$HLP$ implemented by IFSHLP.SYS, etc.

Among these devices, few can handle input and output information. Only devices such as CON, NUL, and serial or parallel port devices connected with input and output hardware (printers, MODEMs, etc.) can. They are called "character devices", and disk files are also listed as a special character device, which greatly expands the freedom and practicability of redirection, so many people also call redirection "file redirection".

The empty device NUL is a special device because it has no controllable PC hardware or port, but is only a fictional device or port, which only exists at the software level. Because of this, it can accept all redirected input and output requests without giving any response. Under NT, it will end the input request without giving any input information. Under DOS, it will repeatedly fill 127 binary bytes 1 (displayed as ^A) and then terminate the response. This feature makes it very similar to the "black hole" in astronomy that can swallow all matter and information, and also very similar to the "mysterious way" in philosophy that can reverse yin and yang and create something out of nothing. The reason why it exists is that we need a "recycling bin" that can silently and unconditionally absorb various redundant output information or input requests, just as the "black hole" is like a huge "cosmic garbage dump".

The command line does not make too many restrictions on the position where the redirection symbol appears. As long as the character device follows the redirection symbol, the following statements are equivalent:

echo Hello World> hello.txt
echo Hello> Hello.txt World
echo> Hello.txt Hello World
> hello.txt echo Hello World

Under the NT series command line, the scope of action of redirection changes from the entire command line to a single command statement, and is restricted by command separators &, &&, || and statement blocks.

echo Message1> msg1.txt & echo Message2> msg2.txt
if "%target%"=="" (echo message to screen ) else (echo message to file> %target%)

To sum up, >nul means redirecting the standard output request generated by this command to the empty device. Because of the silent characteristic of this device, it is equivalent to shielding (not hiding) the output information of this statement; while 2>nul is redirecting and shielding the standard error information output request when the program executes an error. When used together, all output information that may be generated by this statement is shielded.

Application of redirection commands in security offense and defense
http://www.cn-dos.net/forum/viewthread.php?tid=17578

[ Last edited by willsort on 2006-3-24 at 18:42 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2006-03-25 20:37 ·  中国 福建 漳州 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
After I read this article

http://www.cn-dos.net/forum/viewthread.php?tid=16942&fpage=1
I tried it myself and found an interesting phenomenon.

Some special effects can be achieved by entering the following commands in the NT command line.
echo. 2>nul 3>nul
After executing this command, all error prompts are not visible?

echo. 1>nul 3>nul
After executing this command, all output prompts are not visible (but error prompts will be displayed)?

These two commands cannot be used at the same time.
Also, I don't know how to recover (except using EXIT to go back and then enter CMD)?

Also, after executing the following statement, it will keep looping and cannot stop?
echo. 0>nul 3>nul

I wonder what the principle is??
QQ:366840202
http://chenall.net
Floor 7 Posted 2006-03-25 21:22 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
It's really interesting. I think the key is the device numbered 3. I don't know what device 3 represents.

0 is standard input (keyboard)
1 is standard output (screen)
2 is standard error (screen)

In chenall's first example, if we change it to:
somecmd 2>&1
or
somecmd 2>&3
the error messages will all be displayed.

In short, this topic is really worth exploring.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 8 Posted 2006-03-26 08:02 ·  中国 福建 漳州 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Another phenomenon is discovered.

echo. 2>nul 3>test.txt

Then the error messages will be written into the test.txt file.

By the same token,

echo. 1>nul 3>test.txt

The standard output information will be written into the test.txt file.

This topic is indeed worthy of discussion.

[ Last edited by chenall on 2006-3-26 at 10:10 ]
QQ:366840202
http://chenall.net
Floor 9 Posted 2006-03-26 09:55 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
The continuous practice in science has once again proven the unshakable truth that 1 + 2 = 3.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 10 Posted 2006-03-26 11:50 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Originally posted by chenall at 2006-3-25 20:37:
After I read this article

http://www.cn-dos.net/forum/viewthread.php?tid=16942&fpage=1
echo. 2>nul 3>nul
After executing this command, all error prompts are not visible?

echo. 1>nul 3>nul
After executing this command, all output prompts are not visible (but error prompts will be displayed)?

These two commands cannot be used at the same time.
In addition, I don't know how to recover it (except using EXIT to go back and then enter CMD...

Specifying the handle 3 twice naturally cannot be used at the same time. Try changing the second one to echo. 1>nul 4>nul and see
To recover, use echo 2>con 5>con respectively, echo 1>con 6>con. If you only want to recover a certain sentence, use >&, <& to copy the handle and see.
Not tested.
Floor 11 Posted 2006-03-26 14:38 ·  中国 福建 漳州 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Specifying handle 3 twice naturally can't be used simultaneously. Try changing the second one to echo. 1>nul 4>nul and see.
It really works.
To restore, use echo 2>con 5>con respectively, echo 1>con 6>con. If you only want to restore one sentence, try using >&, <& to copy handles and see.
Not tested.
Restoring doesn't work..
Of course, restoring one sentence can use command 2>con 1>con.

[ Last edited by chenall on 2006-3-26 at 14:45 ]
QQ:366840202
http://chenall.net
Floor 12 Posted 2006-03-26 15:56 ·  中国 江西 抚州 临川区 电信
高级用户
★★★
中國DOS聯盟常任參議员
Credits 686
Posts 318
Joined 2005-11-04 13:00
20-year member
UID 77577
Gender Male
Status Offline
Too....Too deep, save me, ```
Floor 13 Posted 2006-03-26 19:09 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

───────────────── Moderation Record ─────────────────
Performer: Will Sort
Operation: Merge Threads: "19735 - Ask: About NT Batch Processing Issues."
Explanation: The two threads have direct contextual connections
Punishment: Since the thread has original nature, no punishment is imposed
Reminder: Please visit {7326}Forum Newcomer Must-Read, Basic Code of Conduct for Everyone
───────────────── Moderation Record ─────────────────


Re chenall:

This is indeed an interesting and meaningful topic. Regarding this strange phenomenon, since it is not found in the public official documentation, it seems to be something between an undocumented feature and a program algorithm flaw.

The following conclusions were obtained through simple testing:

This phenomenon is caused by the secondary redirection of handles, regardless of the command body of the statement;
The target of the secondary redirection can be different. The previous redirection affects this statement, and the subsequent redirection affects the CMD environment after the statement ends;
The handle of the subsequent redirection must be an undefined handle and must not have been used before the previous redirection; this handle is immediately invalidated after use and cannot be repeatedly used for handle redirection of the default CMD environment;

Thus, my speculation about this phenomenon is as follows:

When modifying (redirecting or copying) a handle in a statement in CMD, in order to restore the modified handle after the statement is executed, the designer must copy (or back up) the handle before modification. As for the destination of the backup, CMD chooses the "undefined handle (3-9)" that has never been used, which seems to be an acceptable choice. However, there are obviously some loopholes in the program when judging and obtaining the unused handle. They first handle the first modification operation. After getting the handle to be modified, they immediately look for an unused backup handle. After finding the backup handle and backing it up, they then handle the subsequent modification operation. At this time, this backup handle can still be modified, causing that after the statement ends, CMD will use the modified backup handle to restore the handle of the first modification, and finally cause the default handle of CMD to be modified. At this time, CMD only then realizes that the backup handle has been used and will use the subsequent "unused handle" for backup when modifying the handle in the next statement.

The following statement should also be based on a similar principle, but it more accurately explains the timing of obtaining the backup handle.

pause 1>&3 3>nul

Therefore, the statement in the CMD help document that 3-9 are undefined handles should be inaccurate, because they are indeed used by the system for other purposes, and it is not "defined separately by applications, and they are unique to each tool". It seems that they are more like "reserved handles".

In addition, the behavior of handles in CMD is obviously more complicated, especially the behavior of handle copying, even the Microsoft documentation is vague about this. For example, the following statement can copy stdout to stderr, and stderr is redirected to the nul device, resulting in the stdout of pause being redirected to the nul device, that is, no output. This is understandable.

pause 2>nul 1>&2

In the following statement, just reversing the copy and redirection operations will no longer be effective. And this should also be related to the CMD characteristics mentioned above.

pause 1>&2 2>nul

Finally, it should be mentioned that handles in CMD have read and write attributes. stdin is read-only, stdout is write-only, stderr is readable and writable, and others are not defined. When copying a handle, the read and write attributes of the handle are also copied.

[ Last edited by willsort on 2006-3-26 at 19:14 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 14 Posted 2006-05-11 13:37 ·  IANA 局域网IP(Private-Use)
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Awesome
Floor 15 Posted 2006-05-11 13:47 ·  IANA 局域网IP(Private-Use)
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Why is it so similar to Linux?
Forum Jump: