Batch file that asks for user name and performs processes - batch-file

I work in IT in a GIS group (mapping). We have a server with folders for all of our GIS users. Whenever a new GIS-using co-worker is identified, because GIS data can be HUGE, they are given READ access to ALL folders on that server and a new folder just for them is created and they are given WRITE access to it.
I would like to create a batch process that would ask for a username, then give that username READ access to all folders, ask for a foldername and create that folder and give that username WRITE access to that folder.
How would I do this?

That what you are trying to do is an login script.
I have wrote an example with 3 users:
#echo off
:start
set /p username=Insert Username:
if /i "%username%"=="User1" goto User1
if /i "%username%"=="User2" goto User2
if /i "%username%"=="User3" goto User3
goto start
:User1
set /p folder1=Insert Foldername:
mkdir %folder1%
goto stop
:User2
set /p folder2=Insert Foldername:
mkdir %folder2%
goto stop
:User3
set /p folder3=Insert Foldername:
mkdir %folder3%
goto stop
:stop
cls
exit
The permissions are allowed by default.
I hope I was able to help you.

Related

Batch file to output file name from variable

I am trying to assign a file name from the variable which has been previously given by the user. How can I do it? I am a bit stuck with my code (attached below) Could you please give me some advice?
#ECHO OFF
CLS
mode con:cols=90 lines=30
SET /P name="Please enter name: " &:: Asking for name and store it as name
mkdir "C:\%name%"&ECHO Dump folder created
ECHO.
TIMEOUT /T 3 /nobreak>NUL
SET /P decision="Do you want to run all modules? Y-Yes N-No" &:: Decision to execute all modules or just one
IF %decision%==y (GoTo Full)
ELSE IF %decision%==Y (Goto Full)
ELSE IF(GoTo Basic)
:Full
>C:\%name%\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt
#ECHO OFF
[some commands]
GoTo End
:Basic
ECHO "WORKING?" &::This bit need to be completed lol
GoTo End
:End
TIMEOUT /T 5
I am a little confused as to what you are trying to do. I think that you want to create a directory and then put a file in the directory. If I wanted to create a file and put it in a folder that has the same name, I would do
set /p %name%=
mkdir C:\%name%
(
echo Works!
)>C:\%name%\%name%.txt
Hope this helps.

Batch script to Read the filenames in a directory and ask user to select one

Thanks for looking into my concern.
I have five config files in a given directory. In my batch script, I want to read those file names and prompt them to user. Once user has selected a config file, read the variables from it.
Could anyone help me with some logic here. So that, I can take it forward.
Thank you.
A batch or .cmd file like this demonstrates the menu technique (nothing fancy, the user has to enter the filename precisely). Key items:
FOR
SET /P
IF EXIST
Good luck!
#echo off
REM Show the user the list and ask them which one to use
echo.
echo Please select one of:
echo.
for %%F in ("D:\A Given Directory\*.config") do echo %%~nxF
echo.
set SEL_CFGFNM=
set /P SEL_CFGFNM=Which configuration file:
REM Make sure they answered, and that the file exists
if "%SEL_CFGFNM%" == "" goto ENDIT
if NOT EXIST "D:\A Given Directory\%SEL_CFGFNM%" goto NOCFG
REM User has selected file "D:\A Given Directory\%SEL_CFGFNM%" and it exists
REM Do whatever you want to do with that file now
REM Don't fall through the exit messages
goto ENDIT
REM Exit Messages
:NOCFG
echo.
echo ERROR: Configuration file "%SEL_CFGFNM%" is not on the list
echo.
goto ENDIT
REM Cleanup
:ENDIT
set SEL_CFGFNM=

Batch file to delete files from users on network

I want the batch file to ask for Serial number and username and delete two specific folders from users profile. I made this but it seems to want to delete *.* from folder I am running it from.
#echo off
set /p serial="Enter Serial: "
set /p username="Enter Username: "
del *.* \\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations
del *.*
\\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations
pause
Try next approach with basic checking all user's input:
#ECHO OFF
SETLOCAL EnableExtensions
:inputServer
set "serial="
set /p "serial=Enter Serial: "
if not defined serial goto :endlocal
pushd "\\%serial%\C$\"
if errorlevel 1 (
echo wrong server name "\\%serial%"
goto :inputServer
)
:inputUser
set "_username="
set /p "_username=Enter Username: "
if not defined _username goto :endstack
cd "\users\%_username%\appdata\roaming\Microsoft\Windows\Recent"
if errorlevel 1 (
echo wrong user name "%_username%" or path
echo "\\%serial%\C$\users\%_username%\appdata\roaming\Microsoft\Windows\Recent"
goto :inputUser
)
dir AutomaticDestinations\*.*
dir CustomDestinations\*.*
:endstack
popd
:endlocal
pause
Replace dir with del /Q no sooner than debugged.
Resource for PUSHD - POPD pair:
When a UNC path is specified, PUSHD will create a temporary drive
map and will then use that new drive. The temporary drive letters are
allocated in reverse alphabetical order, so if Z: is free it will be
used first.
c$ is administrator hiding ressources, you can't access to it without login
If you want to connect to network share on the remote computer, use this:
net use * \servername\sharename
The syntax of the del command does not allow to separate the file mask from the path where the files to delete are stored, so you need to use
del /q "\\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations\*.*"
del /q "\\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations\*.*"
The added quotes will prevent problems with paths that include spaces and the /q switch will avoid the security confirmation to delete the all the files in the indicated folder.
First, remove the #echo off untill your code is working so you can see what's happening, then add some echo to see what is stored in the variable that you're using. After that try with DIR instead of DEL so it list the file that match your criteria.
REM #echo off
set /p serial="Enter Serial: "
Echo %serial%
pause
set /p username="Enter Username: "
echo %username%
pause
dir \\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations\*.*
dir \\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations\*.*
pause

Batch File Using Net User "Username Not Found" Batch Hangs No Prompt

My batch script does the following:
user types in username and is added to a variable
trick to ask for password (hides input from user) and adds to variable
checks username and password authentication for domain GROUP using "net user" command
If user is found in set group, continue to map drive.
If user is not part of group restart at beginning
This script works when the username is found.
This script works when a username is found in a group
The problem is if the username is NOT FOUND.
When the user is NOT FOUND, it reports the "More help is available by typing NET HELPMSG 2221."
It just sits there and does not continue to prompt or anything.
I echoed the errorlevel and it comes out to 0, and reports the 0. However it still just sits there never reaching command prompt or going where the GOTO tells it.
I have put in errorlevels and I am unsure why it is stuck after the error message and does not continue. It is as if the batch script is not releasing from somewhere.
#echo off
:Question
Echo.
Echo.
SET /P HelperName=Enter Witness' Name:
Echo.
If %HelperName% EQU %Username% GOTO SameUserName
cls
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>%temp%\ftp.com
set /p password=What is your password? <nul
for /f "tokens=*" %%i in ('%temp%\ftp.com') do set "password=%%i"
del %temp%\ftp.com
cls
set i=0
set group=WGD
set user=%HelperName%
echo Checking if %user% is member of %group%...
for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1
if %i% gtr 0 (goto :member)
:nomember
echo %user% is not member of %group% Please Try Again
goto :question
:member
net use L: \\10.10.10.10\foldernamehere\TEMP /user:wgd\%helpername% %password%
if [%errorlevel%]==[0] goto deletedrive
goto error
:deletedrive
net use /delete L:
goto start
It is as if the script in still in another function.
Well it seems like I was not waiting long enough for the message to finish. It hangs for a bit, then continues on through the script. The wait however for the help message to clear is a little long, if you know how to clear faster please let me know.

Need Assistance in creating a password/username system with batch

Okay so heres what I'm trying to do. I'm trying to make a batch file with a user input for a login. I would like to have the ability to create a new account. I have an idea, I just don't know how to go around implementing it. I have the user input his desired username and password and have it save to a text document like so:
#echo off
set /p user=Enter your desired username:
set /p pass=Enter your desired password:
echo %user% >> log.txt
echo %pass% >> log.txt
That works fine for me, but now what I would like to do is call up those two lines so that when they entered the correct username and password it will take them to their menu.
I know for a fact the the call won't work well with this. Is there any way to do what I'm trying to do?
P.S. I am aware that the txt file is not secure. I have ways around that.
You may do this to recover the saved values:
(
set /P savedUser=
set /P savedPass=
) < log.txt
This way:
if "%user%" == "%savedUser%" if "%pass%" == "%savedPass%" goto accessGranted
Another way to save the values is this:
echo set savedUser=%user%> log.bat
echo set savedPass=%pass%>> log.bat
and to recover the saved values:
call log
I'm decently proud of this :) This is pretty much a complete overhaul of your method.
This has two different files: login.bat which handles the login and login2.bat which handles the registration.
login.bat:
#echo off
choice /c:RL /m "Choose an option: Register (R) or Login (L).:
if errorlevel 2 goto login
if errorlevel 1 goto register
:register
start /wait C:\[path]\login2.bat
cls
goto login
:in
cls
echo Welcome %u%
echo.
echo Bla Bla Bla or start "a program"
pause
exit
:login
set /p u=Username
set /p p=Password
and login2.bat:
#echo off
:a
set /p a="Choose a Username"
set /p b="Choose a Password"
echo.
choice /m "Are you sure you would like your Username to be %a% and your Password to be %b%?"
if errorlevel 2 goto a
set q="if %%u%% equ %a% if %%p%% equ %b% goto in"
for /f "tokens=*" %%I in (%q%) do set m=%%I
echo %m% >>C:\[path]\login.bat
exit
The reason why :in, the label which handles what happens after you login successfully has to be in the middle instead of at the end of login.bat is so that the username/password combinations could be appended to the :login label. Obviously you could add other embellishments and change the name of the files, but this is my basic design.
Hope this helps!
This is what I do
#echo off
:: some code here
set /p username=please create a username:
set /p password=please create a password:
echo %username%>username.txt
echo %password%>password.txt
This will create a text file for both your username and password
I hope this helped. :)

Resources