0

I am trying to learn more about batch scripts and I have googled this question without really understand what I am reading.

So i have the following batch script to create multiple folders. The batch script creates the folders with the current date in the folder name. It also prompts the user for the folder name. The only thing I cant get right is how I can have spaces in the folder name. If I call the folder e.g My documents nothing will happen (the batch will not work).

So how can I enable this batch to accept spaces in the file name?

:: DATE SETTINGS
        @ECHO off
        SETLOCAL ENABLEEXTENSIONS
        if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
        for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
          for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
            set '%%a'=%%i
            set '%%b'=%%j
            set '%%c'=%%k))
        if %'yy'% LSS 100 set 'yy'=20%'yy'%
        set Today=%'yy'%-%'mm'%-%'dd'% 
    ENDLOCAL & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%
:: ECHO Today is Year: [%V_Year%] Month: [%V_Month%] Day: [%V_Day%]
::------------------------------------------------------------------------------------------------
::AskForFolderName
set /p NewFolder=Enter folder name here:
If [%NewFolder%]==[] Goto AskForFolderName
If Exist "%NewFolder%" (
   Echo Folder already exists
   Echo.
   Goto AskForFolderName
)
::------------------------------------------------------------------------------------------------
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\RAW IMAGES"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\EXPORTS\JPG"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\EXPORTS\PNG"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\EXPORTS\PSD"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\VIDEO\RAW VIDEO"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\VIDEO\EDITS"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\VIDEO\EXPORTS"

1 Answer 1

2
If [%NewFolder%]==[] Goto AskForFolderName

let's disassemble this line, assuming %newfolder% is My Documents:

If [My Documents]==[] Goto AskForFolderName

if syntax is if argument1 comparison argument2 command

argument1 is [My, comparison is Documents], wait... What!? Syntax Error!

The batch-way of handling spaces is enclosing the string in qoutes:

If "%NewFolder%"=="" Goto AskForFolderName

argument1 is "My Documents", comparison is ==, argument2 is "", command is Goto ... - syntax ok.

(Interesting detail: in the very next line (if exist ...) you use the string quoted correctly)

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

1 Comment

Very well explained and perfect solution!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.