1

I am new to Batch coding. So struggling with basics. I want to concatenate 2 variables and then print its concatenated result in the third variable.

Batch code

@echo off
SET basePath = C:\Users\Documents\
echo  basePath - %basePath%
SET fileName = T_test 
echo fileName - %fileName%
SET extension= .csv
echo extension - %extension%

SET finalvalue = %basePath%%fileName%%extension%
echo finalvalue - %finalvalue%
pause

Actual Output

 basePath -
fileName -
extension -  .csv
finalvalue -

Expected Output

  basePath - C:\Users\Documents\
  fileName - T_test
  extension - .csv
  finalvalue - C:\Users\Documents\T_test.csv

*****Update******

Since the fileName variable has multiple values separated by a comma. I want to iterate it one-by-one

Batch Code

@echo off
    SET basePath=C:\Users\Documents\
    echo  basePath - %basePath%
    SET fileName=T_test,T_test2,T_test3 
    echo fileName - %fileName%
    SET extension=.csv
    echo extension - %extension%

     for %%f in (%fileName%) do (      
      SET completepath=%basePath%%%f%extension%
      echo completepath - %completepath%
      )
    pause

Actual Output

basePath - C:\Users\Documents\
fileName - T_test,T_test2,T_test3
extension - .csv
completepath -
completepath -
completepath -

Expected Output

basePath - C:\Users\Documents\
    fileName - T_test,T_test2,T_test3
    extension - .csv
    completepath - C:\Users\Documents\T_test.csv
    completepath - C:\Users\Documents\T_test2.csv
    completepath - C:\Users\Documents\T_test3.csv

Code Update 2:

@echo off
setlocal EnableDelayedExpansion 
SET basePath=C:\Users\Documents\
echo  basePath - %basePath%
SET fileName=T_test,T_test2,T_test3 
echo fileName - %fileName%
SET extension=.csv
echo extension - %extension%
for %%f in (%fileName%) do (    

:: below echo prints expected value i.e. 1st iteration -  T_Test, 2nd iteration - T_test2, 3rd iteration - T_test3
  echo current file name - %%f  

  SET completepath=%basePath%!f!%extension%  

:: below echo prints nothing, expected value 1st iteration -  C:\Users\Documents\T_test.csv, 2nd iteration - C:\Users\Documents\T_test2.csv   
    echo completepath - %completepath%  
)

pause
Vikas J
  • 795
  • 3
  • 14
  • 31
  • 3
    The spaces around the equal sign in a set command will become part of the variable name/content. So all your variable names have a trailing space not included when you try to output. Better also double quote when setting to avoid any unintended spaces, `set "basePath=C:\Users\Documents\"` –  Aug 15 '19 at 11:00
  • 1
    Do it like this, 1. `@Echo Off`, 2. `Set "basePath=%UserProfile%\Documents"`, 3. `Set "fileName=T_test"`, 4. `Set "extension=.csv"`, 5. `Set "finalvalue=%basePath%\%fileName%%extension%"`, 6. `Set finalvalue`, 7. `Pause` – Compo Aug 15 '19 at 11:11
  • Thanks @LotPings it's working, but that was just 1 part of it. I have updated my post. please check. – Vikas J Aug 15 '19 at 11:22
  • 3
    Read about [delayedexpansion](http://ss64.com/nt/delayedexpansion.html) –  Aug 15 '19 at 11:25
  • Thanks for the link, Based on the link I made changes in the script, Now it is able to iterate fileName but while concatenating it prints nothing. Please check Updated Code 2 section of my post. – Vikas J Aug 15 '19 at 12:34
  • For meta variables do work only as `%%f`, you ***don't have*** a regular variable `f` you could apply the exclamation mark to `!f!`, OTOH `completepath` was set inside a code block and ***needs*** them `echo completepath - !completepath!` –  Aug 15 '19 at 13:10
  • Also, use `rem` within a code block, not `::` – Magoo Aug 15 '19 at 13:50
  • See also: [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) It contains a comprehensive explanation on how to work with command `set`. – Mofi Aug 15 '19 at 14:02

1 Answers1

5

Here's an example of your 'Code Update 2', complete with the recommended Set syntax and appropriate use of delayed expansion.

@Echo Off
SetLocal DisableDelayedExpansion 
Set "basePath=%USERPROFILE%\Documents"
Echo basePath - %basePath%
Set "fileName=T_test,T_test2,T_test3"
Echo fileName - %fileName%
Set "extension=.csv"
Echo extension - %extension%
For %%A In (%fileName%) Do (
    Echo current file name - %%A
    Set "completepath=%basePath%\%%A%extension%"
    SetLocal EnableDelayedExpansion
    Echo completepath - !completepath!
    EndLocal
)
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    Do you also feel like [Sisyphus](https://en.wikipedia.org/wiki/Sisyphus) sometimes? +1 for your continued efforts ;-) –  Aug 15 '19 at 13:16