Simple concatenation fails

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
karlik
Posts: 1
Joined: 18 Dec 2015 17:10

Simple concatenation fails

#1 Post by karlik » 18 Dec 2015 17:17

Hello,

I just try to concatenate the following variables:
@echo off
FOR /F "tokens=1,2,3 delims=." %%a in ("%date%") do set yy=%%c& set mm=%%b & set dd=%%a
set /a lastyear=%yy%-1
set output=IS_%lastyear%

echo %output%

_________________________
Output:
IS_

Output should be IS_2014

Can somebody help me?

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Simple concatenation fails

#2 Post by Meerkat » 18 Dec 2015 21:07

Fixed Code:

Code: Select all

@echo off
FOR /F "tokens=2,3,4 delims=/" %%a in ("%date: =/%") do set yy=%%c& set mm=%%b & set dd=%%a
set /a lastyear=%yy%-1
set output=IS_%lastyear%
echo %output%
Output:

Code: Select all

IS_2014


Have a look in the %date% variable:

Code: Select all

>echo %date%
Wed 12/16/2015
The delimiters here are [space] and Slashes. I used %date: =/% in IN so that the delimiter will only be slashes.

Code: Select all

>echo %date: =/%
Wed/12/16/2015

Meerkat

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Simple concatenation fails

#3 Post by thefeduke » 20 Dec 2015 15:39

This is simpler, but works as well:

Code: Select all

FOR /F "tokens=2,3,4 delims=/ " %%a in ("%date%") do set yy=%%c& set mm=%%b & set dd=%%a
John A.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Simple concatenation fails

#4 Post by foxidrive » 20 Dec 2015 18:13

There are spaces there which will be a problem.

Post Reply