Author Posts

January 1, 2012 at 12:00 am

by ceem at 2012-10-04 04:27:18

Hey guys

I have a problem I couldn't figure out by myself:
foreach ($file in $trkfiles){
copy-item $trkfolder\$file "Y:\Logfiles\$hname_$file"
}

For some the file won't be renamed during the copy because of the "_". If I'm using "-" everything works as it should.
Any ideas how I can rename the file with an "_" in it?

Thanks

by mjolinor at 2012-10-04 06:25:28

Try:
"Y:\Logfiles\$($hname)_$($file)"

by poshoholic at 2012-10-04 06:56:08

Mjolinor is right. When you use a variable name in a string, underscores are recognized as part of the variable name. You can explicitly tell PowerShell what the variable names are either by using subexpressions or by using curly braces around the variable names. Mjolinor's example should work, as should this:
"Y]