Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Copy-item issue using variable

Reply
 
Old 04-12-2007   #1 (permalink)
JMinahan


 
 

Copy-item issue using variable

When I try to use a variable in the path name of what I want to copy, it
breaks. Here is what I have:


$arr = "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"
$arr[(Get-Date).month-1]

c:
del log.txt

"Date generated:" | out-file -Append log.txt
date | out-file -Append log.txt
" " | out-file -Append log.txt

"Directory for this month:" | out-file -Append log.txt
($arr[(Get-Date).month-1]+(Get-Date).year) | out-file -Append log.txt
" " | out-file -Append log.txt

W:
mkdir ($arr[(Get-Date).month-1]+(Get-Date).year)
F:
cd CDS
mkdir ($arr[(Get-Date).month-1]+(Get-Date).year)

Copy-Item X:\($arr[(Get-Date).month-1]+(Get-Date).year)\*.*
F:\CDS\($arr[(Get-Date).month-1]+(Get-Date).year)\*.* | out-file -Append
cdslog.txt


Everything works fine except for the last Copy-item command. How do I use my
variable in the path I want to copy?

My System SpecsSystem Spec
Old 04-12-2007   #2 (permalink)
Roman Kuzmin


 
 

Re: Copy-item issue using variable

Tip:

0> $arr = 1..3
0> $path = "xxx\$($arr[0])\yyy\$($arr[1] + $arr[2])\zzz"
0> $path
xxx\1\yyy\5\zzz

In other words, thy to use $(...)
Does it help?

--
Thanks,
Roman


My System SpecsSystem Spec
Old 04-12-2007   #3 (permalink)
JMinahan


 
 

Re: Copy-item issue using variable

Thanks. I was checking the manual and had it down to either quotes or
parentheses.

"Roman Kuzmin" wrote:

> Tip:
>
> 0> $arr = 1..3
> 0> $path = "xxx\$($arr[0])\yyy\$($arr[1] + $arr[2])\Zzz"
> 0> $path
> xxx\1\yyy\5\Zzz
>
> In other words, thy to use $(...)
> Does it help?
>
> --
> Thanks,
> Roman
>
>
>

My System SpecsSystem Spec
Old 04-12-2007   #4 (permalink)
JMinahan


 
 

RE: Copy-item issue using variable

I got it in better form by doing what you said, but i am still having trouble
specifically with the copy command.

"Archive directory for this month:" | out-file -Append cdslog.txt
"$frompath" | out-file -Append cdslog.txt
" " | out-file -Append cdslog.txt

"Destination folder:" | out-file -Append cdslog.txt
"$topath" | out-file -Append cdslog.txt

F:
cd CDS
mkdir ($arr[(Get-Date).month-1]+(Get-Date).year)

X:
cd $frompath

##Copy-Item "$frompath" "$topath"
##Copy-Item "$frompath" "$topath" | out-file -Append cdslog.txt

Help! So close...

"JMinahan" wrote:

> When I try to use a variable in the path name of what I want to copy, it
> breaks. Here is what I have:
>
>
> $arr = "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"
> $arr[(Get-Date).month-1]
>
> c:
> del log.txt
>
> "Date generated:" | out-file -Append log.txt
> date | out-file -Append log.txt
> " " | out-file -Append log.txt
>
> "Directory for this month:" | out-file -Append log.txt
> ($arr[(Get-Date).month-1]+(Get-Date).year) | out-file -Append log.txt
> " " | out-file -Append log.txt
>
> W:
> mkdir ($arr[(Get-Date).month-1]+(Get-Date).year)
> F:
> cd CDS
> mkdir ($arr[(Get-Date).month-1]+(Get-Date).year)
>
> Copy-Item X:\($arr[(Get-Date).month-1]+(Get-Date).year)\*.*
> F:\CDS\($arr[(Get-Date).month-1]+(Get-Date).year)\*.* | out-file -Append
> cdslog.txt
>
>
> Everything works fine except for the last Copy-item command. How do I use my
> variable in the path I want to copy?

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Copy-Item : Container cannot be copied onto existing leaf item. PowerShell
Can the copy-item cmdlet be used to copy public folders to C: ? PowerShell
copy-item changing files attributes on network copy failures PowerShell
Getting Copy-Item to display messages (like the old COPY command in CMD.EXE) ?? PowerShell
Remove-item on a variable name using a regex PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46