Checking admin access for a particular file using batch file - windows

I want to know whether my host file has access to EDIT or not using errorcode but i am always getting error code as 0..Please help me out in getting the code to check for a admin access for a particular specified file
I am using below code, but not working
#echo off
setlocal enabledelayedexpansion enableextensions
attrib -s -h -r %systemroot%\system32\drivers\etc\hosts
echo %errorlevel%
echo.
IF %errorlevel% NEQ 0 (
echo.
echo Do not have access..
pause >nul
exit
) else (
echo Has Access..
pause >nul
)

You need to check the file permission,there are many ways to achieve this one by reading the file and another getting file permission using cacls command..
*(for example)
#echo off
SETLOCAL EnableDelayedExpansion
findstr /c:"any string" %systemroot%\system32\drivers\etc\hosts
echo %errorlevel%
echo.
if errorlevel 1 (
echo.
echo File Required Administrator Access
pause >nul
exit
) else (
echo Administrator Access ..
pause >nul
)

Related

Copying a file to mapped drive not working

I am trying to connect to a network drive, copy a file and move it to another location, and log if it is successful or not. This is what I got so far:
#echo off
#setlocal enableextensions enabledelayedexpansion
if exist Transfer_logfile.txt (
ECHO Y | del Transfer_logfile.txt
)
set LogFile=_logfile.txt
set logg=^> _^&^& type _^&^&type _^>^>%LogFile%
for /F "tokens=*" %%A in (ipaddresses.txt) do (
net use \\%%A\c$\test /USER:test %logg%
echo copying files across to %%A... %logg%
COPY -f -R -Y C:\test\test.exe \\%%A\c$\test\test.exe %logg%
echo Copy completed.. %logg%
net use \\%%A\c$\test /delete /Y %logg%
)
pause
Any help on how to finish this of would be appreciated.
I am struggling to save any errors in the output. If it errors I want it to just save that it failed in output and go on to the next IP in the IP address text file.
I think, I need to wrap an if around the net use checking that the ip address pings first. However this does not work.
#echo off
#setlocal enableextensions enabledelayedexpansion
if exist Transfer_logfile.txt (
ECHO Y | del Transfer_logfile.txt
)
set LogFile=_logfile.txt
set logg=^> _^&^& type _^&^&type _^>^>%LogFile%
for /F "tokens=*" %%A in (ipaddresses.txt) do (
ping -n %%A > NUL
IF ERRORLEVEL 0 (
echo
ELSE goto :skipcopyhost1
net use \\%%A\c$\test %logg%
echo copying files across to %%A... %logg%
COPY -f -R -Y C:\test\test.exe \\%%A\c$\test\test.exe %logg%
echo Copy completed.. %logg%
net use \\%%A\c$\test /delete /Y %logg%
)
pause
Try this not tested batch code:
#echo off
if not exist ipaddresses.txt goto :EOF
setlocal
del /F Transfer_logfile.txt 2>nul
set "LogFile=Transfer_logfile.txt"
for /F %%A in (ipaddresses.txt) do (
%SystemRoot%\System32\ping.exe -n %%A >nul
if errorlevel 1 (
echo %%A is not available in network.>>%LogFile%
) else (
echo Connecting to %%A ...>>%LogFile%
%SystemRoot%\System32\net.exe use X: \\%%A\c$\test /persistent:no /Y 2>&1 >>%LogFile%
echo Copying file to %%A ...>>%LogFile%
copy /B /V /Y C:\test\test.exe X:\test.exe 2>&1 >>%LogFile%
echo Disconnecting from %%A ...>>%LogFile%
%SystemRoot%\System32\net.exe use X: /delete /Y 2>&1 >>%LogFile%
)
echo.>>%LogFile%
)
endlocal
pause
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
del /?
for /?
goto /?
if /?
net /?
net use /?
set /?
Further read the Microsoft articles:
Using Command Redirection Operators
Testing for a Specific Error Level in Batch Files
I don't explain all the errors in your code as they are too many.

FOR and MOVE in same statement in a BATCH file

I am trying to move multiple .ARC files from sub-directories within a primary directory to one of the other sub-directories. I can get this FOR statement to run just fine if I manually type it in the command line, but running it from script doesn't seem to be working at all (with the variables assigned).
Can anyone see that I am doing something wrong? It's under my impression that I have all the correct syntax in place for this to work without any issues.
:main
setlocal enabledelayedexpansion
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
findstr " .*!succ!" !sumLog!
if exist errorlevel 0 (
pause
goto :move
) else (
goto :eof
)
:move
set acctDir3=media1
set x=x:\!acct!\!userDir!
set y=x:\!acct!\!userDir!\!acctdir3!
echo !x!
pause
echo !y!
pause
for /r "!x!" %%i in (*.ARC) do move "%%i" "!y!"
pause
:: for /r "X:\101004357\Jan_20_2014_6_56_55_953PM" %%g in (*.ARC) do move "%%g" "X:\101004357\Jan_20_2014_6_56_55_953PM\Media1"
endlocal
goto :eof
Please disregard the Access Denied as I will correct that issue on my own. However, the output I get if I manually run the same FOR statement from the command line is:
X:\>for /r "X:\101004357\Jan_20_2014_6_56_55_953PM" %g in (*.ARC) do move "%g" "X:\101004357\Jan_20_2014_6_56_55_953PM\Media1"
X:\>move "X:\101004357\Jan_20_2014_6_56_55_953PM\Media1\FILES1.ARC"
"X:\101004357\Jan_20_2014_6_56_55_953PM\Media1"
Access is denied.
0 file(s) moved.
X:\>move "X:\101004357\Jan_20_2014_6_56_55_953PM\Media2\FILES2.ARC"
"X:\101004357\Jan_20_2014_6_56_55_953PM\Media1"
Access is denied.
0 file(s) moved.
X:\>move "X:\101004357\Jan_20_2014_6_56_55_953PM\Media3\FILES3.ARC"
"X:\101004357\Jan_20_2014_6_56_55_953PM\Media1"
Access is denied.
0 file(s) moved.
If I run it from the script, I get no output except to go to :eof. Please see the output from the script:
Please type the 9 digit account number you would like to restore: 101004357
The account number you selected is: 101004357
Is this correct (Y/N)? y
1 Jan_14_2014_12_49_11_900PM
2 Jan_20_2014_6_56_55_953PM
Select a directory [1-2]: 2
You selected Jan_20_2014_6_56_55_953PM for your data retrieval.
[2014-01-23 12:15:14 InfoTrace (8209)]DVD Media image creation finished at: Thu Jan 23 12:14:50 2014
Press any key to continue . . .
x:\101004357\Jan_20_2014_6_56_55_953PM
Press any key to continue . . .
x:\101004357\Jan_20_2014_6_56_55_953PM\media1
Press any key to continue . . .
Press any key to continue . . .
If you notice between the double PAUSE syntax above is where the FOR statement should have ran. But it never does and moves to the second pause without any output or any kind of move, etc.
Any input would be greatly appreciated!
change
for /r "!x!" %%i in (*.ARC) do move "%%i" "!y!"
to
for /r "%x%" %%i in (*.ARC) do move "%%i" "%y%"
!var! is not evaluated until after the for loop is running. Only use !var! on a variable that has changed within a block statement (a parenthesised series of statements).
I'd also advise to not use a : in goto except for the special case of goto :eof
and
if exist errorlevel 0 (
means "if the file named 'errorlevelexists, execute the excutable named0`
No idea what you are trying to do here, but IF ERRORLEVEL n is TRUE if errorlevel is n or greater than n. IF ERRORLEVEL 0 is therefore always true. IF NOT ERRORLEVEL 1 is a test for errorlevel=0. So is IF %ERRORLEVEL%==0, except that the former can be used within a block but the latter cannot.

Read a variable from a text file

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"

Findstr batch file in finding listener down servers

Hi I'm trying to create a batch file to filter out servers which has RDP/ICA listener down from a list of servers in a notepad file, I created this script with the below syntax, but for some reasons it won't work as expected, can some one help me fix the situation?
I've a list of servers in computer.txt file and I'm trying to find the one's which are down and if errorlevel is 0, meaning the string down is found, I want the server name to be printed in listenerdown.txt , but for some reasons, if I execute the batch file, all the servers in computer.txt gets written to listenerdown.txt file
below is the batch file
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr/i down >nul 2>&1
if %errorlevel% neq 1
echo %%i >>Listenerdown.txt
)
Move the echo onto the same line as the if statement or else use parentheses to establish scope and %ErrorLevel% will always be 0 because the variable does not get set in a loop without delayed expansion.
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr /i down >nul 2>&1
if !errorlevel! neq 1 echo %%i>>Listenerdown.txt
)
endlocal
or
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr /i down >nul 2>&1
if !errorlevel! neq 1 (
echo %%i>>Listenerdown.txt
)
)
endlocal
Cmd.exe parses batch files line by line and unless you tell it that the scope of the command continues onto the next line it will think the command is finished.
You should use setlocal EnableDelayedExpansion, and !errorlevel! instead of %errorlevel%:
#echo off
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do (
qwinsta /server:%%i | findstr/i down >nul 2>&1
if !errorlevel! neq 1 echo %%i >>Listenerdown.txt
)
otherwise the value of errorlevel would be expanded only once, before entering the loop, and would not have the correct value. You also have to make sure that echo is in the same row as the if.

Determining if batch script has been started/executed from the command line (cmd) -or- To pause or not to pause?

I like to have a typical "usage:" line in my cmd.exe scripts — if a parameter is missing, user is given simple reminder of how the script is to be used.
The problem is that I can't safely predict whether potential user would use GUI or CLI. If somebody using GUI double-clicks this script in Explorer window, they won't have chance to read anything, unless I pause the window. If they use CLI, pause will bother them.
So I'm looking for a way to detect it.
#echo off
if _%1_==__ (
echo usage: %nx0: filename
rem now pause or not to pause?
)
Is there a nice solution on this?
You can check the value of %CMDCMDLINE% variable. It contains the command that was used to launch cmd.exe.
I prepared a test .bat file:
#Echo Off
echo %CMDCMDLINE%
pause
When run from inside of open cmd.exe window, the script prints "C:\Windows\system32\cmd.exe".
When run by double-clicking, it prints cmd /c ""C:\Users\mbu\Desktop\test.bat" "
So to check if your script was launched by double-clicking you need to check if %cmdcmdline% contains the path to your script. The final solution would look like this:
#echo off
set interactive=1
echo %cmdcmdline% | find /i "%~0" >nul
if not errorlevel 1 set interactive=0
rem now I can use %interactive% anywhere
if _%1_==__ (
echo usage: %~nx0 filename
if _%interactive%_==_0_ pause
)
Edit: implemented fixes for issues changes discussed in comments; edited example to demonstrate them
:: exit if not interactive
echo %CMDCMDLINE% | find /i "/c"
if not ERRORLEVEL 1 goto:eof
Here, I wrote something...
Usage.bat
#echo off
if arg%1==arg goto help
goto action
:action
echo do something...
goto end
:help
set help1=This is Help line 1.
set help2=This is Help line 2.
cmd.exe /k "echo %help1% &echo %help2%"
goto end
:end
It's not perfect, but it works! :D
-joedf
This is only using the internal command. so effectively....
EnableDelayedExpansion
if "!cmdcmdline!" neq "!cmdcmdline:%~f0=!" pause >nul
or
if not "!cmdcmdline!" == "!cmdcmdline:%~f0=!" pause >nul
DisableDelayedExpansion
if "%cmdcmdline%" neq "%cmdcmdline:%~f0=%" pause >nul
or
if not "%cmdcmdline%" == "%cmdcmdline:%~f0=%" pause >nul
Start your batch checking for %WINDIR% in %cmdcmdline% like this:
echo "%cmdcmdline%" | findstr /ic:"%windir%" >nul && (
echo Interactive run of: %0 is not allowed
pause
exit /B 1
)
Please use findstr
echo %cmdcmdline% | findstr /ic:"%~f0" >nul && ( pause >nul )
or
setlocal EnableDelayedExpansion
.
.
echo !cmdcmdline! | findstr /ic:"%~f0" >nul && ( pause >nul )
.
.
endlocal
This is always worked...
for internal command
setlocal EnableDelayedExpansion
set "cmddiff=!cmdcmdline:~0,1!" & if !cmddiff! neq ^" ( pause >nul )
endlocal
or
setlocal EnableDelayedExpansion
set "cmddiff=!cmdcmdline:~28,1!" & if !cmddiff! neq ^" ( pause >nul )
endlocal
You can compare the different thing, but this is only worked within EnableDelayedExpansion. and I don't think that this will be always worked, cause windows version, etc...
Similar approach...
setlocal
set startedFromExplorer=
echo %cmdcmdline% | find /i "cmd.exe /c """"%~0""" >nul
if not errorlevel 1 set startedFromExplorer=1
...
if defined startedFromExplorer pause
goto :EOF
setlocal EnableDelayedExpansion
if "!cmdcmdline!" neq "!cmdcmdline:%comspec%=!" ( pause>nul )
Test is done in Windows 10. Using %windir%, it is a little dangerous or ambiguous. So %comspec% is super safe.

Resources