Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:\downloads\data.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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Instead of:

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

use:

$dlfile = $dldir + '\data.csv'
share|improve this answer
    
Thanks, none of the documentation I read suggested I could do that. –  nakedhitman Sep 17 '12 at 22:35

Another option would be to use Join-Path

$dlfile = Join-Path $dldir '\data.csv'

The benefit of Join-Path is that you don't have to worry about leading or trailing slashes.. the command will clean that up for you and give you a nice clean file path.

So

$dlfile = Join-Path $dldir 'data.csv'

or

$dldir = 'c:\downloads\'
$dlfile = Join-Path $dldir '\data.csv'

or

$dldir = 'c:\downloads\'
$dlfile = Join-Path $dldir '\data.csv'

would all turn out with a valid path. This is very useful if you are taking user input for the file location and don't want to worry about sanitizing the input for trailing slashes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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