【问题标题】:Reading input and moving files with similar names读取输入和移动具有相似名称的文件
【发布时间】:2015-08-14 12:44:19
【问题描述】:

我正在尝试创建一个脚本,该脚本读取文件的第一行,然后将具有类似命名约定的所有文件上移一个级别。

这些是 3 个示例文件:

C:\Users\USERNAME\location\PYYYYYYYYY.txt.asc
C:\Users\USERNAME\location\holdingarea\PYYYYYYYYY.tt.asc
C:\Users\USERNAME\location\holdingarea\PYYYYYYYYY.t.asc

我目前的脚本是:

$location = Read-Host -Prompt "Location Filename"
$locationfilename = Select-String C:\Users\USERNAME\location\holdingarea\*.txt.asc –pattern $location  -Context 1
$locationfilenames = $locationfilename.basename
$locationarea = "C:\Users\USERNAME\location\holdingarea"
$locationlocation = "C:\Users\USERNAME\location"

Select-String C:\Users\USERNAME\location\holdingarea\*.txt.asc -Pattern $location -Context 1 |
  Out-File -Append C:\Users\USERNAME\location\logs.txt
Move-Item -Path "$locationfilenames" -Destination "$locationlocation"

【问题讨论】:

  • $locationlocation加1
  • 您的脚本有什么问题?
  • 它带回以下错误,移动项目:找不到驱动器。名称为“> C”的驱动器不存在。在 line:10 char:10 + Move-Item C:String) [Move-Item], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound, Microsoft.PowerShell.Commands.MoveItemCommand

标签: powershell file-move


【解决方案1】:

您的问题出现在这里:

$locationfilename = Select-String C:\Users\USERNAME\location\holdingarea\*.txt.asc –pattern $location  -Context 1

假设您只是想获取文件名,这应该可以:

$lcoationfilename = $location | Split-Path -Leaf

如果它是您所追求的父文件夹,那么这是合适的:

$lcoationfilename = $location | Split-Path -Parent

如果由于某种原因您使用的 PS 版本没有拆分路径,请告诉我,我会修改为更合适的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2016-12-27
    相关资源
    最近更新 更多