what i want is if user already exist just put the name of this user in errorlog.txt
i have this:
set var_b=123
FOR /F %%v in (%arxiu%) do (
net user %%v >nul 2>nul
if errorlevel 1 ( net user %%v %var_b% /add >nul 2>nul
)
if errorlevel 0 ( echo %%v >> C:\Users\%username%\Desktop\errorlog.txt
)
)
but with this code i get the name of users that already exist and the one news that creates i dont know why any error ?
See Microsoft support article Testing for a Specific Error Level in Batch Files.
if errorlevel X means if exit code of previous command is greater or equal X.
Use following batch code:
set "var_b=123"
for /F %%v in (%arxiu%) do (
net user %%v >nul 2>nul
if errorlevel 1 (
net user %%v %var_b% /add >nul 2>nul
) else (
echo %%v>>"%USERPROFILE%\Desktop\errorlog.txt"
)
)
Related
:deleteaccount
cls
echo what account do you want to delete?
set /p dan=
if not exist %~dp0\database\%dan%\ (
echo this account doesn't exist & pause >nul & goto stage
)
else %~dp0\database\%dan%\ (
cls & echo password:
set /p dap=
call %~dp0\database\%dan%\%dan%.bat
if %dap% == %rpassword1% (
echo are you sure you want to delete this account? yes/no
set /p daq=
if %daq% == yes (
#RD /S /Q %~dp0\database\%daq%\
echo account succesfully deleted
pause >nul & goto stage)
if %daq% == no (goto stage)
)
)
After I type the correct password for the account I wanted to delete, it says < is unexpected for some reason.
For the purposes of saving screen real estate, here's an example of your script, modified to use the correct syntax and to include a logical structure.
#Echo Off
:DeleteAccount
ClS
Set "dan=:"
Set /P "dan=Which account do you want to remove? "
If Not Exist "%~dp0database\%dan%\" (
Echo The account does not exist.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
ClS
If Not Exist "%~dp0database\%dan%\%dan%.bat" (
Echo The required batch file is missing for the account.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
Set "dap="
Set /P "dap=Please enter your password: "
Call "%~dp0database\%dan%\%dan%.bat" 2> NUL
If ErrorLevel 1 (
Echo A error occurred running the batch file.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
If "%dap%." == "%rpassword1%." (
"%__AppDir__%choice.exe" /M "Are you sure you want to remove this account"
If Not ErrorLevel 2 (
RD /S /Q "%~dp0database\%daq%" 2> NUL
If ErrorLevel 1 (
Echo An error occurred deleting the account.
) Else Echo Account successfully removed.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
) Else (
Echo Incorrect password.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
:Stage
Rem Rest of code goes here.
Please read and review it, taking particular notice of the included double-quotes which are used to enclose and protect special characters in strings. The first two and last two lines were included to keep this example independent, (as they were omitted from your posted snippet).
Deploying applications via SCCM. Currently trying to deploy Wireshark and I am 1st trying to create the directory then copy over a preference file to the users %APPDATA%. The preference file basically stops the application from checking for auto updates.
Reason I need to create the directory is because it does not exist until Wireshark is launched for the 1st time.
Issue is that when doing this, SCCM is deploying as a system user so %APPDATA% goes to directory C:\Windows\System32\config\systemprofile\AppData\Roaming\
But I would like to get to C:\Users\SPECIFIC USER\AppData\Roaming
I am deploying the application with a batch file:
Wireshark-win64-2.4.6.exe /S
mkdir %APPDATA%\Wireshark\
xcopy preferences %APPDATA%\Wireshark
This will work locally on my own machine but if I run under PSEXEC (like SCCM would) then it will end up in the incorrect directory.
I am new to creating applications within SCCM as well as using batch files to deploy so please include details if possible. Regardless I appreciate the help!
Done with using getprofiles.cmd to echo the profiles and using main.cmd
with a for loop to process the profile paths.
main.cmd:
#echo off
setlocal
:: Install Wireshark.
echo Wireshark-win64-2.4.6.exe /S
:: Update Wireshark app data in user profiles.
for /f "tokens=*" %%A in ('getprofiles.cmd "\AppData\Roaming"') do (
call :skip_profile "%%~A" "\\Administrator\\" "\\MSSQL\$SQLEXPRESS\\" || (
echo mkdir "%%~A\Wireshark\"
echo xcopy preferences "%%~A\Wireshark"
)
)
exit /b
:skip_profile
for %%A in (%*) do (
if not "%%~A" == "" if /i not "%%~A" == "%~1" (
echo "%~1"| findstr /i "%%~A" >nul 2>nul
if not errorlevel 1 (
echo Skip account "%~1"
exit /b 0
)
)
)
exit /b 1
getprofiles.cmd:
#echo off
setlocal
if "%~1" == "/?" goto :help
:: ProfileList key that contains profile paths.
set "ProfileListKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
:: Get profiles directory path.
set "ProfilesDirectory="
for /f "tokens=1,3" %%A in (
'reg query "%ProfileListKey%" /v "ProfilesDirectory"'
) do if /i "%%~A" == "ProfilesDirectory" call set "ProfilesDirectory=%%~B"
if not defined ProfilesDirectory (
>&2 echo ProfilesDirectory is undefined
exit /b 1
)
:: Search all profile paths in profiles directory and echo existing paths appended with the 1st script argument.
for /f "delims=" %%A in (
'reg query "%ProfileListKey%"'
) do call :ProfilePath "%%~A" "%~1"
exit /b
:ProfilePath
setlocal
set "arg1=%~1"
:: Validate 1st call argument is a profile subkey.
if not defined arg1 exit /b 1
if /i "%arg1%" == "%ProfileListKey%" exit /b 1
if "%arg1:~,1%" == " " exit /b 1
:: Echo existing profile paths with defined 2nd argument appended.
for /f "tokens=1,3" %%A in (
'reg query "%arg1%" /v ProfileImagePath^|find /i "%ProfilesDirectory%"'
) do (
if "%%~A" == "ProfileImagePath" (
if exist "%%~B%~2" echo "%%~B%~2"
)
)
exit /b
:help
echo Prints profile paths from the registry that exist in the Profiles Directory.
echo 1st argument can be a path to be appended to the profile path.
echo i.e. "\AppData\Roaming" is appended to become "C:\Users\...\AppData\Roaming".
exit /b
The script main.cmd echoes the results for testing. Remove the echoes
to actually use if commands are valid.
The ProfileList key in the registry stores the path to find the
profiles and has subkeys with data such as path of each profile on
the machine.
main.cmd can avoid profiles such as Administrator and MSSQL$SQLEXPRESS.
The called label :skip_profile takes the profile path as 1st argument.
Following arguments are for patterns and if matched, will be a skipped
profile.
findstr is being used for checking the profile path for a match with
regular expressions so use findstr /? for syntax requirements.
Case is set as insensitive as to use of /i.
The getprofiles.cmd script gets the ProfilesDirectory path which
is where the user profile folders can be found. It then querys the key
to get the profile keys by using the called label of :ProfilePath.
The label checks if the ProfilesDirectory path is found in each
profile path found. It then checks if the path exists before echoing
the path. If the optional 1st parameter is passed, then it will be
appended and the path will be validated as that path.
A test outputs:
Wireshark-win64-2.4.6.exe /S
mkdir "C:\Users\Michael\AppData\Roaming\Wireshark\"
xcopy preferences "C:\Users\Michael\AppData\Roaming\Wireshark"
which seems OK as I only have 1 user profile on my current machine.
You could probably merge the code together to make just 1 script though
I decided to leave getprofiles.cmd as reusable for other uses.
Here's a batch file that may help you. It creates the directory you tell it in all user's %APPDATA% directories it finds if you don't specify a username, or creates the directory in only the specific user's %APPDATA% directory if you do specify a username.
#ECHO OFF
SETLOCAL
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
SET BAT=%~NX0
SET ROOTDIR=C:\Users
IF "%~1" == "" (
GOTO USAGE
)
SET "DIR=%~1"
SET "USER=%~2"
IF NOT "%USER%" == "" (
IF EXIST %ROOTDIR%\%USER%\AppData\Roaming\ (
IF NOT EXIST "%ROOTDIR%\%USER%\AppData\Roaming\%DIR%" (
MKDIR "%ROOTDIR%\%USER%\AppData\Roaming\%DIR%"
ECHO Created %ROOTDIR%\%USER%\AppData\Roaming\%DIR%
)
)
GOTO :EOF
)
FOR /F "DELIMS=" %%D IN ('DIR /A:D /B %ROOTDIR%') DO (
IF EXIST %ROOTDIR%\%%D\AppData\Roaming\ (
IF NOT EXIST "%ROOTDIR%\%%D\AppData\Roaming\%DIR%" (
MKDIR "%ROOTDIR%\%%D\AppData\Roaming\%DIR%"
ECHO Created %ROOTDIR%\%%D\AppData\Roaming\%DIR%
)
)
)
GOTO :EOF
:: --------------------------------------------------------------------------
:USAGE
ECHO.
ECHO A batch file that creates a directory in every user's %%APPDATA%% directory
ECHO if no username is specified or creates the directory only for the specific
ECHO user if the username is specified.
ECHO.
ECHO Usage: %BAT% ^<directory^> [username]
GOTO :EOF
Find the owner of the EXPLORER.EXE process:
for /f "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do if /i "%%a %%b"=="User Name:" (set domain_user=%%c)
for /f "TOKENS=1,2 DELIMS=\" %%a in ("%domain_user%") do set domain=%%a && set LoggedInUserID=%%b
for /f "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do if /i "%%a %%b"=="User Name:" (set domain_user=%%c)
for /f "TOKENS=1,2 DELIMS=" %%a in ("%domain_user%") do set domain=%%a && set LoggedInUserID=%%b
Dears
Here is my .bat command line
I use bitsadmin /transfer command download from url.
but got error(like disconnect network...etc) I need continue execute remain command.
But now I can't get achievement... what should I do
#echo off
:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates are the exact same file, downloaded from the same location!
SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET "destination=C:\FilterUpdates.cab"
if not exist "%DTMBIN%\" (
echo ERROR: folder "%DTMBIN%"
echo does not exist! Please verify that you have the controller installed.
pause
exit /B 1
)
echo Please make sure that all instances of the Studio are turned OFF!
echo Downloading Filters...
bitsadmin /transfer "Downloading Filters" "%source%" "%destination%"
if errorlevel 1 goto end
echo Extracting...
expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
echo Installing...
pushd "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
"%DTMBIN%\updatefilters.exe " /s
if not errorlevel 0 echo ERROR & exit /B 1
popd
:end
exit
Your problem could be caused by the errorlevels generated by bitsadmin: some of them are negative values and the test if errorlevel 1 will be evaluated as false (if errorlevel n is true for values greater than or equal to n)
You will need to read and test the value of the errorlevel variable
if not %errorlevel%==0 exit /b 1
But sometimes, bitsadmin will have errors and will generate a errorlevel 0, so, you need to manualy check the status
#echo off
setlocal enableextensions disabledelayedexpansion
:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates
:: are the exact same file, downloaded from the same location!
SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if not exist "%DTMBIN%\" (
echo ERROR: folder "%DTMBIN%"
echo does not exist! Please verify that you have the controller installed.
pause
exit /B 1
)
SET "destination=C:\FilterUpdates.cab"
if exist "%destination%" del /q "%destination%"
echo Please make sure that all instances of the Studio are turned OFF!
echo Creating download task...
set "taskName=[HCK_FilterUpdater]"
>nul (
rem remove task if already present
bitsadmin /list | find "%taskName%" && bitsadmin /cancel "%taskName%"
rem create the task
bitsadmin /create "%taskName%"
rem include our file in the task
bitsadmin /addfile "%taskName%" "%source%" "%destination%"
rem start the download
bitsadmin /resume "%taskName%"
)
echo Downloading...
set "exitCode="
for /l %%a in (1 1 500) do if not defined exitCode for /f "delims=" %%a in ('
bitsadmin /info "%taskName%"
^| findstr /b /l /c:"{"
') do for /f "tokens=3,*" %%b in ("%%a") do (
if "%%~b"=="TRANSFERRED" (
set "exitCode=0"
>nul bitsadmin /complete "%taskName%"
echo ... done
)
if "%%~b"=="ERROR" (
set "exitCode=1"
bitsadmin /geterror "%taskName%" | findstr /b /c:"ERROR"
>nul bitsadmin /cancel "%taskName%"
)
if not defined exitCode (
echo(%%b %%c
timeout /t 2 >nul
)
)
if not defined exitCode ( echo TIMEOUT & exit /b 1 )
if not exist "%destination%" ( echo ERROR & exit /b 1 )
echo Expanding...
>nul expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%"
if errorlevel 1 ( echo ERROR & exit /b 1 )
echo Installing...
pushd "%DTMBIN%" || ( echo ERROR & exit /b 1 )
".\updatefilters.exe " /s || ( echo ERROR & exit /b 1 )
popd
It seems like once command shell is in bitsadmin mode you can't take it out of it. My solution unfortunately is two seperate .bats.
I have a script to delete all files in my Epson Scans Folder. In my pclist.txt i have each pc name listed on each line. I am trying to figure out how to adjust this script to continue to the following pc if a pc on the list is not on. The script runs perfect if all pcs are turned on. But if a pc is turned off it stops running and does not move on to the next pc in the list. Does anyone know how I can adjust for that?
Note: pclist.txt is just a list of pc names
#echo off
IF "%CD%\" NEQ "%~dp0" PUSHD "%~dp0"
for /F %%G in (pclist.txt) do (
pushd "\\%%G\C$\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
)
There is an exit /B command, so it is no surprise that the script terminates in case pushd fails. The || constitutes a conditional command concatenation operator which lets the following command execute only in case the previous one fails (that is, it returns a non-zero exit code).
So I would remove the || exit /B 1 part and simply reverse the logic, so that the deletions are accomplished only in case pushd succeeds to connect to the drive. There is also an && operator that lets the following command execute in case the preceding one succeeded, which I would use:
#echo off
for /F "usebackq delims=" %%G in ("%~dp0pclist.txt") do (
pushd "\\%%G\C$\Epson Scans" && (
rem Perform the actual deletions in case of successful connection:
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
) || (
rem Do something in case of failure, like printing an error message:
>&2 echo Could not connect "%%G"!
)
)
Try using ping command (with -n 1 tries only 1 time) and test using ERRORLEVEL variable
ping -n 1 MYSERVER > nul
if NOT ERRORLEVEL 1 (
echo "MYSERVER IS OK"
)
In your script:
#echo off
IF "%CD%\" NEQ "%~dp0" PUSHD "%~dp0"
for /F %%G in (pclist.txt) do (
ping -n 1 %%G > nul
if NOT ERRORLEVEL 1 (
pushd "\\%%G\C$\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
)
)
I need to check the registry for multiple keys before I start my program (they shouldn't exist). The widely spread solution for checking registry keys works for only one check since it sets global ErrorLevel to 1. The example below won't work correctly.
#echo off
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey >nul
if %errorlevel% equ 0 (
echo "mykey exists - do nothing"
) else (
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey2 >nul
if %errorlevel% equ 0 (
echo "mykey2 exists - do nothing"
) else (
run my program
)
)
Using errorlevel like this will require delayed expansion.You can try with IF ERRORLEVEL
#echo off
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey >nul
if %errorlevel% equ 0 (
echo "mykey exists - do nothing"
) else (
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey2 >nul
if errorlevel 1 (
run my program
) else (
echo "mykey2 exists - do nothing"
)
)
Get inspired by this example :
#echo off
cls & color 0A & echo.
Mode con cols=70 lines=5
Title Check Startup Registry Keys
Set TmpLogFile=TmpLogkey.txt
Set LogFile=Startup_key_Log.txt
If Exist %TmpLogFile% Del %TmpLogFile%
If Exist %LogFile% Del %LogFile%
Set mykey="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"^
^ "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"^
^ "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"^
^ "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run"^
^ "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run"^
^ "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options"
Echo.
ECHO **************************************
ECHO Please wait..........
ECHO **************************************
For %%K in (%mykey%) Do Call :Check_Key %%K %TmpLogFile%
Cmd /U /C Type %TmpLogFile% > %LogFile%
Start "" %LogFile%
Exit /b
::********************************************
:Check_Key
reg QUERY %1 >nul 2>&1
(
if %errorlevel% equ 0 ( reg QUERY %1 /s
) else ( echo %1 ===^> Not found
)
) >>%2 2>&1
::********************************************