1

Good evening all,

Please excuse the basic question - I'm attempting to create a script to make some jobs at my workplace a little more efficient.

The script is designed to open a network share and create a folder within, ask for input from the user (which will be the local profile name), and if it exists, copy the contents to the folder we created on the network share. The script will then remove the network share.

The script is as follows (excuse the untidiness)

    @echo off

net use X: \\40aGlenwood-NAS\ROOM16-Backup

:UserInput
set /p "UserName=Enter the user's name: " 

IF NOT EXIST %UserName% GOTO UserInput
IF EXIST %UserName% GOTO Proceed

:Proceed
xcopy "C:\Users\%UserName%\Desktop" "X:\Backup" /f /r /i

net use X: /D /YES

timeout /t 10

This script works well in theory, however once run, the command box displays the following:

"Douglas" is not recognized as an internal or external command

See, the word 'Douglas' is part of the variable that we have declared. It isn't supposed to be a command.

Any help here? I know it's not the affecting the script, however I don't want it to display that message. Also, bear in mind that I have no previous experience coding or creating batch scripts.

3
  • There are several flaes, UserName is a predefined environment var already set with current users name. If you want to refer to the current user no need to ask. You aren't creating a dir in the code. To check the existence of a dir you'll need to include the path (and better double quote it) Commented Jan 20, 2017 at 21:36
  • Try in an open cmd window: IF NOT EXIST Joe Douglas echo GOTO UserInput. Use double quotes: IF NOT EXIST "Joe Douglas" echo GOTO UserInput. Commented Jan 20, 2017 at 21:48
  • LotPing - Doesn't the xcopy command create the directory if it doesn't already exist? And I've changed the variable to 'ProfileName' so thanks for that. The idea is to enter the name of a local profile on the PC that follows a specific naming criteria, which it will then copy to the network share. Thanks for the comment! Commented Jan 20, 2017 at 21:55

1 Answer 1

1

Give this a whirl:

@echo off
setlocal    
%SystemRoot%\System32\net.exe use X: \\40aGlenwood-NAS\ROOM16-Backup
if %ERRORLEVEL% NEQ 0 (
    echo Failed to map drive.
    goto END
    )

:UserInput
set /p USER="Enter the user's name: " 
IF NOT EXIST "c:\users\%USER%\." GOTO UserInput

%SystemRoot%\System32\xcopy.exe "C:\Users\%USER%\Desktop" "X:\Backup" /f /r /i
%SystemRoot%\System32\net.exe use X: /D /YES
%SystemRoot%\System32\timeout.exe /t 10

endlocal    
:END

As an aside, the explicit path references are there for security, i.e.: to prevent code execution of someone else's NET.EXE, etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.