使用PowerShell将文件复制到随机命名的目录

时间:2017-11-29 15:47:19

标签: file powershell random location copy-item

#copies program directory

copy-item .\iphone -Recurse c:\Users\$env:username -force

#sets file I want to copy to unknown directory name as $file1

C:\Users\$env:username\iphone\.filename = $file1

#sets unknown folder location to $location1 which is a randomly named directory 
#under this subfolder in appdata\Roaming\parentfolder\23761253721536721(random //number) , 
#name is just a string of numbers

Get-ChildItem  C:\Users\$env:username\AppData\Roaming\parentfolder = $location1

//attempts to copy file to location

copy-item $file1 $location1

PowerShell新手。我敢肯定,我正在努力实现这一目标。这就像我的第10个剧本试图这样做,我觉得我离答案越来越远了。曾在technet上查看示例,但没有找到任何设置我正在尝试复制文件的未知目录路径的内容。

如果你的回答你不介意解释代码在做什么,所以我可以学习我会很感激。

2 个答案:

答案 0 :(得分:0)

为变量赋值时,变量引用需要放在=的左侧:

$file1 = Get-Item C:\Users\$env:username\iphone\.filename
$location1 = Get-ChildItem C:\Users\$env:username\AppData\Roaming\parentfolder 

$file1 |Copy-Item -Destination $location1.FullName

答案 1 :(得分:0)

假设您只有一个“随机文件夹”:

## Translates to C:\Users\%USERNAME%\AppData\Roaming
$Parent = "$env:AppData\ParentFolder"

$Destination = Get-ChildItem -Path $Parent | Where-Object { $_.PSIsContainer }
$File = "$env:UserProfile\iphone\.filename"

Copy-Item -Path $File -Destination $Destination