0

In Windows Batch I have 2 variables FOLDER_WITH_PATH and BACKUP_FILE. I need to have a variable FULL_BACKUP_FILE whose value is concatenating FOLDER_WITH_PATH and BAKUP_FILE, with a backslash in between.

So, for example, if FOLDER_WITH_PATH="E:\Backup\Proj1" and BACKUP_FILE="version_1.txt", then FULL_BACKUP_FILE should be "E:\Backup\Proj1\version_1.txt"

How can I do that?

I have tried:

set FULL_BACKUP_FILE=%FOLDER_WITH_PATH%\%BACKUP_FILE%

and:

set "FULL_BACKUP_FILE=%FOLDER_WITH_PATH%\%BACKUP_FILE%"

but the value of FULL_BACKUP_FILE is wrong as

> "E:\Backup\Proj1"\\"version_1.txt"

How can I get the right value?

Compo
  • 36,585
  • 5
  • 27
  • 39
srh
  • 1,661
  • 4
  • 30
  • 57
  • Please read my answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) It explains in full details the difference between `set variable="value"` and `set "variable=value"` and why the latter is highly recommended, especially on string values are later concatenated together. There must be enabled the __command extensions__ which are enabled by Windows default, but better would be the command line `setlocal EnableExtensions DisableDelayedExpansion` below the first line `@echo off` to make it sure. – Mofi Jul 07 '23 at 04:54
  • So, use `set "FOLDER_WITH_PATH=E:\Backup\Proj1"` and `set "BACKUP_FILE=version_1.txt"` and `set "FULL_BACKUP_FILE=%FOLDER_WITH_PATH%\%BACKUP_FILE%"` and reference the resulting value with `"%FULL_BACKUP_FILE%"`. That highly recommended syntax can be seen on thousands of answers on Stack Overflow with the tag [batch-file](https://stackoverflow.com/tags/batch-file/info). – Mofi Jul 07 '23 at 04:59
  • If the name and value pairs are being read from a file, meaning you cannot change them, try removing the doublequotes during the definition of the final variable: ```Set "FULL_BACKUP_FILE=%FOLDER_WITH_PATH:"=%\%BACKUP_FILE:"=%"``` Then reference it using `"%FULL_BACKUP_FILE%"` if you need to quote it in use. – Compo Jul 07 '23 at 10:32

1 Answers1

0

You should define variables as set "<<name>>=<<value>>" and when y need to pass/call it, do so as "%<<name>>%" . In Cmd, value encapsulation is very weak .

If you rather want to edit the provided input, see this: https://ss64.com/nt/syntax-substring.html .

  • But be aware that each time you reassign the variable, special characters in it will be unfolded . So you should avoid this .
irvnriir
  • 673
  • 5
  • 14