batch file to mask input with * without an external file - file

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 -

Related

How do I make those two codes work in one code?

I'm working for days to get code for coloring text,
I get this one which work perfectly:
#Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
call :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
But I don't know how to get it work with mine:
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
set mydate=%%c%%b%%a
set myyear=%%c
set mymonth=%%b
)
mkdir "C:\TEST\%myyear%\%mymonth%\%mydate%\"
for /d %%X in (*) do "C:\Program Files\WinRAR\WinRAR.exe" a -u -r -ed -m5 -agYYYYMMDD "C:\TEST\%myyear%\%mymonth%\%mydate%\Test_%%X_.rar" "C:\DATA\%%X\*.*"
How do I make a variable %%X (Which is a folder to zip) in my code colored?
Thanks

How to pass argument in batch file

I am trying to make a path directory generic for all users using batch file. The code generates a popup and it asks the user to input the folder. The directory is echoed on the terminal. I am trying to pass the this output directory as an argument in another function 'folder' so that the application starts running. But I am not able to pass the argument.
#echo off
Title Browse4Folder
Color 0A
Call :Browse4Folder "Choose source folder to scan" "c:\scripts"
echo You have chosen this location "%Location%"
pause & exit
::***************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
echo set shell=WScript.CreateObject("Shell.Application"^)
echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^)
echo if typename(f^)="Nothing" Then
echo wscript.echo "set Location=Dialog Cancelled"
echo WScript.Quit(1^)
echo end if
echo set fs=f.Items(^):set fi=fs.Item(^)
echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
PAUSE
Call :Folder Location
:Folder
Set T32_PATH=%Location%
ECHO T32_PATH=%Location%
cd ..\Appl
START %Location%\t32mtc.exe -c ..\cm3550a_vecm\vlab_mcd.t32 -s ..\cm3550a_vecm\vlab_mcd_config.cmm
ECHO T32_PATH=%Location%
PAUSE
I have to pass the argument to line START %Location%\t32mtc.exe -c ..\cm3550a_vecm\vlab_mcd.t32 -s ..\cm3550a_vecm\vlab_mcd_config.cmm How shall I do it.
To use a GUI dialog box for the end user to provide their T32 directory, I'd choose to leverage either PowerShell, JScript, or (your chosen) VBScript.
Here's a quick and basic example of each, (none of which write to files):
batch-file leveraging powershell:
#Set "T32_PATH="
#For /F Delims^=^ EOL^= %%G In ('^"
"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile ^
"(New-Object -COM 'Shell.Application').BrowseForFolder(0,'Select your T32 directory.',1,0).Self.Path"
^"') Do #If Exist "%%G\t32mtc.exe" Set "T32_PATH=%%G"
#If Defined T32_PATH Start "" "%T32_PATH%\t32mtc.exe" …
batch-file leveraging jscript:
0</* :
#Set "T32_PATH="
#For /F "Delims=" %%G In (
'""%SystemRoot%\System32\cscript.exe" //E:JScript //NoLogo "%~f0" 2>Nul|Find /V """'
) Do #If Exist "%%G\t32mtc.exe" Set "T32_PATH=%%G"
#If Defined T32_PATH Start "" "%T32_PATH%\t32mtc.exe" …
#Exit /B */0;
var Folder=new ActiveXObject('Shell.Application').BrowseForFolder(0,'Select your T32 directory.',1,'0');
try{new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Folder.Self.Path)};catch(e){};close();
batch-file leveraging vbscript:
<!-- :
#Set "T32_PATH="
#For /F "Delims=" %%G In (
'"%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" 2>Nul|Find /V """'
) Do #If Exist "%%G\t32mtc.exe" Set "T32_PATH=%%G"
#If Defined T32_PATH Start "" "%T32_PATH%\t32mtc.exe" …
#Exit /B
-->
<Job><Script Language="VBScript">
Set objFolder = CreateObject("Shell.Application").BrowseForFolder(0,"Select your T32 directory.",1,0)
WScript.Echo objFolder.Self.Path
</Script></Job>
To hopefully better show you that the methodology doesn't change, here's an example which uses something more like the structure of your posted example.
<!-- :
#Echo Off
SetLocal EnableExtensions DisabledelayedExpansion
Title Browse4Folder
Color 0A
:GetT32Parent
Set "T32_FILE=t32mtc.exe"
Set "T32_PATH="
Call :Browse4Folder "Select your T32 directory." "C:\scripts" "T32_PATH"
If Not Defined T32_PATH (Echo Error: Please try again.
Pause
GoTo GetT32Parent)
If Not Exist "%T32_PATH%\%T32_FILE%" (
Echo The chosen directory does not contain %T32_FILE%.
Echo Error: Please try again.
Pause
GoTo GetT32Parent)
Echo You have chosen this location "%T32_PATH%"
Pause
Rem Your T32 based code goes here.
Start "" "%T32_PATH%\%T32_FILE%" …
Rem Any other code goes here.
Exit /B
:Browse4Folder
If "%~3" == "" (Set "sVariable=location") Else Set "sVariable=%~3"
If "%~2" == "" (Set "vRootFolder=%CD%") Else Set "vRootFolder=%~2"
If "%~1" == "" (Set "sTitle=Please select your directory.") Else Set "sTitle=%~1"
Set "%sVariable%="
For /F "Delims=" %%G In ('%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf"
"%sTitle%" "%vRootFolder%" 2^>Nul') Do Set "%sVariable%=%%G"
GoTo :EOF
-->
<Job><Script Language="VBScript">
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0,WScript.Arguments.Item(0),0,WScript.Arguments.Item(1))
WScript.Echo objFolder.Self.Path
</Script></Job>

Undetectable issue with large multi-color batch ascii art

I'm having a strange issue where some large multi-colored ascii art will display irregularly, either only displaying the bottom 1/5 or so of the art, or the top 9 lines of the art if they are commented and un-commented.
The following batch code is being used to multi-color stuff.
:c
setlocal enableDelayedExpansion
:colorPrint Color Str [/n]
setlocal
set "s=%~2"
call :colorPrintVar %1 s %3
exit /b
:colorPrintVar Color StrVar [/n]
if not defined DEL call :initColorPrint
setlocal enableDelayedExpansion
pushd .
':
cd \
set "s=!%~2!"
for %%n in (^"^
^") do (
set "s=!s:\=%%~n\%%~n!"
set "s=!s:/=%%~n/%%~n!"
set "s=!s::=%%~n:%%~n!"
)
for /f delims^=^ eol^= %%s in ("!s!") do (
if "!" equ "" setlocal disableDelayedExpansion
if %%s==\ (
findstr /a:%~1 "." "\'" nul
<nul set /p "=%DEL%%DEL%%DEL%"
) else if %%s==/ (
findstr /a:%~1 "." "/.\'" nul
<nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%"
) else (
>colorPrint.txt (echo %%s\..\')
findstr /a:%~1 /f:colorPrint.txt "."
<nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
)
)
if /i "%~3"=="/n" echo(
popd
exit /b
:initColorPrint
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A"
<nul >"%cd%\'" set /p "=."
subst ': "%cd%" >nul
exit /b
:cleanupColorPrint
2>nul del "%cd%\'"
2>nul del "%cd%\colorPrint.txt"
>nul subst ': /d
exit /b
https://pastebin.com/Z1CKjwWR
but the real meat of the issue comes with trying to display this anime girl
https://pastebin.com/W0w1AtNN
Is anyone able to tell what causes the display issues, and why? None of my experimentation got me anywhere.

Scheduled batch file error (0xff) - Executing normal it work fine

I've this batch file to execute.
#echo off
set /a count=0
for /f "tokens=1delims=:" %%i in ('findstr /n "^" "foto1.txt"') do set /a count=%%i
set /a rd=%random%%%count
if %rd% equ 0 (set "skip=") else set "skip=skip=%rd%"
set "found="
for /f "%skip%tokens=1*delims=:" %%i in ('findstr /n "^" "foto1.txt"') do if not defined found set "found=%%i"&set "var=%%j"
echo.%var%
break > urlfoto1.txt
echo %var% >> urlfoto1.txt
set /a count=0
for /f "tokens=1delims=:" %%i in ('findstr /n "^" "foto2.txt"') do set /a count=%%i
set /a rd=%random%%%count
if %rd% equ 0 (set "skip=") else set "skip=skip=%rd%"
set "found="
for /f "%skip%tokens=1*delims=:" %%i in ('findstr /n "^" "foto2.txt"') do if not defined found set "found=%%i"&set "var=%%j"
echo.%var%
break > urlfoto2.txt
echo %var% >> urlfoto2.txt
set /a count=0
for /f "tokens=1delims=:" %%i in ('findstr /n "^" "foto3.txt"') do set /a count=%%i
set /a rd=%random%%%count
if %rd% equ 0 (set "skip=") else set "skip=skip=%rd%"
set "found="
for /f "%skip%tokens=1*delims=:" %%i in ('findstr /n "^" "foto3.txt"') do if not defined found set "found=%%i"&set "var=%%j"
echo.%var%
break > urlfoto3.txt
echo %var% >> urlfoto3.txt
This script is generating random jpg url from a list on a file and create a txt new file with 1 random jpg url.
When I exectute it manually, it work fine.
But when I go to scheduling it with Windows Task Scheduler I retrieve a (0xFF) ERROR.
What does (0xff) mean? And why the scheduling is not working?
And similarly to LotPings answer:
#Echo Off
CD /D "%~dp0"
For %%A In (1 2 3) Do Call :Sub "%%A"
Pause
GoTo :EOF
:Sub
For /F "Delims==" %%A In ('Set line[ 2^>Nul') Do Set "%%A="
Set "total="&Set "randlinenum="
For /F "Tokens=1*Delims=[]" %%A In ('Find /V /N ""^<"foto%~1.txt"'
)Do Set "line[%%A]=%%B"&Set "total=%%A"
Set /A randlinenum=1+(%RANDOM% %% total)
SetLocal EnableDelayedExpansion
Echo(!line[%randlinenum%]!
(Echo(!line[%randlinenum%]!)>"urlfoto%~1.txt"
EndLocal
Exit /B
Please note that the Find /V /N "", (or FindStr /N "^"), construct will also include any empty lines, so if those exist in any of your foto* files this could feasibly randomly select and output an empty line.
I don't like the redundancy in your batch, use a counting for /l and a call :sub
using a find /c is simpler to get the line count of a file.
using more +x avoids the need to differentiate the skip count.
when outputting only one line, directly use > to overwrite previous content.
Here using %~dp0 (batch folder) for input/output.
:: Q:\Test\2019\05\02\SO_55951454.cmd
#echo off
cd /d "%~dp0"
for /L %%L in (1,1,3) do Call :Sub %%L
Goto :Eof
:Sub
for /f %%i in ('find /C /V "" ^<"foto%1.txt"') do set count=%%i
set /a "rd=%random% %% count"
for /f "delims=" %%i in ('more +%rd% "foto%1.txt"') do set "var=%%i"&goto :cont
:cont
echo.%var%
>"urlfoto%1.txt" echo.%var%
Goto :Eof

Formatting results in .CMD

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
::----------------------------------------------------

Resources