Powershell - 复制后重命名文件
Powershell -renaming a file after copying
我编写的脚本一直存在问题,该脚本(应该)执行以下操作。
我有一个文件夹,里面有很多csv文件,我想把最新的公司名称的文件复制到另一个文件夹,然后重命名。
当前格式:
21Feb17070051_CompanyName_Sent21022017
我想要以下格式:
CompanyName21022017
所以我有以下 powershell 脚本来执行此操作:
## Declare variables ##
$DateStamp = get-date -uformat "%Y%m%d"
$csv_dest = "C:\Dest"
$csv_path = "C:\Location"
## Copy latest Company CSV file ##
get-childitem -path $csv_path -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime |
select-object -last 1 |
copy-item -Destination $csv_dest
## Rename the file that has been moved ##
get-childitem -path $csv_dest -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime |
select-object -last 1 | rename-item $file -NewName {"Company" + $DateStamp + ".csv"}
文件似乎可以复制,但重命名失败 -
Rename-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\Powershell Scripts\MoveCompanyFiles.ps1:20 char:41
+ select-object -last 1 | rename-item $file -NewName {"CompanyName" + $DateSt ...
我认为这与 powershell 的工作顺序有关,或者它在 $file 变量中看不到 .csv。目标中还有其他文件(文本文件、批处理文件),以防影响。
任何对我出错的地方的帮助将不胜感激。
正如 wOxxOm 回答的那样,您需要从 Rename-Item
中删除 $file
,因为它未定义并且 cmdlet 已经通过管道接收了输入对象。
我还建议您通过将复制文件的 fileinfo
-对象传递到 Rename-Item
来组合这两个操作。例如:
## Declare variables ##
$DateStamp = get-date -uformat "%Y%m%d"
$csv_dest = "C:\Dest"
$csv_path = "C:\Location"
## Copy and rename latest Company CSV file ##
Get-ChildItem -Path $csv_path -Filter "*Company*.csv" |
Where-Object { -not $_.PSIsContainer } |
Sort-Object -Property CreationTime |
Select-Object -Last 1 |
Copy-Item -Destination $csv_dest -PassThru |
Rename-Item -NewName {"Company" + $DateStamp + ".csv"}
您可以在一条命令中重命名和复制。只需使用 Copy-Item 命令并将新路径和名称作为 -Destination 参数值。它将复制并重命名文件。你可以在下面找到一个例子。
$source_path = "c:\devops\test"
$destination_path = "c:\devops\test\"
$file_name_pattern = "*.nupkg"
get-childitem -path $source_path -Filter $file_name_pattern |
Copy-Item -Destination { $destination_path + $_.Name.Split("-")[0] + ".nupkg"}
我编写的脚本一直存在问题,该脚本(应该)执行以下操作。 我有一个文件夹,里面有很多csv文件,我想把最新的公司名称的文件复制到另一个文件夹,然后重命名。
当前格式:
21Feb17070051_CompanyName_Sent21022017
我想要以下格式:
CompanyName21022017
所以我有以下 powershell 脚本来执行此操作:
## Declare variables ##
$DateStamp = get-date -uformat "%Y%m%d"
$csv_dest = "C:\Dest"
$csv_path = "C:\Location"
## Copy latest Company CSV file ##
get-childitem -path $csv_path -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime |
select-object -last 1 |
copy-item -Destination $csv_dest
## Rename the file that has been moved ##
get-childitem -path $csv_dest -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime |
select-object -last 1 | rename-item $file -NewName {"Company" + $DateStamp + ".csv"}
文件似乎可以复制,但重命名失败 -
Rename-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\Powershell Scripts\MoveCompanyFiles.ps1:20 char:41
+ select-object -last 1 | rename-item $file -NewName {"CompanyName" + $DateSt ...
我认为这与 powershell 的工作顺序有关,或者它在 $file 变量中看不到 .csv。目标中还有其他文件(文本文件、批处理文件),以防影响。 任何对我出错的地方的帮助将不胜感激。
正如 wOxxOm 回答的那样,您需要从 Rename-Item
中删除 $file
,因为它未定义并且 cmdlet 已经通过管道接收了输入对象。
我还建议您通过将复制文件的 fileinfo
-对象传递到 Rename-Item
来组合这两个操作。例如:
## Declare variables ##
$DateStamp = get-date -uformat "%Y%m%d"
$csv_dest = "C:\Dest"
$csv_path = "C:\Location"
## Copy and rename latest Company CSV file ##
Get-ChildItem -Path $csv_path -Filter "*Company*.csv" |
Where-Object { -not $_.PSIsContainer } |
Sort-Object -Property CreationTime |
Select-Object -Last 1 |
Copy-Item -Destination $csv_dest -PassThru |
Rename-Item -NewName {"Company" + $DateStamp + ".csv"}
您可以在一条命令中重命名和复制。只需使用 Copy-Item 命令并将新路径和名称作为 -Destination 参数值。它将复制并重命名文件。你可以在下面找到一个例子。
$source_path = "c:\devops\test"
$destination_path = "c:\devops\test\"
$file_name_pattern = "*.nupkg"
get-childitem -path $source_path -Filter $file_name_pattern |
Copy-Item -Destination { $destination_path + $_.Name.Split("-")[0] + ".nupkg"}