Access is denied creating text file in cmd & weird "if exist" problem - batch-file

For a batch file project of mine, I had to create two text files if they didn't already exist. For some reason the 'if exist "example.txt"' didn't work. What I mean by that is:
if not exist "example.txt" goto :label
wouldn't go to 'label' when the file did not exist. So, i presumed the file must be existing already (from my previous tests). The thing is that
if exist "example.txt" goto :label2
always went to 'label2', even though later in the script cmd could not find the file, and the folder is empty except the batch script (the file didn't exist). I decided I'd test the label for if the file didn't exist anyway. when I used the break> command to create the text file, it came back as access is denied. It did the same thing with the following code which i used to enter information:
>> "example.txt" echo 0
and with
del/f "example.txt"
I am a full administrator on my computer, the code I use works in other batch files I have created. I have tried running cmd in administrator (same outcome). Specifying the directory works no better in this case than using current directory. I'm wondering what is the cause of this is.
Anyway, here is the full batch script.
#echo off
title Folder Safe
color e8
if exist "infocj.txt" goto start
goto MAKESOURCE
:MAKESOURCE
rem notes: access is denied. this means that createproc doesn't work (createproc relies on makesource).
break>"infocj.txt"
attrib +s +h +r "infocj.txt"
break>"setupcj.txt"
>> "setupcj.txt" echo 0
attrib +s +h +r "setupcj.txt"
goto START
:START
echo what safe do you want to unlock (number only)
set/p "loc=>"
if exist %loc% goto LOCK
if exist Locked%loc% goto UNLOCK
echo there is no safe hashing to %loc%. do you wish to create a new safe? (y/n)
set/p "cho=>"
if %cho%==y goto createpass
exit
:LOCK
cls
For /f "tokens=%loc% delims=;" %%i in (infocj.txt ) do set pass=%%i
echo enter password to lock Safe %loc%
set/p "qaz=>"
if not %qaz%==%pass% goto END
ren %loc% Locked%loc%
attrib/d +h +s +r Locked%loc%
exit
:UNLOCK
cls
For /f "tokens=%loc% delims=;" %%i in (infocj.txt ) do set pass=%%i
echo enter password to unlock Safe %loc%:
set/p "qaz=>"
if not %qaz%==%pass% goto END
ren Locked%loc% %loc%
attrib/d -s -h -r %loc%
exit
:createpass
echo enter password:
set/p "pass=>"
cls
echo confirm password:
set/p "npass=>"
cls
if not %pass%==%npass% (
echo the passwords you entered are not matching
timeout /t 2 /nobreak > NUL
goto createpass
)
goto createproc
:createproc
rem this stuff is screwed because the stuff under makesource is not working properly, but even if I do all that manually, a lot of this still returns access is denied
for /f "tokens=* usebackq" %%a in ("infocj.txt") do set inf=%%a
set inf=%inf%%pass%;
attrib -s -h -r "infocj.txt"
del/f "infocj.txt"
break>"infocj.txt"
>> "infocj.txt" echo %inf%
attrib +s +h +r "infocj.txt"
for /f "delims=" %%b in ("setupcj.txt") do set stp=%%b+1
md %stp%
attrib -s -h -r "setupcj.txt"
del/f "setupcj.txt"
break>"setupcj.txt"
>> "setupcj.txt" echo %stp%
attrib +s +h +r "setupcj.txt"
echo your locker hashes to the number %stp%
pause
exit
Thanks in advance for your answers. This one's got me truly stumped.

Related

Batch file ends unexpectedly. Has been working for years but has suddenly stopped working

I have a batch file which hids a folder. It has been working for years but has suddenly stopped working and I can't work out why.
#ECHO OFF
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Private goto MDPrivate
:CONFIRM
echo Are you sure you want to lock this folder? (Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock Your Secure Folder
set/p "pass=>"
if NOT %pass%== takingcareofbusiness goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Private
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDPrivate
md Private
echo Private created successfully
goto End
:End
The problem initially presents as a problem with the attrib settings not being applied in the LOCK section but on further investigation (putting an echo after the ren command), the batch file closes after the ren command.
What's even more weird is that is runs perfectly well in Command Prompt but not when running it from Windows Explorer.
User has Administrator priviledge but it just stops working.
Completely untested as I am not even on my Windows device. but some things to note. Do not use set /p when dealing with choice selection. You can use choice.exe for that.
When comparing items using if always follow the trend on both sides by matching exactly between double quotes. i.e if "var" == "%var%"
#ECHO OFF
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST "Private" goto MDPrivate
choice /c yn /m "echo Are you sure you want to lock this folder?"
if errorlevel 2 goto :EOF
ren "Private" "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto :EOF
:UNLOCK
echo Enter password to Unlock Your Secure Folder
set/p "pass=>"
if NOT "%pass%" == "takingcareofbusiness" goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" "Private"
echo Folder Unlocked successfully
goto :EOF
:FAIL
echo Invalid password
goto :EOF
:MDPrivate
md Private
echo Private created successfully

Testing Folder Attributes using .bat

iam using .bat code to check folder attribute if its ( Hidden + System ) then change it to ( Not Hidden + Not System )
"MTD" is Folder
Code:
if attrib +h +s "MTD" attrib -h -s "MTD"
Thanks
REWRITE With thanks to #Compo and a post from dbenham (https://stackoverflow.com/a/8669636/447901), this is completely rewritten. It is -very- hardcoded for specific character positions which is not a good idea.
When it appears that the correct ATTRIB commands will be run on the correct directories, remove the echo from the ATTRIB command.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "delims=" %%A IN ('DIR /B /A:H') DO (
FOR /F "delims=" %%B in ("%%~aA") DO (
SET ATTRS=%%~B
if "!ATTRS:~0,1!" == "d" if "!ATTRS:~3,2!" == "hs" (
echo ATTRIB -H -S %%~A
)
)
)
This would be far better done in PowerShell. This will require the current PowerShell 5.x or higher. In fact...
Get-ChildItem -Directory -Hidden -System |
ForEach-Object {
$_.Attributes -= 'Hidden'
$_.Attributes -= 'System'
}
Hi All i found solution
cls
#ECHO OFF
title Folder Locker
:CONFIRM
echo Are you sure u want to Lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto UNLOCK
if %cho%==N goto UNLOCK
echo Invalid choice.
goto CONFIRM
:LOCK
attrib +h +s "MTD"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%== 123 goto FAIL
attrib -h -s "MTD"
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:End

Creating and configuring remote server shares in BATCH

I took this portion out of a larger script that I'm modifying in order to create and set the permissions to shares on remote file servers. I know this can done with Powershell but there are legacy servers preventing me from using it in this case unfortunately.
My concerns are that I'm not sure you can stack the psexec commands the way that I'm attempting to, that it is processing the parenthesis, and that it may not be passing on the variables to psexec properly. Trying to use call in this case may also add a lot of complexity but may be an option.
#echo off
setlocal EnableDelayedExpansion
set Fs1FQDN=fs1.city.local
set Fs2FQDN=fs2.city.local
set Fs3FQDN=fs3.city.local
set BSTFQDN=bst-fs1.city.local
:: this is set to whatever server is being tested
set UserFileServer=fs1.city.local
set UserName=footest
:: please note that for icacls I am setting up a user I know exists to insure that it can appropriately set permissions
:EMPTY_FUNCTION
echo.
echo EXECUTING CREATEUSERFILESHARE
echo.
CREATEUSERFILESHARE
:CREATEUSERFILESHARE
if UserFileServer==%Fs1FQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs1FQDN% -e cmd /c mkdir E:\UDrives\%UserName% ^& attrib +h E:\UDrives\%UserName% /s /d ^& icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else if UserFileServer==%Fs2FQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs2FQDN% -e cmd /c (mkdir E:\UDrives\%UserName% ^& attrib +h E:\UDrives\%UserName% /s /d ^& icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else if UserFileServer==%Fs3FQDN% (
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs3FQDN% -e cmd /c (mkdir E:\UDrives\%UserName% ^& attrib +h E:\UDrives\%UserName% /s /d ^& icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else if UserFileServer==%BSTFQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%BSTFQDN% -e cmd /c (mkdir D:\UserHomeDrive\UDrives\%UserName% ^& attrib +h D:\UserHomeDrive\UDrives\%UserName% /s /d ^& icacls "D:\UserHomeDrive\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else (
echo.
echo Invalid selection or no file server specified. Skipping user folder creation...
echo
goto CLEANUP
:CLEANUP
echo.
echo CLEANUP INITIATED
exit
I did get this resolved. So it turns out you don't need to stack remote commands to the terminal in psexec. Since each successful remote command returns an error code it will proceed to the next set of commands. Please note that you will not get any sort of error code back unless you log it on the machine you are performing remote executions on.
A working chunk of the originally posted code would be.
:CREATEUSERFILESHARE
if UserFileServer==%Fs1FQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs1FQDN% -e cmd /c mkdir E:\UDrives\%UserName%
psexec \\%Fs1FQDN% -e cmd /c icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%:(OI)(CI)M"
echo. User folder successfully created
echo.
goto CLEANUP

Password Protect any folder

I used a tutorial at this link for protecting one of my important files,
The CODE is:
cls
#ECHO OFF
title coolhacking-tricks.blogspot.com
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto
UNLOCK
if NOT EXIST MyFolder goto MDMyFolder
:CONFIRM
echo Are you sure to lock this folder? (Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren MyFolder "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock Your Secure Folder
set/p "pass=>"
if NOT %pass%== coolhacks goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" MyFolder
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDMyFolder
md MyFolder
echo MyFolder created successfully
goto End
:End
But when I run lock.bat I don't get a screen asking for a password. Instead a screen appears and states: "the system cannot find the batch label specified" and suddenly the screen disappears without waiting a fraction of a second. The uploader was unable to help me with my problem.
Run this in the the root folder of your drive (assuming your script used the same GUID as the script on that page):
dir /b /s /a:s *.{21EC2020-3AEA-1069-A2DD-08002B30309D}
It'll take less time if you can narrow down the location to a particular parent folder.
Unhide the folder with these two commands:
attrib -s -h "C:\path\to\Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "C:\path\to\Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" newname
And next time steer clear of crappy "security tools" like that. It doesn't password-protect anything in the first place. It merely renames a folder an sets the hidden and system attributes.
Maybe use this (but re:
cls
#ECHO OFF
title Folder Private
if EXIST "HTG Locker" goto UNLOCK
if NOT EXIST Private goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "HTG Locker"
attrib +h +s "HTG Locker"
echo Folder locked
goto End
:UNLOCK
echo Enter password to unlock folder
set/p "pass=>"
if NOT %pass%== beef goto FAIL
attrib -h -s "HTG Locker"
ren "HTG Locker" Private
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Private
echo Private created successfully
goto End
:End

fix variable = variable in a batch file for a password

in this batch file i am trying to created a program where the user can enter his or her desired password and use it when ever they open or close the Batch file.
the problem is that i can not get the user password and (at :MDLOCKER and :UNLOCK) and the unlock part of the script to work. when finally get it to work it accepts any password
if you could help that would be great thanks.
enter code here#ECHO OFF
prompt Filelocker`enter code here`
:START
echo what do you want to do? (insert number)
echo 1 Lock current folder
echo 2 Unlock current folder
echo 3 Make new locked folder
set/p "cho=>"
if %cho%==1 goto CONFIRM
if %cho%==2 goto UNLOCK
if %cho%==3 goto NEW
echo not valid
goto start
: NEW
title Folder Locked files
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto ALREADY
if NOT EXIST Locker goto MDLOCKER
:ALREADY
echo folder already exist!
echo try unlocking if folder can not be found
pause
goto START
:CONFIRM
echo Are you sure u want to Lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto end
:MDLOCKER
md Locker
echo Locked folder created....
echo folder is now created
echo enter password for your file.
set/p 1%=
echo password accepted
goto start
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%==%1% goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
echo 2 MORE TRYS LEFT
pause
goto
:End
I don't think you can change the positional parameters %1 .. %9. Try saving your password into a variable instead.
the problem is here:
set/p 1%=
Why are you doing that?
You can't use a special var like a var-name.
And you can't use numbers at the start of a var-name.
and you can use the command Choice instead of all the IF statements that you are using:
#ECHO OFF
prompt Filelocker`enter code here`
:START
echo what do you want to do? (insert number)
echo 1 Lock current folder
echo 2 Unlock current folder
echo 3 Make new locked folder
choice /C 123 /M "choose an option: "
IF %ERRORLEVEL% EQU 1 (goto :CONFIRM)
IF %ERRORLEVEL% EQU 2 (goto :UNLOCK)
IF %ERRORLEVEL% EQU 3 (goto :NEW)
:NEW
title Folder Locked files
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" (
echo folder already exist!
echo try unlocking if folder can not be found
pause
goto :START
)
if NOT EXIST "Locker" (goto :MDLOCKER)
:CONFIRM
choice /C YN /M "Are you sure u want to Lock the folder? "
IF %ERRORLEVEL% EQU 1 (goto :LOCK)
IF %ERRORLEVEL% EQU 2 (goto :END)
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto :end
:MDLOCKER
md "Locker"
echo Locked folder created....
echo folder is now created
echo enter password for your file.
set/p "VAR=>> "
echo password accepted
goto :start
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>> "
if NOT "%pass%"=="%VAR%" goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
echo 2 MORE TRYS LEFT
pause
goto :UNLOCK
:End

Resources