1

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

3 Answers3

2

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*).

Stephan
  • 53,940
  • 10
  • 58
  • 91
1

The problem is that your commands are seperated by &&. In this instance, the first does not have redirect to file, only the second, so what actually happens is this:

date /T
&&
time /T>output.txt

Where you really wanted:

(date /T && time /T)>output.txt

The problem here is they are 2 different commands, so they will be placed on 2 seperate lines:

2019/09/20
2:22:23 PM

Instead either do:

echo %date% %time%>C:\TimsFolder2\networkInfo\networkInfo.txt

That said, for echoing many things to the same file, this is even cleaner:

(echo %date% %time%
net users
net accounts
) >> C:\TimsFolder2\groupInfo\groupInfo.txt

So everything in your file can be re-written as that, here are some of them as a demo:

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

::User Info Function
:userInfo
(echo %date% %time%
 hostnam
 whoami
)> C:\TimsFolder2\userInfo\userInfo.txt
....
...
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

I have decided to post this rewritten yet untested example of how I might attempt this:

@Echo Off
CD /D "C:\TimsFolder2" 2>NUL||Exit /B
For %%A In (compInfo,groupInfo,networkInfo,userInfo) Do (If Exist "%%~A\" (
    Echo %%~A is present) Else MD "%%~A" && Echo %%~A directory created
  Call :%%A>>"%%A\%%A.txt")

:fileSystem
Forfiles

Pause
GoTo :EOF

:compInfo
Date /T
Time /T
Ver
FSUtil FSInfo Drives
WMIC OS Get /Format:List
WMIC BIOS Get /Format:List
WMIC BootConfig Get /Format:List
WMIC CPU Get /Format:List
WMIC MemPhysical Get /Format:List
Exit /B 0

:userInfo
Date /T
Time /T
HostName
WhoAmI
Exit /B 0

:groupInfo
Date /T
Time /T
Net Users
Net Accounts
GPResult /R
Exit /B 0

:networkInfo
Date /T
Time /T
Route Print
NetSh Interface IPv4 Show Addresses
GetMAC
ARP /A
Exit /B 0

I have not tested or checked the information listed under each of your Called labels, although I did notice at the bottom ForFiles which I doubt on its own will actually output much if anything.

Compo
  • 36,585
  • 5
  • 27
  • 39