2015-08-14 68 views
3

我正在尝试创建一个脚本,它读取文件的第一行,然后将所有具有类似命名约定的文件移动到一个级别。读取输入和移动具有相似名称的文件

这些都是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" 
+0

加1用于'$ locationlocation' – Naigel

+0

你的脚本有什么问题? –

+0

它带回以下错误, Move-Item:找不到驱动器。名称为'> C'的驱动器不存在。 在行:10字符:10 +移动-项目<<<< -path “$ paymasterfilenames” -Destination “$ paymasterlocation” + CategoryInfo:ObjectNotFound:(> C:字符串)[移动-项目],DriveNotFoundException + FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.MoveItemCommand –

回答

0

你的问题是出现在这里:

$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的,不具有独立路径,让我知道,我会编辑一个更合适的解决方案。

相关问题