I'm trying to execute a script that pulls a bunch of information for me. It was working until I removed my pause break points and re-introduced the IF NOT EXIST (which was working before I added the CPU get name and the part that pulls RAM info). Now the file will not run at all.
Using pause and echo, I have managed to find out that the script stops at IF NOT EXIST. If I add a pause between %location% and ( the script pauses then starts working from there. I have no idea why this is and I need this to run as silently as possible with NO interaction by the user.
REM Set save location
REM set location="\\colfs1\public\inventory\%computername%.txt"
SET location="c:\users\jpell\desktop\%computername%.txt"
REM If file is not there create it and populate info
pause
IF NOT EXIST %location% (
pause
systeminfo | findstr /i /b /c:"host name" /c:"os name" /c:"os version" /c:"original" /c:"system manu" /c:"system model" >> %location%
pause
#echo. >> %location%
pause
wmic /append:%location% bios get serialnumber
pause
wmic /append:%location% computersystem get systemtype
pause
wmic /append:%location% cpu get name
pause
REM Script to pull RAM info taken from StackOverflow
Set "WMIC_TOTMEM=wmic ComputerSystem get TotalPhysicalMemory /format:Value"
Set "WMIC_Capacity=wmic memorychip get capacity /format:Value"
Set "CAP=Capacity"
Set "TOT=TotalPhysicalMemory"
Call :GetTOTMEM %TOT% TotalPhysicalMemory
Call :GetCapacityMem %CAP% Capacity
Call :Convert %TotalPhysicalMemory% TotalPhysicalMemory_Converted
Call :Convert %Capacity% Capacity_Converted
echo -------------------------------------------------
echo TotalPhysicalMemory = %TotalPhysicalMemory%
echo Memorychip Capacity = %Capacity%
echo -------------------Converted---------------------
echo TotalPhysicalMemory = %TotalPhysicalMemory_Converted% >> %location%
echo Memorychip Capacity = %Capacity_Converted% >> %location%
echo -------------------------------------------------
GOTO :Continue
pause
::-------------------------------------------------------
:GetCapacityMem
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_Capacity% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::-------------------------------------------------------
:GetTOTMEM
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_TOTMEM% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::-------------------------------------------------------
:Convert
Set "VBS=%Temp%\%Random%.vbs"
(
echo wscript.echo Convert("%~1"^)
echo 'Function to format a number into typical size scales
echo Function Convert(iSize^)
echo aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo For i = 0 to 4
echo If iSize ^> 1024 Then
echo iSize = iSize / 1024
echo Else
echo Exit For
echo End If
echo Next
echo Convert = Round(iSize,2^) ^& " " ^& aLabel(i^)
echo End Function
)>"%VBS%"
for /f "delims=" %%a in ('Cscript //NoLogo "%VBS%"') do set "%2=%%a"
Del "%VBS%"
Exit /b
::-------------------------------------------------------
REM Continue the script after pulling RAM
:Continue:
#echo. >> %location%
#echo Current User: >> %location%
whoami >> %location%
#echo. >> %location%
#echo Forticlient or Sophos: >> %location%
wmic product get version,vendor | findstr /i /c:"forti" /c:"sophos" >> %location%
pause
)
pause
This script pulls a bunch of hardware information from a computer and puts it into a text file. This file is then used for inventory purposes. The information is populating with the pause in there and when the IF NOT EXIST is REMed out.
Yes, follow your own comment and reverse the logic:
if exist "%location%" goto :Jumpover
to avoid :labels and pseudo labels ::-- inside (code blocks)
All your pause commands for debugging could also be put in a variable %dbg%
and switched on/off on demand to not have to change much of the code.
Untested:
REM Set save location
REM set location="\\colfs1\public\inventory\%computername%.txt"
SET location="c:\users\jpell\desktop\%computername%.txt"
REM If file is not there create it and populate info
set "dbg=pause"
::set "dbg=Rem"
%dbg%
IF EXIST "%location%" goto :JumpOver
%dbg%
systeminfo | findstr /i /b /c:"host name" /c:"os name" /c:"os version" /c:"original" /c:"system manu" /c:"system model" >> %location%
%dbg%
#echo. >> %location%
%dbg%
wmic /append:%location% bios get serialnumber
%dbg%
wmic /append:%location% computersystem get systemtype
%dbg%
wmic /append:%location% cpu get name
%dbg%
REM Script to pull RAM info taken from StackOverflow
Set "WMIC_TOTMEM=wmic ComputerSystem get TotalPhysicalMemory /format:Value"
Set "WMIC_Capacity=wmic memorychip get capacity /format:Value"
Set "CAP=Capacity"
Set "TOT=TotalPhysicalMemory"
Call :GetTOTMEM %TOT% TotalPhysicalMemory
Call :GetCapacityMem %CAP% Capacity
Call :Convert %TotalPhysicalMemory% TotalPhysicalMemory_Converted
Call :Convert %Capacity% Capacity_Converted
echo -------------------------------------------------
echo TotalPhysicalMemory = %TotalPhysicalMemory%
echo Memorychip Capacity = %Capacity%
echo -------------------Converted---------------------
echo TotalPhysicalMemory = %TotalPhysicalMemory_Converted% >> %location%
echo Memorychip Capacity = %Capacity_Converted% >> %location%
echo -------------------------------------------------
GOTO :Continue
%dbg%
::-------------------------------------------------------
:GetCapacityMem
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_Capacity% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::-------------------------------------------------------
:GetTOTMEM
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_TOTMEM% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::-------------------------------------------------------
:Convert
Set "VBS=%Temp%\%Random%.vbs"
(
echo wscript.echo Convert("%~1"^)
echo 'Function to format a number into typical size scales
echo Function Convert(iSize^)
echo aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo For i = 0 to 4
echo If iSize ^> 1024 Then
echo iSize = iSize / 1024
echo Else
echo Exit For
echo End If
echo Next
echo Convert = Round(iSize,2^) ^& " " ^& aLabel(i^)
echo End Function
)>"%VBS%"
for /f "delims=" %%a in ('Cscript //NoLogo "%VBS%"') do set "%2=%%a"
Del "%VBS%"
Exit /b
::-------------------------------------------------------
REM Continue the script after pulling RAM
:Continue:
#echo. >> %location%
#echo Current User: >> %location%
whoami >> %location%
#echo. >> %location%
#echo Forticlient or Sophos: >> %location%
wmic product get version,vendor | findstr /i /c:"forti" /c:"sophos" >> %location%
%dbg%
:JumpOver
%dbg%```
Related
I am having an issue with a script. I will post it.
It runs fine till it gets to this line:
wmic diskdrive list full | findstr /B "Size=" >> %cd%ProductCodes\%Serial%\PCINFO.txt
This is what it sends to the file...
楓敺ㄽ㐲ㄱ㈱㤴㈹ര楓敺㈽㘵㔰㤲㘶〴ര
Any help would be appreciated
set drive=%cd:~0,3%
#echo off
for /F "skip=2 tokens=2 delims=," %%A in ('wmic systemenclosure get serialnumber /FORMAT:csv') do (set "serial=%%A")
mkdir %cd%ProductCodes\%Serial%
wmic path softwarelicensingservice get OA3xOriginalProductKey >> %cd%ProductCodes\%Serial%\Code.txt
wmic computersystem get model,manufacturer >> %cd%ProductCodes\%Serial%\PCINFO.txt
wmic cpu get name >> %cd%ProductCodes\%Serial%\PCINFO.txt
wmic ComputerSystem get TotalPhysicalMemory >> %cd%ProductCodes\%Serial%\PCINFO.txt
wmic diskdrive list full | findstr /B "Size=" >> %cd%ProductCodes\%Serial%\PCINFO.txt
cls
More %cd%ProductCodes\%Serial%\Code.txt
ECHO Open Windows Authentication?
Echo Y/N
SET /P M=Make Choice then press ENTER:
IF %M%==N GOTO :message
IF %M%==n GOTO :message
IF %M%==y GOTO :Start
IF %M%==Y GOTO :Start
:Start
start "C:\Windows\System32" slui.exe 3
pause
:message
CLS
ECHO Done!!
pause
Here is the requested output I get
Manufacturer Model
HP HP ZBook 15 G5
Name
Intel(R) Core(TM) i5-8300H CPU # 2.30GHz
TotalPhysicalMemory
16977272832
楓敺ㄽ㐲ㄱ㈱㤴㈹ര楓敺㈽㘵㔰㤲㘶〴ര
Have a look at this. I made quite a few changes, especially the way you accessed the format of wmic output formats. I also used choice instead of a bunch of if statements.
#echo off
for /f "tokens=2 delims==" %%A in ('wmic systemenclosure get serialnumber /FORMAT:list') do mkdir "%~dp0\ProductCodes\%Serial%" >nul 2>&1
for /f "tokens=2delims==" %%i in ('wmic path softwarelicensingservice get OA3xOriginalProductKey /format:list') do (echo %%i)>"%~dp0\ProductCodes\%Serial%\code.txt"
(wmic computersystem get model,manufacturer
wmic cpu get name
wmic ComputerSystem get TotalPhysicalMemory
wmic diskdrive get size
)> "%~dp0\ProductCodes\%Serial%\PCINFO.txt"
cls
More "ProductCodes\%Serial%\code.txt"
ECHO Open Windows Authentication?
choice /m "Open Authentication?"
if errorlevel 2 goto :message
goto :eof
:Start
start "C:\Windows\System32" slui.exe 3
pause
:message
cls
ECHO Done!!
pause
Side note on your original if %m%==y statements. You can use if /i to make the match case insensitive.. in other words, if /i "y"=="Y" will be true as well, but no longer relevant here if using choice.
You can give a try for this modification with piping the command More since the output of WMIC is UNICODE
#echo off
#for /F "skip=2 tokens=2 delims=," %%A in (
'wmic systemenclosure get serialnumber /FORMAT:csv'
) do (set "serial=%%A")
Set "LogFolder=%cd%ProductCodes\%Serial%"
MkDir "%LogFolder%">nul 2>&1
Set "Code=%LogFolder%\Code.txt"
Set "PCINFO=%LogFolder%\PCINFO.txt"
wmic path softwarelicensingservice get OA3xOriginalProductKey>"%code%"
(
wmic computersystem get model,manufacturer
wmic cpu get name
wmic ComputerSystem get TotalPhysicalMemory
wmic diskdrive list full ^| findstr /B "Size="
) | More>"%PCINFO%"
More "%Code%"
ECHO Open Windows Authentication?
Echo Y/N
SET /P M=Make Choice then press ENTER:
IF /I %M%==N GOTO :message
IF /I %M%==Y GOTO :Start
:Start
start "C:\Windows\System32" slui.exe 3
pause
:message
CLS
ECHO Done!!
pause
I am attempting to create a basic script that runs and checks for basic system info but I want the output to be formatted so the results are on the same line and easily readable.
#Echo Off
systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
wmic cpu get name, status
systeminfo | findstr /C:"Total Physical Memory"
#echo off & setlocal ENABLEDELAYEDEXPANSION
SET "volume=C:"
FOR /f "tokens=1*delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
SET "diskfree=!disktotal!"
SET "disktotal=!diskavail!"
SET "diskavail=%%j"
)
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
ECHO(Total Space: %disktotal:~0,-9% GB
ECHO(Available Space: %diskavail:~0,-9% GB
systeminfo | find "System Boot Time:"
systeminfo | find "System Type:"
Echo Antivirus: & wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName | findstr /V /B /C:displayName || echo No Antivirus installed
The main example of this would be the wmic command placing the result on the next line rather than the same line.
Also any tips of better ways of scripting what I currently have would be appreciated.
#echo off
setlocal
for /f "tokens=1,* delims=:" %%A in ('systeminfo') do (
if "%%~A" == "OS Name" (
call :print "%%~A" %%B
) else if "%%~A" == "OS Version" (
call :print "%%~A" %%B
call :cpu
) else if "%%~A" == "Total Physical Memory" (
call :print "%%~A" %%B
call :fsutil C:
) else if "%%~A" == "System Boot Time" (
call :print "%%~A" %%B
) else if "%%~A" == "System Type" (
call :print "%%~A" %%B
)
)
:next
call :antivirus
pause
exit /b
:print
setlocal enabledelayedexpansion
set "name=%~1"
if not defined name exit /b
set "name=%~1 "
set "full=%*"
set "full=!full:,=!"
set "data="
set "skip1="
for %%A in (!full!) do (
if not defined skip1 (
set "skip1=defined"
) else if not defined data (
set "data=%%~A"
) else (
set "data=!data! %%~A"
)
)
echo !name:~,30!: !data!
exit /b
:antivirus
setlocal enabledelayedexpansion
set "antivirus="
for /f "tokens=*" %%A in ('
2^>nul wmic /node:localhost
/namespace:\\root\SecurityCenter2 path AntiVirusProduct
Get DisplayName /value
^| findstr /i /b /c:"DisplayName" ^|^| echo No Antivirus installed
') do set "antivirus=%%~A"
if /i "!antivirus:~,12!" == "DisplayName=" set "antivirus=!antivirus:~12!"
call :print Antivirus "!antivirus!"
exit /b
:cpu
for /f "tokens=1,* delims==" %%A in ('wmic cpu get name^, status /value') do (
call :print "%%~A" %%B
)
exit /b
:fsutil
setlocal enabledelayedexpansion
2>nul >nul net session || exit /b
for /f "tokens=1,* delims=:" %%A in ('2^>nul fsutil volume diskfree %1') do (
set "name=%%~A"
set "value=%%~B"
call :print "!name:bytes=GBs!" !value:~,-9!
)
exit /b
The fsutil command outputs alittle different.
I chose an easy option if it is OK.
Unsure of antivirus output as you need both results of
installed or not to be sure.
It only runs systeminfo once to save time.
Look at set /? for information about substitution that
is used.
The net session command is used to test if script is run as Admin.
If not Admin, then fsutil will be skipped as it requires Admin.
Look at for /? for usage of the command that is used in the script.
Use of enabledelayedexpansion is used to prevent special
characters being exposed which may cause error otherwise.
And used in code blocks to delay expansion as needed.
Remove comma from value strings which get replaced with a space.
A comma is often used as thousands separator and numbers look odd
with a space instead. This occurs because of usage of a simple
for loop interpreting comma as a argument separator. Some other
chararters may also do this though perhaps a space maybe better
than none in their case.
The output of WMIC is unicode !
The trailing <CR> can be removed by passing the value through another FOR /F loop. This also removes the phantom "blank" line (actually a <CR>)
#Echo Off
Call :GET_CPU name CPU_Name
Set "WMIC_Antivirus=wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName ^| findstr /V /B /C:displayName"
#For /F "delims=" %%I in ('%WMIC_Antivirus%') do (
for /f "delims=" %%A IN ("%%I") DO SET "Antivirus=%%A"
)
echo CPU : %CPU_Name%
echo Antivirus : %Antivirus%
pause & exit
::----------------------------------------------------
:GET_CPU
Set "WMIC_CPU=wmic cpu get name /Value"
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_CPU% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------
You can try to save the output into a text file :
#Echo Off
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO **************************************
ECHO Running Admin shell... Please wait...
ECHO **************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
SetLocal EnableDelayedExpansion
set "Log=%~dp0Log.txt"
If exist "%Log%" Del "%Log%"
Call :GET_CPU name CPU_Name
Set "WMIC_Antivirus=wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName ^| findstr /V /B /C:displayName"
#For /F "delims=" %%I in ('%WMIC_Antivirus%') do (
for /f "delims=" %%A IN ("%%I") DO SET "Antivirus=%%A"
)
(
Echo.
Echo ***************************** General infos ***********************************
Echo.
Echo Running under: %username% on profile: %userprofile%
Echo Computer name: %computername%
Echo.
systeminfo
Echo Operating System:
Echo PROCESSOR ARCHITECTURE : %PROCESSOR_ARCHITECTURE%
echo NUMBER_OF_PROCESSORS : %NUMBER_OF_PROCESSORS%
echo PROCESSOR_IDENTIFIER : %PROCESSOR_IDENTIFIER%
echo PROCESSOR_LEVEL : %PROCESSOR_LEVEL%
echo PROCESSOR_REVISION : %PROCESSOR_REVISION%
echo OS TYPE : %OS%
echo(
echo Program files path : %Programfiles%
echo Program files(86^) path : %Programfiles(86^)%
echo ProgramW6432 path : %ProgramW6432%
echo PSModulePath : %PSModulePath%
echo SystemRoot : %SystemRoot%
echo Temp Folder : %Temp%
echo CPU : !CPU_Name!
echo Antivirus : !Antivirus!
Echo.
Echo **************************** Drives infos *************************************
Echo.
Echo Listing currently attached drives:
wmic logicaldisk get caption,description,volumename | find /v ""
Echo.
Echo Physical drives information:
for /F "tokens=1-3" %%A in ('fltmc volumes^|find ":"') do echo %%A %%B %%C
)>>"%Log%"
Start "" "%Log%" & exit
::----------------------------------------------------
:GET_CPU
Set "WMIC_CPU=wmic cpu get name /Value"
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_CPU% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------
#echo off
set a=2,113 MB
for /f "tokens=2* delims=:" %%a in ('systeminfo ^| findstr /I /C:"Total Physical Memory"') do set r=%%a
echo available RAM IS %r%
pause
if "%r%" gtr "%a%"
(
echo "proceed"
)
else (
echo "no"
)
pause
This is just another way to check ram do the required if conditions are met.
#echo off
::set required memory size 1024=1GB | 2048=2GB | 3072=3GB | 4096=4GB ||
set reqramsize=1024
for /f "tokens=4" %%a in ('systeminfo ^| findstr Physical') do if defined totalMem (set availableMem=%%a) else (set totalMem=%%a)
set totalMem=%totalMem:,=%
set availableMem=%availableMem:,=%
set /a usedMem=totalMem-availableMem
:: Output Your Memory Variables
echo Total Memory: %totalMem%
echo Used Memory: %usedMem%
::Check Condition with Used Memory & Required Memory
if %usedMem% GTR %reqramsize% (goto available) else goto navailable
:available
echo ram available - Proceed
pause>nul
:navailable
echo ram not available - Unable to Proceed
pause>nul
exit
I'm pretty new to this so please bear with me, and if you require anymore information from me please say. Thanks in advance for your help.
I have this code that pings different PCs then returns back if they are online/offline. I wanted to know if you could add another column once the bat file has ran its ping test so it has the computer name next to it.
#echo off
if exist C:\tools\computers.txt goto Label1
echo.
echo Cannot find C:\tools\computers.txt
echo.
Pause
goto :eof
:Label1
echo PingTest executed on %date% at %time% > C:\tools\z.txt
echo ================================================= >> C:\tools\z.txt
for /f %%i in (C:\tools\computers.txt) do call :Sub %%i notepad C:\tools\z.txt
goto :eof
:Sub
echo Testing %1 set state=alive ping -n 1 %1 | find /i "bytes=" || set state=dead echo %1 is %state% >> C:\tools\z.txt
The bat file creates a document that shows the following;
PingTest executed on 28/07/2016 at 13:10:28
99.1.82.28 is alive
99.1.82.100 is alive
ect.
If possible I would like the bat file to run so it displays this;
The bat file creates a document that shows the following;
PingTest executed on 28/07/2016 at 13:10:28
Computer 1 : 99.1.82.28 is alive
Computer 2 : 99.1.82.100 is alive
ect.
--
Would appreciate any help & guidance on this.
Thanks.
You can try this solution :
#echo off
Title Ping Test
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
(
echo ******************************************************
echo PingTest executed on %Date% # Time %Time%
echo ******************************************************
echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
ping -n 1 %%a>nul && set "msg=%%a : !ip! ALive ok" || set "msg=%%a : !ip! Dead failed to respond"
echo !msg!
echo !msg! >> %LogFile%
)
)
EndLocal
Start "" %LogFile%
pause>nul & exit
EDIT : on 29/07/2016 # 12:48
Another version with multi-colors : Special thanks goes to ICARUS for the color function (-_°)
#echo off
Rem Special thanks goes to Iracus for the color function (-_°)
mode con cols=60 lines=20
Title Multi-Ping hosts Tester with Multi-colors by Hackoo
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
call :init
echo(
call :color 0E "------- Ping Status of Computers hosts -------" 1
echo(
(
echo ******************************************************
echo PingTest executed on %Date% # Time %Time%
echo ******************************************************
echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
ping -n 1 %%a>nul && set "msg=%%a - !ip! ALive ok" && Call :Color 0A "!msg!" 1 || set "msg=%%a - !ip! Dead failed to respond" && Call :Color 0C "!msg!" 1
echo !msg! >> %LogFile%
)
)
EndLocal
Start "" %LogFile%
pause>nul & exit
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
<nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
EDIT : Update On 23/08/2016
http://pastebin.com/zjYwSqUM
i need batch file to mask the input with * without external file and i need the code to be fast to write letters
FOR EXAMPLE:
#echo off
set char=*
set /p variable=Enter your password:
set char=%variable%
echo %char%
pause
Here is a way to do it using Powershell in a batch file
#echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%
Powershell is installed by default on all newer OS's so it's a good compromise. If you have to support XP machines, you can do something similar with VBScript.
Yes! There is a way in pure batch, with misusing of xcopy and prompt command we can do it
#echo off
setlocal enabledelayedexpansion
call:show "Enter your passwordÿ" "0a"
call:hidePass
set /p password=<_tmp&del _tmp&echo/
echo/%password%
pause>nul
exit
:hidePass
set "str="
set "chars="
:writing
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined key set "key=%%L"
)
setlocal EnableDelayedExpansion
set "key=!key:~-1!"
if "%key%" == "" (echo/!str!>_tmp&exit/b)
call:show "-" "0c"
set "str=!str!!key!"
goto :writing
:show
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
call :ColorText %~2 "%~1"
exit/b
:ColorText
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
exit/b
The only problem is you can not use * as mask, in this case I'm using -