首页 > 解决方案 > Batch Script that is printing date to the screen but should be redirected to a file

问题描述

I've written a simple batch script that redirects the output of the command to a file, however, I'm still seeing the date output in the terminal screen. Each Function Starts with a colon, double colons are comments. At the end of the script I call each Function and lastly I'm utilizing the builtin pause command. Anyone know why I'm seeing just one single date output?

@echo off
::navigate to correct folder
cd C:\TimsFolder2

::verify directories are created
if exist C:\TimsFolder2\compInfo (ECHO TimsFolder2 is present)^
 Else (mkdir compInfo && echo "TimsFolder2^\compInfo directory created") 
if exist C:\TimsFolder2\groupInfo (ECHO TimsFolder2 is present)^
 Else (mkdir groupInfo && echo TimsFolder2^\groupInfo created) 
if exist C:\TimsFolder2\networkInfo (ECHO TimsFolder2 is present)^
 Else (mkdir networkInfo && echo TimsFolder2^\networkInfo created) 
if exist C:\TimsFolder2\userInfo (ECHO TimsFolder2 is present)^
 Else (mkdir userInfo && echo TimsFolder2^\userInfo) 

::system Info Function
:sysInfo
date /t && time /T > C:\TimsFolder2\compInfo\compInfo.txt
ver >> C:\TimsFolder2\compInfo\compInfo.txt
fsutil fsinfo drives >> C:\TimsFolder2\compInfo\compInfo.txt
wmic OS get /format:list >> C:\TimsFolder2\compInfo\compInfo.txt
wmic BIOS get /format:list >> C:\TimsFolder2\compInfo\compInfo.txt
wmic BOOTCONFIG get /format:list >> C:\TimsFolder2\compInfo\compInfo.txt
wmic CPU get /format:list >> C:\TimsFolder2\compInfo\compInfo.txt
wmic memphysical get /format:list >> C:\TimsFolder2\compInfo\compInfo.txt
EXIT /B 0

::User Info Function
:userInfo
date /t && time /T > C:\TimsFolder2\userInfo\userInfo.txt
hostname >> C:\TimsFolder2\userInfo\userInfo\userInfo.txt
whoami >> C:\TimsFolder2\userInfo\userInfo.txt
EXIT /B 0

::Group Info Function
:groupInfo
date /t && time /T > C:\TimsFolder2\groupInfo\groupInfo.txt
net users >> C:\TimsFolder2\groupInfo\groupInfo.txt 
net accounts >> C:\TimsFolder2\groupInfo\groupInfo.txt
gpresult /R >> C:\TimsFolder2\groupInfo\groupInfo.txt
EXIT /B 0

::NetworkInfo Info Function
:networkInfo
date /t && time /T > C:\TimsFolder2\networkInfo\networkInfo.txt
route print >> C:\TimsFolder2\networkInfo\networkInfo.txt
netsh interface ipv4 show addresses >> 
C:\TimsFolder2\networkInfo\networkInfo.txt
getmac >> C:\TimsFolder2\networkInfo\networkInfo.txt
arp -a >> C:\TimsFolder2\networkInfo\networkInfo.txt
EXIT /B 0

::File System
:fileSystem
cd C:\TimsFolder2
forfiles
EXIT /B 0

call : sysInfo
call : userInfo
call : groupInfo
call : networkInfo
call : fileSystem

pause

Batch Script Output

标签: batch-filecmd

解决方案


Besides the issue, Gerhard already answered, you have two more problems:

First:

call : sysInfo

tries to call a subroutine with no name : (which will give you a syntax error) with a parameter sysInfo. Not what you want. You want:

call :sysInfo

Second:

Batch has no such concept as a Function (like in other / true languages). Code is simply run line by line (except there is a GOTO (which jumps to the corresponding label) or a CALL (which remembers where it comes from and then jumps to the corresponding label and only jumps back if it reaches the last line of the script or a GOTO :eof (which is essentially "goto the very end of the script")).

So you have to "close" each of your "Functions" (I personally like the term "Subroutine" better, but technically, they are both wrong) with a goto :eof (which you already did) and add another goto :eof before the first subroutine to prevent the code to execute the following subroutine (remember: line-by-line-exectuion) after the "main code" (when it's expected to stop) (*which you forgot*).


推荐阅读