1

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.

2 Answers 2

1

Instead of:

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

use:

$dlfile = $dldir + '\data.csv'
1
  • Thanks, none of the documentation I read suggested I could do that.
    – MrDrMcCoy
    Sep 17, 2012 at 22:35
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .