Open and run commands in simultaneous windows - windows

I have created a batch file to test IP addresses on the local network of the host. It looks to pull the operating systems of the other machines and write them to a text file.
FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.1 os get buildnumber,caption,CSDVersion /format:csv > C:\IP\IP.txt
FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.2 os get buildnumber,caption,CSDVersion /format:csv >> C:\IP\IP.txt
FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.3 os get buildnumber,caption,CSDVersion /format:csv >> C:\IP\IP.txt
(Note: I only show 3 lines here but the actual file runs through all 256 addresses.)
It runs through each line and writes to the file just fine. However if there is nothing at that IP, it takes 30 second to time out and move onto the next IP.
I would like to open a new cmd window for each line and run them simultaneously. Once I have this set up, I'd modify it to only run 10 at a time or something - but I need to reduce the total time it takes to run every line.
I have tried running them with CMD /c (as seen below) but it still seems to wait until each line is complete before moving to the next one. Is there a CMD parameter that I can add to make new window completely independent?
CMD /c "FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.1 os get buildnumber,caption,CSDVersion /format:csv > C:\IP\IP.txt"
CMD /c "FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.2 os get buildnumber,caption,CSDVersion /format:csv >> C:\IP\IP.txt"
CMD /c "FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.3 os get buildnumber,caption,CSDVersion /format:csv >> C:\IP\IP.txt"

Add START at te beginning of the line:
START CMD /c "FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.1 os get buildnumber,caption,CSDVersion /format:csv > C:\IP\IP.txt"
START CMD /c "FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.2 os get buildnumber,caption,CSDVersion /format:csv >> C:\IP\IP.txt"
START CMD /c "FOR /F "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %%g IN ("%%d") DO FOR /F "tokens=1 delims=." %%h IN ("%%g") DO FOR /F "tokens=2 delims=." %%i IN ("%%g") DO FOR /F "tokens=3 delims=." %%j IN ("%%g") DO WMIC /node:%%h.%%i.%%j.3 os get buildnumber,caption,CSDVersion /format:csv >> C:\IP\IP.txt"
...

You can use START command. You may want to escape the double quotes.
START "FOR /F ""tokens=1 delims=:"" %%d IN ('ping %computername% -4 -n 1 ^| find /i ""reply""') DO FOR /F ""tokens=3 delims= "" %%g IN (""%%d"") DO FOR /F ""tokens=1 delims=."" %%h IN (""%%g"") DO FOR /F ""tokens=2 delims=."" %%i IN (""%%g"") DO FOR /F ""tokens=3 delims=."" %%j IN (""%%g"") DO WMIC /node:%%h.%%i.%%j.1 os get buildnumber,caption,CSDVersion /format:csv > C:\IP\IP.txt"
To make it clean, put the command in a separate batch file and execute it.
START "Path\to\batch\file.bat"
There are more options available with START command. As you may very well know, they can be seen by simply giving the command START /?.

Related

Using cmd compact with folder name patterns

My folder structure is as below:
How could I use compact /s /c only on those folders whose name ends in "Holder" to compress all files recursively within those folders? Thanks
Windows 10 64-bit
Set folder and / or file compress attribute with compact and cmd for loop.
Set the compress attribute for every folder whose name ends with holder. Compress the files that are not system or hidden files in every folder whose folder name ends with holder. Stop if COMPACT encounters an error.
CMD:
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do COMPACT /c "%g"
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %h in ('DIR /a-d /b "%g"') do COMPACT /c "%g\%h"
rem un-set un-compress
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do COMPACT /u "%g"
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %h in ('DIR /a-d /b "%g"') do COMPACT /u "%g\%h"
SCRIPT:
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do ECHO COMPACT /c "%%g"
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do ECHO COMPACT /c "%%g\%%h"
rem un-set un-compress
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do ECHO COMPACT /u "%%g"
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do ECHO COMPACT /u "%%g\%%h"
TEST SCRIPT:
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do COMPACT /c "%%g"
echo.
echo Set compress attribute of folders whose name ends in holder.
echo.
pause
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do COMPACT /c "%%g\%%h"
echo.
echo Compress and set compress attribute of files in folders whose folder name ends in holder.
echo.
pause
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do COMPACT /u "%%g"
echo.
echo Unset compress attribute of folders whose name ends in holder.
echo.
pause
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do COMPACT /u "%%g\%%h"
echo.
echo Un-compress and un-set compress attribute of files in folders whose folder name ends with holder.
exit /b

How to add error code numbers to the output text file?

I have a batch script which loops through all windows errors and puts them in a text file called helplog2.txt. However, they are not separated by blank lines and the error messages do not have the error numbers before them.
Is there any way to fix that?
Here is my batch script:
#echo off
If Exist HelpLog2.txt Del HelpLog2.txt
(for /l %%i in (0,1,99999) do net helpmsg %%i 1>NUL 2>&1 && echo %%i & net helpmsg %%i | findstr /i "[a-z]" >> HelpLog2.txt)
Start "" HelpLog2.txt
pause
Use this batch code:
#echo off
del HelpLog2.txt 2>nul
for /L %%I in (0,1,9) do for /F delims^=^ eol^= %%J in ('%SystemRoot%\System32\net.exe helpmsg %%I 2^>^&1') do >>HelpLog2.txt echo %%I: %%J
for /L %%I in (10,1,99) do for /F delims^=^ eol^= %%J in ('%SystemRoot%\System32\net.exe helpmsg %%I 2^>^&1 ^| %SystemRoot%\System32\findstr.exe /V /C:"NET HELPMSG 3871"') do >>HelpLog2.txt echo %%I: %%J
for /L %%I in (100,1,999) do for /F delims^=^ eol^= %%J in ('%SystemRoot%\System32\net.exe helpmsg %%I 2^>^&1 ^| %SystemRoot%\System32\findstr.exe /V /C:"NET HELPMSG 3871"') do >>HelpLog2.txt echo %%I: %%J
for /L %%I in (1000,1,9999) do for /F delims^=^ eol^= %%J in ('%SystemRoot%\System32\net.exe helpmsg %%I 2^>^&1 ^| %SystemRoot%\System32\findstr.exe /V /C:"NET HELPMSG 3871"') do >>HelpLog2.txt echo %%I: %%J
for /L %%I in (10000,1,99999) do for /F delims^=^ eol^= %%J in ('%SystemRoot%\System32\net.exe helpmsg %%I 2^>^&1 ^| %SystemRoot%\System32\findstr.exe /V /C:"NET HELPMSG 3871"') do >>HelpLog2.txt echo %%I: %%J
start "" HelpLog2.txt
It takes several minutes to finish, but finally you should have the wanted information in well formatted file HelpLog2.txt.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
del /?
echo /?
findstr /?
for /?
net /?
net helpmsg /?

store part of outcome command to variable

Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]

Windows Batch file split string %%i was unexpected at this time

I'm trying to set few variables from systeminfo command in Windows. I read all kinds of similar threads and tried various things, but always keep getting the same error:
%%G was unexpected at this time.
or
%%i was unexpected at this time.
I've been trying to get this done all day long. Can someone point me in the right direction please? This is what I have tried so far:
for /f "tokens=:" %%i in ('systeminfo ^| grep "OS Name"') do set OSname=%%i
for /f "usebackq delims=: tokens=2" %%i in ('systeminfo ^| grep "OS Name"') do set OSname=%%i
FOR /F "usebackq delims= tokens=2" %%i IN ('systeminfo ^| grep "OS Name"') DO set vers=%%i
for /f "tokens=* delims= " %%a in ('systeminfo ^|grep "OS Name"') do (set OS_Name=%%a)
systeminfo | find "OS Name" > osname.txt
for /f "usebackq delims=: tokens=2" %%i in (osname.txt) do set osname=%%i
FOR /F %%G IN (FINDSTR /L /C:"OS Name" systeminfo.txt) DO ECHO %%G
UPDATE: I got it to work like this:
FOR /F "usebackq delims=: tokens=2" %i IN (`FINDSTR /L /C:"OS Name" systeminfo.txt`) DO set osname=%i
Any way to remove the leading space in front of the string? Thanks.
This is all you need to get the OS name.
for /f "tokens=2* delims=: " %%i in ('systeminfo ^| find "OS Name"') do set OSname=%%j

How do i get a specific line from an output using a batch file?

I have the code below that returns the hostname and IP address of a given server. How can i only have the hostname as the output?
FOR /F %%i in (servers.txt) do FOR /F "skip=3 delims=: tokens=2 usebackq" %%j in (`nslookup %%i`) do #echo %%j >> Devices_With_IP.txt
Assuming your machine uses English:
FOR /F %%i in (servers.txt) do FOR /F "delims=: tokens=2" %%j in (
'nslookup %%i ^| find "Name:"'
) do #echo %%j >> Devices_With_IP.txt
Try using the findstr command on the output of your nslookup to get only the line containing "Name".
FOR /F %%i in (servers.txt) do FOR /F "tokens=2 usebackq delims=: " %%j in (`nslookup %%i ^| findstr Name`) do #echo %%j >> Devices_With_IP.txt
Note that I also rearranged the /F conditions in the second loop in order to include space as a deliminator, this removes the leading spaces before the output.
Using the find command instead of findstr -
FOR /F %%i in (servers.txt) do FOR /F "tokens=2 usebackq delims=: " %%j in (`nslookup %%i ^| find "Name"`) do #echo %%j >> Devices_With_IP.txt
Just realized that using find instead of findstr makes this (almost) exactly the same as dbenham's answer.

Resources