Batch-File Stops at first Line or Ends up in Loop - batch-file

I have a little problem I don't understand. First off I am a Rookie and never done Batch-Scripting before. I made two scripts for my company including the command nslookup
First script: in this script the nslookup command shall print me out the results of a specific ip range, but after "echo NSLOOKUP" nothing is displayed" The for-loop doesn't execute. But it worked last week with the same code:
#echo off
for /L %%x IN (33, 1, 60) DO (
echo _______________________
echo NS LOOKUP
nslookup 192.168.178.%%x
)
pause
Second script: Here it's a bit different. I can enter the name for the domain, but after that it doesnt execute the nslookup-command aswell
#echo off
set input=y
:while
if %input%==n (
exit
)
echo _______________
echo Name for nslookup
set /p name=
nslookup %name%.domain.domain.domain.de
echo _______________
echo Search another one? (y/n)
set /p input=
goto while
Also whenever I make a new batch-file and typing something like:
#echo off
echo tell me your name
set /p name=
echo tell me your age
set /p age=
echo %name%
echo %age%
pause
It just end in a loop with an input of name and age.
I am confused. Please help me. I'd appreciate it. Thank you in advance.

Related

Change the hostname using the batch script

Hi i wanted to change the value of hostname for new computer but things didn't work as i try to input the value and getting the value for the hostname. I try to restart the computer the result is still the same.. I am testing to see whether this script is capable of updating the hostname for pcs or computer name
thanks
here is my code
REM This script runs in MS-DOS Batch File Language
#echo off
set /p id= Enter ID or Hostname:
echo %id%
WMIC computersystem where caption='%ComputerName%' rename %id%
REM exit the applications
echo "Export completed successfully. Press any key to exit"
pause >nul
exit /B
Here's how I'd probably do it:
#Echo Off
Echo Your current name is %ComputerName%
:AskID
Set "ID="
Set /P "ID=Enter your new name: "
If Not Defined ID (Echo Can not be empty
GoTo AskID)
If /I "%ID%"=="%ComputerName%" Exit /B
If "%ID:~,1%"=="." (Echo Must not begin with a period
GoTo AskID)
Rem Put here some more checks for disallowed words or characters
WMIC ComputerSystem Where Name="%ComputerName%" Call Rename "%ID%"
Notes
This will need to be run 'As administrator'.
The change will not take effect until the next reboot.
It is important that you don't allow your end user, especially when running an administrative task to just enter anything at the prompt. Please consider following the Remarked line's advice.
I tested the below batch script to change the windows 10 hostname through batch script and it work perfectly fine try it out and reboot the system atlast !
set /p NEW_NAME="Please enter computer name: "
for /f %%i in ('hostname') do set OLD_NAME=%%i
echo %OLD_NAME%
echo %NEW_NAME%
WMIC computersystem where caption="%OLD_NAME%" rename "%NEW_NAME%"
pause

Read webs from a file and ping them with a loop (?)

im making an script to ping different websites. I want to read addresses from a file, ping them one time, and write the results on another text file.
echo off
cls
echo "TEST" > %cd%\out.txt
time >> %cd%\out.txt
ping -n 1 google.com>> %cd%\out.txt
pause>null
I dont know how to make the loop for pinging and also, how to read line by line from the file. Can anyone help me?
Thanks.
Something along the lines of the following should be what you want:
#echo off& setlocal
echo google.com>sites.txt
echo yahoo.com>>sites.txt
set cd=.
for /f %%w in (sites.txt) do call :NextSite %%w
exit /b
:NextSite
echo. >>%cd%\out.txt
echo. >>%cd%\out.txt
echo. | time | find "current" >>%cd%\out.txt
ping -n 1 %1 >>%cd%\out.txt
The logic starts at the line with the "for" statement; the preceding lines merely set up a test environment. You will have your own %cd% setting and filename to replace "sites.txt"

How do you call variables put into a text file from a batch into a batch file?

I was Creating a quiz as a test of my skills and was able to successfully put text and a variables into a separate text file. When I tried to call them from the batch file, the variables were blank. How can I fix this? Also, if anyone can tell me how to recall a specific line, I would be grateful.
My Code (Shortened):
Echo What is your Name?
set /p Name=Name=
echo Test Results: > TestResults.txt
echo Name: %Name% >> TestResults.txt
cls
echo What's 2 + 2?
set /p Problem=2 + 2=
echo 2 + 2= %Problem% >> TestResults.txt
cls
echo Who is President?
set /p President=President=
echo President= %President% >> TestResults.txt
cls
echo Goodbye!
pause>nul
cls
for /f "delims= " %%G in (TestResults.txt) do echo %%G
pause>nul
If you look closely at your output, it should give you a clue to the problem.
When you TYPE the resultant file, it should have all the expected data:
c:\>TYPE TestResults.txt
Test Results:
Name: Simple Simon
2 + 2= 16
President= Bugs Bunny
But your FOR /F loop is producing:
Test
Name:
2
President=
Nothing appears after the fist space in each line. Look at your DELIMS option and it should be obvious you are setting the token delimiter to be a <space>. Simply make sure the DELIMS option is the last option in the list, and put the closing quote immediately after DELIMS=, and prolem is solved.
for /f "delims=" %%G in (TestResults.txt) do echo %%G

(Windows Batch File) IF statement inside FOR LOOP

I'm trying to write manual document validation part of my program. It's basically opening all the pdf documents one by one in the same folder. When its open i would like to echo few possibilities for user. Here starts the problem. I have around 180 possible choices. I was thinking to ask for the first letter of choice. Then it will echo all choices with started with X letter and user has to simply enter the number of this choice. So for example we have :
1. Asomething
2. Asomename
3. Asomenametoo
4. Bname
5. Bname 2
6. Bname 3
I want user to choose first letter and print possible choices. When the choice is made program should add some string to txt file with the same name in the same folder. Here i have a problem with IF statement inside FOR loop. I wanted to use goto but i can't do it inside FOR loop.
I can set up all the strings for each number before. For example : When you choose 1 it will add SomeString to txt. It's important to use choice option to avoid any typo's. Does anybody knows any other way to do this inside FOR loop ?
CODE:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
I'm having some trouble understanding the problem you're having, but it looks like all of the statements following the IF should all be conditions of the IF, not just the ECHO statement. For that, you can put the entire block in parentheses like this:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" (
ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
) else (
goto :EOF REM Just an example of else
)
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
Were you having some problem using goto in the FOR loop?

Setting User account "Full Name" and "Description" from within a batch using variables

I am attempting to write a batch script that adds new users to a machine using user prompts to get the information. I am at the point where the user gets created correctly, but the "Full Name" and "Description" fields don't populate, no matter what I try. I can't seem to get it to pass the %fullname% variable into either the net user command or the wmic command. If I just type quoted text into either spot it adds the information properly. Also if there's a better way to do echos like that it would be appreciated, takes up too many lines IMO. :)
:User
echo.
echo Please Input Username...
echo.
set /p username=Username:
echo.
echo Please Confirm Username...
echo.
set /p confirm=Username:
if '%username%' == '%confirm%' (
echo.
echo Please Input User Full Name...
echo.
set /p fullname=Full Name:
echo.
echo Please Input User Description...
echo.
set /p description=Description:
echo Creating %username%...
net user %username% * /add /active:yes /fullname:%fullname% /comment:%description% /passwordchg:no /expires:never
wmic useraccount where "Name='%username%'" set PasswordExpires=FALSE
wmic useraccount where "Name='%username%'" set FullName=%fullname%
This script creates the user just fine, but the fullname and comment fields are blank.
Problem solved. Apparently the parentheses for my if statement were messing everything up. The fullname and description variables were blank for whatever reason. I guess you can't do a set inside an if statement?
No matter, changing to this has solved the problem and it works perfectly now.
:User
echo.
echo Please Input Username...
echo.
set /p username=Username:
echo.
echo Please Confirm Username...
echo.
set /p confirm=Username:
if '%username%' == '%confirm%' goto User1
goto User
:User1
echo.
echo Please Input User Full Name...
echo.
set /p fullname=Full Name:
echo.
echo Please Input User Description...
echo.
set /p description=Description:
pause
echo Creating %username%...
net user %username% * /add /active:yes /fullname:"%fullname%" /comment:"%description%" /passwordchg:no /expires:never
wmic useraccount where "Name='%username%'" set PasswordExpires=FALSE

Resources