I am sort of new to coding, and tried looking around. My goal is to remotely obtain Domain Controller from a machine on the network. The command I am using via batch is below. I want to filter the results from nltest to only display the DC Name. I don't want connection status, and flags.
:Start
color 02
cls
#echo off
Echo.
Echo Domain Controller Finder
Echo.
Set /P Computer=Enter the Asset ID:
If "%Computer%"== "" goto BadName
nltest /sc_query:<Domain_Name> /server:%Computer%
pause
Goto End
:BadName
Cls
Echo.
Echo You have entered an incorrect name or left this field blank
Echo Please enter a valid Name or press Ctr-C to exit.
Echo.
Pause
Goto Start
:End
I'm guessing that the DC Name begins with \\, so you could try this:
#ECHO OFF
:Begin
SET/P "AssetID=Enter the Asset ID: "
IF "%AssetID%"=="" GOTO BadName
SET "DCName="
FOR /F "TOKENS=1* DELIMS=\" %%A IN (
'nlest /sc_query:<Domain_Name> /server:%AssetID%^|FIND "\\"'
) DO SET "DCName=\\%%B"
IF DEFINED DCName (ECHO %%DCName%% = %DCName%
PAUSE & GOTO End)
:BadName
Note. I changed :Start to :Begin because Start is a command and looks wrong, the decision is yours.
Related
I'm trying to make a username change option for the settings in one of my batch file programs, but it keeps displaying a message like "set was unexpected at this time" which is weird, because my code seems right. It happens after entering a value for "cuser". None of the values I'm entering for input are null, since I've pre-declared the values at the beginning of the program to make it so I didn't have any null-value errors.
:uch
cls
echo.
echo Are you sure you want to change your username?
echo.
echo [Y/N]
echo.
set /p input=
if %input% EQU n goto set
if %input% NEQ y goto uch
:ucy
cls
echo.
echo Enter your current username
echo.
set /p cuser=
if %cuser% NEQ %username1% (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
echo.
pause>null
goto :ucy
)
if %cuser% EQU %username1% (
echo Please enter new username.
echo.
set /p nuser=
echo Please enter again.
echo.
set /p nuser2=
if %nuser2% EQU %nuser% set username1=%nuser%
if %nuser2% EQU %nuser% goto ga1
if %nuser2% NEQ %nuser% (
echo Usernames do not match. Please try again.
echo Press any button to continue.
echo.
pause>null
goto ucy
)
goto ucy
You are trying to set value of variable inside if block without delayed expansion and the if is parsed with wrong syntax.And you have one unclosed bracket...
:uch
cls
echo.
echo Are you sure you want to change your username?
echo.
echo [Y/N]
echo.
set /p input=
if %input% EQU n goto set
if %input% NEQ y goto uch
:ucy
cls
echo.
echo Enter your current username
echo.
set /p cuser=
if %cuser% NEQ %username1% (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
echo.
pause>null
goto :ucy
)
setlocal enableDelayedExpansion
if %cuser% EQU %username1% (
echo Please enter new username.
echo.
set /p nuser=
echo Please enter again.
echo.
set /p nuser2=
if !nuser2! EQU !nuser! set username1=!nuser!
if !nuser2! EQU !nuser! goto ga1
if !nuser2! NEQ !nuser! (
echo Usernames do not match. Please try again.
echo Press any button to continue.
echo.
pause>null
goto ucy
)
goto ucy
You are not using the proper syntax for the if command. When comparing strings, use
if "%var1%"=="%var2%" to compare. EQL NEQ etc. are for numeric comparison.
You DO NOT need to have multiple if statements (one for yes and one for no) because, you can assume that if they didn't say yes then they meant to say no. Here is an improved script for you, one that doesn't need delayed expansion in order to work. Hope this helps.
:uch
cls
echo.
echo Are you sure you want to change your username?
set /p input=[Y/N]
if not "%input%"=="y" (goto ga1)
::THEY WANT TO CHANGE THEIR USERNAME
cls
echo.
set /p cuser=Enter your current username ^>
if not "%cuser%"=="%username1%" (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
pause>NUL
goto :uch
)
::THE USERNAME MATCHES
echo.
set /p nuser=Please enter new username. ^>
echo.
set /p nuser2=Please enter again. ^>
if "%nuser2%"=="%nuser%" (set username1=%nuser% && goto ga1)
echo Usernames do not match. Please try again.
echo Press any button to continue.
echo.
pause>NUL
goto uch
:ga1
:://DO SOME OTHER STUFF HERE AFTER THEY CHANGED THEIR NAME OR OPTED NOT TO.
I am making a login system for my OS. And there is a error i dont know how to fix it
#echo off
color 02
IF NOT EXIST Users md Users
:start
:menu
cls
echo ZEROS 0.2.1 LOGIN
echo.
echo [1]-Sign in
echo [2]-Register
echo [3]-User List
echo [4]-Exit
echo.
set /p input=Your choice:
if %input%==1 goto log
if %input%==2 goto reg
if %input%==3 goto ucheck
if %input%==4 exit
echo Invalid Choice
pause
goto menu
:reg
cls
color 02
echo ZEROS 0.2.1 REGISTER
echo.
set /p user="Enter new username: "
echo %user%>>"Users\userlist.txt"
cls
echo ZEROS 0.2.1 REGISTER
echo.
set /p pass="Enter new password: "
echo %pass% >> "Users\%user%.txt"
goto menu
:log
cls
echo ZEROS 0.2.1 LOGIN
echo.
set /p user="Username: "
cls
echo ZEROS 0.2.1 LOGIN
echo.
set /p pass="Password: "
#echo off
set /p password=<Users\%user%.txt
#echo off
if %pass% equ %password% call console.bat
pause
goto menu
:ucheck
IF NOT EXIST Users (
cls
echo There are no users.
pause
goto start
)
:users
if not exist Users\userlist.txt (
goto reg
)
cls
cd Users
echo ZEROS 0.2.1 USERS
echo.
type userlist.txt
pause
goto start
When i choose 3rd option one time, it works well, but if i choose 3rd option 2 or more times, it shows that there are no users. How to fix it?
It's because Users\Users doesn't exist. At some point you cd Users, but never cd ... Logic problem.
I suggest instead of cd, use pushd Users, then popd when you want to return. You should also setlocal at the top of your script. See this page for an explanation of why this is important.
And don't be afraid to add double line breaks between sections and indents in your parenthetical code blocks to improve the readability of your code. This script is about as readable as assembly. :P
What I am trying to do:
Prompt the user to enter an account number.
The account number is the their folder.
The user is then prompted to select another directory (their time-stamped folder).
The script will then kick off an executable.
There will then be the parsing for findstr in a log file.
If the string comes back that it exists, then the script will then goto the :move sub-routine to attempt to recursively move through the "time-stamped" directory that the user selected to move all "*.ARC" files into their "Media1" folder.
The issue I am having is that the final step of running the for loop command in the "sub routine" :move is outputting the following error:
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
I'm not sure what is going on here as I can input this into a command prompt (without the !variables!) and get it to work. However, using the variables doesn't seem to work.
Another interesting point is that I am basically using the same !variables! under the :string "sub routine" and it finds exactly what I asked it to. So, I'm thinking there is nothing wrong with the !variables!?
Am I missing something here?
:main
cls
echo.
set /P acct=Please type the 9 digit account number you would like to restore:
set acctDir=x:\!acct!
set acctDir2=media1\Setup\setup.exe /cd
set log=c:\log.txt
echo. Starting on !date! !time! on !Computername! >> !log!
echo.
echo The account number you selected is: !acct!
echo.
goto :user
:user
set /p answer=Is this correct (Y/N)?
echo.
if /i !answer!==y goto :yes (
) else (
echo.
echo Ok. Let's try again^^!
echo.
pause
cls
goto :main
)
)
:yes
set c=0
For /f %%a in ('dir !acctDir! /B /A:D') do (
set /a c+=1
echo !c! %%a
set dir!c!=%%a
)
echo.
set /p userIn="Select a directory [1-!c!]: "
set userDir=!dir%userIn%!
echo.
echo You selected !userDir! for your data retrieval.
echo.
goto :string
:execute
echo.
echo The Data Protector Program will now be initialized...
start !acctdir!\!userDir!\!acctDir2!
goto :string
:string
set sumLog=!acctdir!\!userDir!\SummaryLog.txt
set succ=finished
set acctDir3=media1
set x=x:\!acct!\!userDir!
findstr " .*!succ!" !sumLog!
if exist errorlevel 0 (
pause
goto :move
) else (
goto :eof
)
:move
for /r "!acctdir!\!userDir!\" %%g in (*.ARC) do echo move "%%g" "!acctdir!\!userdir!\!acctdir3!\"
if exist errorlevel 1 (
echo Cannot move files. Error occurred.
pause
)
endlocal
goto :eof
Here is another tack to test your variables.
Add the four lines below after the :move label and run your batch file.
If there are any spaces or illegal characters you will see them there before the first pause.
:move
echo "!acctdir!"
echo "!userDir!"
echo "!acctdir3!"
pause
for /r "!acctdir!\!userDir!\" %%g in (*.ARC) do echo move "%%g" "!acctdir!\!userdir!\!acctdir3!\"
pause
I'm trying to create a password prompt which compares the user input to information in a text file (the password is saved in the .txt file).
I've tried to work with information provided to me through the command prompt and and this website but I just can't get it to work, probably because i don't have sufficient experience as I'm rather new to advanced batch coding.
This is what I've come up with so far, the name of the text file is Q47.txt and in it is just the word "hello" until I can get this to work:
#echo off
:a
cls
SetLocal EnableDelayedExpansion
set content=
for /F "delims=" %%i in (Q47.txt) do set content=!content! %%i
echo %content%
EndLocal
echo Enter password to continue:
set /p "VAR=>"
if "%VAR%" == "%content%" (
goto begin
)
echo Incorrect, please try again.
pause >nul
goto a
:begin
cls
echo Welcome
pause >nul
Please can you tell me where I've gone wrong.
I'd also like to know how to eliminate the space before the variable.
You might try this:
#ECHO OFF &SETLOCAL
:a
set "content="
for /F "tokens=*" %%i in (Q47.txt) do set "content=%%i"
echo "%content%"
echo Enter password to continue:
set /p "VAR=>"
if "%VAR%" == "%content%" goto begin
echo Incorrect, please try again.
pause >nul
goto a
:begin
cls
echo Welcome
pause >nul
Read the file into a variable like this:
set /p content=<"C:\path\to\Q47.txt"
I am running a batch file that prompts the user to "enter name". If the user entered nothing and pressed enter, then I want to show the same window over and over again that says:
Enter Name:
The below code is not working:
#echo off
:myDosFunc
set /p name=Enter Name:
IF "%name%"=="" (
call:myDosFunc
) ELSE (
echo %path%
)
pause
GOTO:EOF
call:myDosFunc
I am new to batch, please help.
This is similar to the w0051977 answer, but I belive it more closely matches your original intent.
#echo off
set "name="
:myDosFunc
set /p "name=Enter Name: "
if not defined name goto :myDosFunc
echo %path%
pause
exit /b
If you only want the prompt to appear once, no matter how many times the user presses Enter without entering anything, then you can use:
#echo off
set "name="
<nul set /p "=Enter Name: "
:myDosFunc
set /p "name="
if not defined name goto :myDosFunc
echo %path%
pause
exit /b
I think this is what you are after:
#ECHO OFF
:start
SET /P uname=Please enter your name:
IF "%uname%"=="" GOTO Error
ECHO Hello %uname%, Welcome to DOS inputs!
GOTO End
:Error
goto start
:End
Use the GOTO keyword. Go here for more information: http://www.codeproject.com/Tips/123810/Get-user-input-from-DOS-prompt
The problem is that you are trying to show the PATH without enclosing it with double quotes, and PATH can contain conflictive operators like ) then the batfile closes by an error.
PS: I've made other minor corrections in the code.
#echo off
:myDosFunc
Set "NAME="
set /p "name=Enter Name:"
IF NOT DEFINED NAME (
call :myDosFunc
) ELSE (
echo "%path%"
)
pause
GOTO:EOF
A simple solution would be to add cls before you prompt for the input
:myDosFunc
cls
set /p name=Enter Name: