How can I combine variables to name a folder?

Here is my current code. My script has delayedexpansion enabled:

mkdir "%myPath%\"%Letter%""!Number!""
  • Are you using delayed variable expansion? If so, that needs to be mentioned in your question. – I say Reinstate Monica May 12 '19 at 15:14
  • Yes, I'm using delayed variable expansion. I've added it to the question. – relo999 May 12 '19 at 15:29
  • Use only one pair of double quotes enclosing the whole path => mkdir "%myPath%\%Letter%!Number!"" If you aren't sure when to use the exclamation mark instead of a percent sign, here you can use it for all vars. – LotPings May 12 '19 at 15:35

You only need a single set of double-quotes, like this:

mkdir "%myPath%\%Letter%!Number!"

Sometimes it helps make your code more readable by building your variables in stages. For example:

Set Fullpath=%myPath%\%Letter%
MkDir "%Fullpath%!Number!"

Note that when using this approach it's typically best to not put the double-quotes into the variables containing path elements and only include them in the actual executing commands. This reduces confusion and adds the flexibility of allowing you to combine variables containing path elements in various ways without having to worry about ending up with too many quotes as was the problem with your original statement.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.