PowerShell overwrites source variable when I create new concatenated variable

nakedhitman asked:

I have a large script that will be asking for a download directory and downloading a bunch of files into it. However, when I set the download filename string based on the directory string, they are both overwritten with the full filename string. Here’s what I mean:

$dldir = 'c:downloads'
$dlfile = $dldir += 'data.csv'

This results in both $dldir and $dlfile to be set to “c:downloadsdata.csv”. Obviously, since I want to reuse $dldir to set $dlfile multiple times in the script, I don’t want it to change. Does anyone know how to do this?

Thanks.

My answer:


Instead of:

$dlfile = $dldir += 'data.csv'

use:

$dlfile = $dldir + 'data.csv'

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.