Script .batch to rename a computer - windows

I need a script to rename a machine with a name that I choose myself, I also need to compare if the machine is already with that name because that same script will be used for more things.
I got to this code, but even if teh name i set is te same as the mchine it runs the IF
TITLE PADRONIZACAO
#ECHO off
COLOR 1f
CLS
SET /p name=Inset the machine name:
IF /I NOT "%name%" == "%computername%"( ECHO The machine will be renamed to %name%
WMIC ComputerSystem WHERE NAME="%computername%" RENAME "%name%"
ECHO The machine will be restarded
PAUSE
SHUTDOWN -r -f -t 0
) ELSE (
ECHO The machine has already ben renamed
)
ECHO the instalations will be started
PAUSE
Can someone help me?

Related

BATCH exist not working

I've started making a small program in Batch, it worked fine until I've gotten into IF NOT EXIST, just then all my problems started as every time it got into that statement, the batch file has just crashed.
Here's the code:
REM BEGGINING OPTIONS
#ECHO OFF
TITLE Organizer
COLOR 07
MODE CON COLS=101 LINES=30
SETLOCAL enableDelayedExpansion
CLS
REM WELCOME
CLS
COLOR E
ECHO WELCOME TO ORGANIZER.BAT! THIS PROGRAM IS MADE BY ELDAR BAKERMAN TO ORGANIZE YOUR FILES AND COMPUTER!
ECHO THIS IS VERSION 1.0!
ECHO THIS PROJECT STARTED IN 11.08.2017 (DD/MM/YYYY)
ECHO PRESS ANY KEY TO CONTINUE
PAUSE>NUL
REM ORGANIZATION
:ORGANIZATION
COLOR 0B
CLS
REM CREATE FOLDERNAME VARIABLE
SET /P FOLDERNAME=WHAT IS THE NAME OF THE FOLDER WHERE THE UNORGANIZED FILES ARE LOCATED IN?
REM FIND FOLDER
IF NOT EXIST "D:\Users\Eldar\Desktop\%FOLDERNAME%\NUL"
(
COLOR 0C
ECHO ERROR! FOLDER "%FOLDERNAME%" WAS NOT FOUND!
PAUSE>NUL
) ELSE (
PAUSE
)
PAUSE
Here's your script without the bloat:
#ECHO OFF
SET/P "FOLDERNAME=WHAT IS THE NAME OF THE UNORGANIZED FILES FOLDER? "
IF NOT EXIST "D:\Users\Eldar\Desktop\%FOLDERNAME%\" (
ECHO ERROR! FOLDER "%FOLDERNAME%" WAS NOT FOUND!
) ELSE ECHO "%FOLDERNAME%" WAS FOUND
PAUSE
Hopefully you can see how the parenthesis placement works.

Running Portable Exe file through batch file

I have created a batch file which runs multiple commands in the Windows command prompt, which is working just fine, however, I want to run a portable exe file through the same batch file as well. E.g. if I transfer the zip file to another computer, all I would like to do is run the batch file and that portable exe would run along with the other commands as well
:start
cls
color 1A
cls
#echo off
echo.
echo.
echo.
echo ********************************************
echo ************* Test Program **************
echo ********************************************
echo.
echo.
echo.
echo 01) System information
echo 02) Ping
echo 03) IP configuration
echo 04) Verify Drivers
echo 05) Driver List
echo 06) Get Serial Number
echo 07) Disk Defragmentation
echo 08) DiskPart
echo 09) Repair Load Preferences
echo 10) Run CCleaner
echo.
REM This is a test program
REM echo This is a Test Program
set /pnum= Type the corresponding number to perform an operation:
if %num%==01 (
cls
systeminfo
)
if %num%==02 (
cls
ping www.google.com
)
if %num%==03 (
cls
ipconfig /all
)
if %num%==04 (
cls
verifier
)
if %num%==05 (
cls
driverquery
)
if %num%==06 (
cls
wmic bios get serialnumber
)
if %num%==07 (
defrag c: /a
pause
cls
)
if %num%==08 (
diskpart
pause
cls
)
if %num%==09 (
cls
lodctr /r /f
echo.
pause
)
if %num%==10 (
cls
C:\Users\kumar\Desktop\CCleaner.exe
echo.
pause)
set /p choice="Do you want to restart? Press 'Y' to continue, or any other key to exit: "
if '%choice%'=='y' goto start
So for example in the last condition I am running CCleaner which is on the Desktop at the moment, but if i copy a zip file which consists of the BAT File and the CCleaner.exe how would i enable it to run on another PC after copying?
Any help would be appreciated.
If the "portable" directory will contain all executable files, another way is to make the location of the .bat script the current working directory.
#ECHO OFF
PUSHD "%~dp0"
: do things, the directory of the .bat script is the current directory
POPD
EXIT /B 0
place your tools into the same folder as the batchfile and instead of
C:\Users\kumar\Desktop\CCleaner.exe
do
%~dp0\CCleaner.exe
%~dp0 is Drive and Path of your batchfile.
You may also put your tools into a subdir (tools) and:
%~dp0\tools\CCleaner.exe
This could be useful in the case your app uses some system variables (e.g. PATH)
and you might want to reassign them locally before starting the application. The provided .exe will pass control to a .bat where you can do this and much more.
At the end of this .bat run the App.exe

Batch script prompt needs multiple entries

So I have this subroutine that I want to call from another location in a batch file. The functions work as desired, but for some reason I cant pin down, the prompt wants to have the user enter something TWICE, before it will accept anything.
Say, if I enter "0", to go back to a previous menu, it takes me right back to the prompt, and I have to enter "0" again before it will actually go back to the previous menu (elsewhere in my main script).
I can, say, enter "w" (or any other value), then the second time, enter the one I actually WANT to use, and it will finally do it.
This is driving me nuts.
:subfullbackup
cls
if exist "%current%\Backup\Full_Backup" (
Echo Backup folder already exists
Echo.
Echo [o] Overwrite local device files with existing local files
Echo [w] Wipe current local backup and start fresh
Echo.
set /p choice=Select:
if %choice% == o (
Echo.
Echo Depending on how much data you have,
Echo this could take a couple hours.
Echo.
Echo Backing up...
adb pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo -= BACKUP COMPLETE =-
Pause
Goto :backup
)
if %choice% == w (
Echo.
Echo Removing all current local backup files in 'Full_Backup'
rmdir /S /Q "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo Depending on how much data you have,
Echo this could take a couple hours.
Echo.
Echo Backing up...
adb pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo -= BACKUP COMPLETE =-
Pause
Goto :backup
)
if not %choice% == o goto subfullbackup
if not %choice% == w goto subfullbackup
) else (
Echo.
Echo Depending on how much data you have,
Echo this could take a couple hours.
Echo.
Echo Backing up...
adb pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo -= BACKUP COMPLETE =-
Pause
Goto :backup
)
Goto :eof
Your batch code with using delayed expansion, enabled at top of the batch script with command setlocal which additionally creates a copy of all environment variables and remembering also current directory for restoring the variables list, current directory and current states of command extensions and delayed expansion on endlocal or leaving batch processing:
#echo off
setlocal EnableDelayedExpansion
set "current=%CD%"
:FullBackup
cls
if exist "%current%\Backup\Full_Backup" (
Echo Backup folder already exists
Echo.
Echo [o] Overwrite local device files with existing local files
Echo [w] Wipe current local backup and start fresh
Echo.
set "UserChoice="
set /p "UserChoice=Select: "
if /I "!UserChoice!" == "o" (
Echo.
Echo Depending on how much data you have,
Echo this could take a couple hours.
Echo.
Echo Backing up...
adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo -= BACKUP COMPLETE =-
Pause
Goto DoBackup
)
if /I "!UserChoice!" == "w" (
Echo.
Echo Removing all current local backup files in 'Full_Backup'
rmdir /S /Q "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo Depending on how much data you have,
Echo this could take a couple hours.
Echo.
Echo Backing up...
adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo -= BACKUP COMPLETE =-
Pause
Goto DoBackup
)
goto FullBackup
) else (
Echo.
Echo Depending on how much data you have,
Echo this could take a couple hours.
Echo.
Echo Backing up...
adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo -= BACKUP COMPLETE =-
Pause
Goto DoBackup
)
Goto :EOF
:DoBackup
But your batch code could be also written without delayed expansion and much more compact avoiding duplicate code lines:
#echo off
set "current=%CD%"
:FullBackup
cls
if exist "%current%\Backup\Full_Backup" goto PromptBackup
:OverwriteBackup
Echo.
Echo Depending on how much data you have,
Echo this could take a couple hours.
Echo.
Echo Backing up...
adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo -= BACKUP COMPLETE =-
Pause
Goto DoBackup
:PromptBackup
Echo Backup folder already exists
Echo.
Echo [o] Overwrite local device files with existing local files
Echo [w] Wipe current local backup and start fresh
Echo.
set "UserChoice="
set /p "UserChoice=Select: "
if /I "%UserChoice%" == "o" goto OverwriteBackup
if /I not "%UserChoice%" == "w" goto FullBackup
Echo.
Echo Removing all current local backup files in 'Full_Backup'
rmdir /S /Q "%current%\Backup\Full_Backup" >nul 2>&1
goto OverwriteBackup
:DoBackup
Some notes about small changes in code:
choice (SS64 article) is a standard Windows command. Therefore it is advisable to avoid choice (Microsoft article) as name for an environment variable or label. UserChoice (CamelCase spelling for easier reading) is used instead of choice.
backup (SS64 article) is not a standard Windows command, but a standard SQL command. Therefore it is also advisable to avoid backup as name for an environment variable or label. DoBackup is used instead in batch code above.
It is advisable to define a default for an environment variable before prompting a user. The user can hit just RETURN or ENTER in which case the environment variable keeps its value.
The environment variable is cleared with set "UserPoint=" before prompting the user and therefore the variable does not exist when user enters nothing.
Possible would be also set "UserPoint=o" or set "UserPoint=w" to define a valid default value.
Comparing strings with user input should be done always with using double quotes to avoid an exit of batch processing caused by a syntax error when user inputs nothing.
if %choice% == w ( becomes if == w ( when the user enters nothing which is a syntax error and results in breaking batch processing by command processor.
if /I "%UserChoice%" == "w" ( becomes if /I "" == "w" when the user enters nothing in code above which is still valid batch code and can be therefore processed.
Note: User could now break batch processing by entering "w".
But it can be expected here that the user does not input 1 or more double quotes on being asked for o or w.
On comparing strings entered by user with predefined strings it is advisable to do case-insensitive string comparisons if letters are included in the compared strings.
The option /I changes a string comparison from case-sensitive to case-insensitive.
So now the user can enter also O or W and this is interpreted like o or w.

Batch file to search list of PCs for a folder/file and create log file

I am trying to get a script to seach all the pcs on my network and tell me if a folder is present within the C directory.
Here's the script I've got so far, it is working but it is not pulling through the list of PCS from the file specified.. It is always running on my PC alone.
#ECHO OFF
for /f %%a in (c:\pcs.txt) do IF EXIST "C:\HMSJAVA\jt400.jar" (
echo %computername% has HMS GUI locally installed. >> "c:\hmsguipcs.txt"
GOTO :END
) ELSE ( Goto :NoHMS
)
:NoHMS
IF NOT EXIST "C:\HMSJAVA\jt400.jar" (
echo %computername% does NOT have HMS GUI locally installed. >> "c:\hmsguipcs.txt"
GOTO :END
)
:END
PAUSE
*****MY PCs File pcs.txt*****
COMPUTER1
COMPUTER2
COMPUTER3
Got it working, what I did was add the for /f command to the if not exist command, and then ran the batch using psexec. I also had to change all teh stuff specific to the C drive to our public shared drive. Works like a charm now!
You only testing your C: drive for the file.
You need to specify a network path for the If NOT Exists command.
IF NOT EXIST "\\%%A\C$\HMSJAVA\jt400.jar"
I'd put it in an echo to see if it's right.
The hidden admin share C$ may not be available. Choose another share you made.

Batch file add/remove hosts file entries syntax error

The following script is supposed to manage adding and removing entries into the hosts file. Entries with .dev are supposed to point to localhost and all others should prompt for an IP address. If the entry already exists it should ask you if you want to remove it.
I am getting a vague The syntax of the command is incorrect error but I'm not sure which line it's referring to. If I turn #echo on it then spits out if ( on the next line and then dies. The only way I can catch the error is to do a screenshot because it immediately exits.
I've spent several hours on this, so any assistance would be greatly appreciated!
#echo off
title ClearSight Studio
echo Virtual website setup script
echo Version 1.4
echo Last modified 12/30/2011
echo.
REM Get the name of the domain name
echo What domain name? Ctrl+C to cancel. Example: newdomain.dev.
set /p domain= Domain:
REM Set a variable for the Windows hosts file location
set hostpath=\windows\system32\drivers\etc
set hostfile=hosts
set sitespath=\clearsight\sites
set extension=%domain:~-3%
REM Make the hosts file writable
attrib -r %hostpath%\%hostfile%
REM Add the domain to point to localhost at the end of the file if it doesn't already have an entry.
find /c " %domain%" %hostpath%\%hostfile%
if errorlevel 1 (
if /i %extension%==dev (
set /p ip= What is the IP?
)
echo. >> %hostpath%\%hostfile%
echo.# Adding %domain% via batch process on %date:~10,4%-%date:~4,2%-%date:~7,2%. >> %hostpath%\%hostfile%
if %ip% (
echo.%ip% %domain% >> %hostpath%\%hostfile%
) else (
echo.127.0.0.1 %domain% >> %hostpath%\%hostfile%
)
) else if errorlevel 0 (
echo Entry found in hostfile already. Would you like to remove it?
set /p remove= Remove (Y/N)
if /i %remove%==Y (
findstr /v "%domain%" %hostpath%\%hostfile% > %hostpath%\%hostfile%
)
)
if /i %extension%==dev (
REM Create the folder if it doesn't exist
if exist %sitespath%\%domain% (
echo %sitespath%\%domain% already exists.
) else (
echo Creating %sitespath%\%domain%...
mkdir %sitespath%\%domain%
)
)
REM Clean up
attrib +r %hostpath%\%hostfile%
echo.
echo Done.
if /i %extension%==dev (
REM Do a "git" of the repo, if requested
echo Would you like to clone an external git repository named %domain% from Bitbucket?
echo This assumes the git repository was set up under the "jamonholmgren" account.
echo Do it manually if it's under someone else's account. Also, make sure you have permissions to this repo.
set /p getgit= Get git clone? (Y/N):
)
REM
if /i %getgit%==Y (
git clone git#bitbucket.org:jamonholmgren/%domain%.git %sitespath%\%domain%\
pause
) else (
echo Okay, then we're done.
pause
)
You have two problems with this bit of code
if %ip% (
echo.%ip% %domain% >> %hostpath%\%hostfile%
) else (
It is invalid syntax. I presume you want to test if the variable is defined. The proper syntax is if defined ip (
You are attempting to expand %ip% in the same if() block where it was defined. This can't work because %ip% is expanded at parse time, which is before you assign the value! The solution is to use delayed expansion instead !ip!. Delayed expansion must first be enabled, probably near the top of your script, using setlocal enableDelayedExpansion
So the fixed code would look something like this
setlocal enableDelayedExpansion
.
.
.
if defined ip (
echo.!ip! %domain% >> %hostpath%\%hostfile%
) else (
You have the same delayed expansion issue with %remove%
Edit - expanding on Andriy M's comment
You also have a potential issue with how you deal with user input for ip and remove. Both values should be initialized either to nothing, or to a default value, prior to prompting for the value. If the user does not enter anything, then the existing default value (if any) is preserved.
Also, you may want to confirm that a valid value was entered, especially for the ip. If you do, then you will want to take the prompt out of the loop and put it in a called subroutine. The subroutine can check if a value was entered and loop back to try again if it wasn't. It must be in a subroutine because you cannot GOTO within a parenthesized block of code like you have with your IF statement.
When you expand a variable value via %variable% its value is expanded just once after the line that contain commands is read and before the commands are executed. For example:
set var=Original
set var=New & echo %var%
Previous line show "Original". The same effect happen with any command enclosed in parentheses (that belong to any IF or FOR commands).
The way to solve this problem is using Delayed Variable Expansion by enclosing a variable in exclamation marks instead percents:
set var=Original
set var=New & echo !var!
However, you must first activate the delayed expansion with this command:
setlocal EnableDelayedExpansion
So, insert previous line at beginning of your program and change ALL references to variables that may change its value inside an IF or FOR by !name! instead of %name%. For example:
if %ip% (
that I'm sure is the line that caused your problem...
Thanks everyone for the help! Here is the final (working) result in case anyone ever wants to do something similar. The output to the screen could probably use a little tweaking but in all my testing it seems completely reliable.
#echo off
setlocal enableDelayedExpansion
title ClearSight Studio
echo Virtual website setup script
echo Version 1.5
echo Last modified 2/23/2012
echo.
REM Get the name of the domain name
echo What domain name? Ctrl+C to cancel. Example: newdomain.dev.
set /p domain= Domain:
REM Set a variable for the Windows hosts file location
set hostpath=\windows\system32\drivers\etc
set hostfile=hosts
set sitespath=\clearsight\sites
set extension=%domain:~-3%
REM Make the hosts file writable
attrib -r %hostpath%\%hostfile%
REM Add the domain to point to localhost at the end of the file if it doesn't already have an entry.
find /c " %domain%" %hostpath%\%hostfile%
if errorlevel 1 (
if /i NOT %extension%==dev (
set /p ip= What is the IP?
) else (
set ip=127.0.0.1
)
echo. >> %hostpath%\%hostfile%
echo.# Adding %domain% via batch process on %date:~10,4%-%date:~4,2%-%date:~7,2%. >> %hostpath%\%hostfile%
echo.!ip! %domain% >> %hostpath%\%hostfile%
) else if errorlevel 0 (
echo Entry found in hostfile already.
set /p remove= Would you like to remove it? (Y/N)
if /i !remove!==Y (
findstr /v %domain% %hostpath%\%hostfile% > %hostpath%\%hostfile%.txt
type %hostpath%\%hostfile%.txt > %hostpath%\%hostfile%
)
)
if /i %extension%==dev (
REM Create the folder if it doesn't exist
if exist %sitespath%\%domain% (
echo %sitespath%\%domain% already exists.
) else (
echo Creating %sitespath%\%domain%...
mkdir %sitespath%\%domain%
)
)
REM Clean up
attrib +r %hostpath%\%hostfile%
REM Flush DNS so the changes are available imidiately
ipconfig /flushdns
echo.
echo Done.
if /i %extension%==dev (
REM Do a "git" of the repo, if requested
echo Would you like to clone an external git repository named %domain% from Bitbucket?
echo This assumes the git repository was set up under the "jamonholmgren" account.
echo Do it manually if it's under someone else's account. Also, make sure you have permissions to this repo.
set /p getgit= Get git clone? (Y/N)
)
REM
if /i !getgit!==Y (
git clone git#bitbucket.org:jamonholmgren/%domain%.git %sitespath%\%domain%\
pause
) else (
echo Okay, then we're done.
pause
)
Just some hints for debugging the batch file:
Start it from a cmd shell to avoid closing the window after exiting
Add more REM commands that will serve as log entries when turning echo on
Simplify your script by repeatedly removing parts from the end until it runs without error. Reinsert the faulty parts and try to figure out what's wrong there
While this does not quite answer your question, I hope the hints will help you solve the problem. You are always welcome to ask for general advice, but I'm afraid that your particular problem is somehow a bit too specific to be of general interest.

Resources