繁体   English   中英

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

[英]Copy file to randomly named directory with PowerShell

#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的新功能。 我确信我正在使它变得比所需的更难。 就像我的第十个脚本试图做到这一点一样,我觉得我离答案越来越远。 一直在technet上查看示例,只是没有找到任何东西来设置要将文件复制到的未知目录路径。

如果您的回答是您不介意解释代码的功能,那么我将不胜感激。

为变量分配值时,变量引用必须位于=的左侧:

$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

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

## 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM