-1

The script takes user inputs then crashes

Tried renaming files and variables, and combining variables.

cls
set /p FN="First Name: "
set /p LN="SECOND Name: "
set folder="%FN% %LN%"
set path="F:\ADMIN\USERS\_USER"
set final = %path%\%folder%
if not exist %final% (
    mkdir %path%\%folder%
    mkdir %path%\%folder%\Info
    mkdir %path%\%folder%\JOB
    mkdir %path%\%folder%\HR
    mkdir %path%\%folder%\Department
    copy %path%\%folder%\_takeoff.xlsx %path%\%folder%
) else (
    echo %path%\%folder% + " is already Created"
)
pause

Script crashes

EDIT: Here is the updated code. I creates the intended files in the intended location. However it also creates the files on the Desktop. Not sure if editing here is what I should to do or ask a new question.

echo off
cls
echo Type The SO number, Customer, and Job named when asked. Then press Enter
echo.
set /p SO="SO #: "
set /p Customer="Customer Name: "
set /p JOB="Job Name: "
set folder="%SO%"_"%Customer%"_"%JOB%"
echo %folder%
set dir="F:\Sales\SALES FOLDER\_QUOTES"
set final="%dir%\%folder%"
set dst="%final%/1 - Estimating Orginal Quote Material"
if not exist %final% (
    mkdir "%dst%"
    copy /-Y "%dir%\_takeoff.xlsx" %est%
    mkdir "%dst%\Downloads"
    mkdir "%dst%\Plans"
    mkdir "%dst%\Quotes"
    mkdir "%dst%\Cut Sheets"
    mkdir "%dst%\Venders"
    mkdir "%final%\2 - Signed Quote - Contract\Non-Current Purchase Orders"
    mkdir "%final%\3 - Special Purchase Trading Goods"
    mkdir "%final%\4 - Engineering\2 - Submittals"
    mkdir "%final%\4 - Engineering\3 - Approvals"
    mkdir "%final%\4 - Engineering\4 - Drawings in Progress\Released Production Drawings"
    mkdir "%final%\5 - Final Production Drawings"
    mkdir "%final%\6 - Q.C. Document"
    mkdir "%final%\7 - Project Management _ Schedule"   
) 
pause
gizmobrat
  • 5
  • 3
  • 2
    Your code does not make sense! If `"F:\ADMIN\USERS\_USER\FirstName Surname"` doesn't exist, i.e.`If Not Exist "%final%\"` it is not possible within that block to `Copy "F:\ADMIN\USERS\_USER\FirstName Surname\_takeoff.xlsx"` to `"F:\ADMIN\USERS\_USER\FirstName Surname"`, because you've just created `"F:\ADMIN\USERS\_USER\FirstName Surname"` and it has no content, i.e. `_takeoff.xlsx` is not located in the newly created directory yet! – Compo Jul 19 '19 at 14:50
  • @Compo, You are correct the copy path is meant to be %path%\_takeoff.xls. Got a little copy paste happy when making it – gizmobrat Jul 19 '19 at 14:56
  • 1
    I recommend reading [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) as well as [How to set environment variables with spaces?](https://stackoverflow.com/a/34402887/3074564) and [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) and finally [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) – Mofi Jul 20 '19 at 08:01
  • 1
    By the way, the script does not crash. Executables can crash, but not scripts processed by a script interpreter (executable) like `cmd.exe` in this case. See [debugging a batch file](https://stackoverflow.com/a/42448601/3074564) on how to solve such simple syntax errors by yourself next time. Windows command processor outputs an error message with information about cause of __exit__ (not crash) of script processing because of a serious syntax error making it impossible to continue processing of the batch file. – Mofi Jul 20 '19 at 08:04

2 Answers2

0

The issue is the spaces you have on the set final = %path%\%folder% line; if you take those extra spaces out you should be fine.

To take it a step further I highly recommend adding quotes to places that don't have them, and moving them ahead of the variables in your set statements to account for any spaces users may add:

cls
set /p "FN=First Name: "
set /p "LN=SECOND Name: "
set "folder=%FN% %LN%"
set "mypath=F:\ADMIN\USERS\_USER"
set "final=%mypath%\%folder%"
if not exist "%final%" (
    mkdir "%final%"
    mkdir "%final%\Info"
    mkdir "%final%\JOB"
    mkdir "%final%\HR"
    mkdir "%final%\Department"
    copy "%mypath%\_takeoff.xlsx" "%final%"
) else (
    echo %final% is already Created
)
pause

You also don't need the quotes in your echo, unless you just want it to echo quotes.

Compo
  • 30,301
  • 4
  • 20
  • 32
mael'
  • 420
  • 3
  • 7
0

A simpler looking idea:

PushD "F:\ADMIN\USERS\_USER" 2>NUL || Exit /B
ClS
Set "FN="
Set /P "FN=First Name: "
Set "LN="
Set /P "LN=Surname: "
Rem Note: You should perform some input verification here.

MD "%FN% %LN%" "%FN% %LN%\Info" "%FN% %LN%\JOB" "%FN% %LN%\HR" "%FN% %LN%%\Department" 2>NUL
If Not Exist "%FN% %LN%\_takeoff.xlsx" Copy "_takeoff.xlsx" "%FN% %LN%"

Please take note of the Remarked line, you currently have no control over what the end user enters at each prompt, they can enter nothing or anything, (including invalid characters)!

Compo
  • 30,301
  • 4
  • 20
  • 32