3

I am trying to create a script which reads the first line of a file and then moves all of files with a similar naming convention up a level.

These are 3 example files:

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

The script I have so far is:

$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"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
A.Watkins
  • 37
  • 5
  • plus 1 for `$locationlocation` – Naigel Aug 14 '15 at 12:54
  • What issue are you having with your script? –  Aug 14 '15 at 13:43
  • it brings back the following error, Move-Item : Cannot find drive. A drive with the name '> C' does not exist. At line:10 char:10 + Move-Item <<<< -path "$paymasterfilenames" -Destination "$paymasterlocation" + CategoryInfo : ObjectNotFound: (> C:String) [Move-Item], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.MoveItemCommand – A.Watkins Aug 14 '15 at 14:22

1 Answers1

0

Your issue is arising here:

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

Assuming you're just trying to get the filename, this should work:

$lcoationfilename = $location | Split-Path -Leaf

If it's the parent folder you're after, then this is appropriate:

$lcoationfilename = $location | Split-Path -Parent

If for some reason you're on a version of PS that doesn't have Split-Path, let me know and I'll edit to a more appropriate solution.

Eric Walker
  • 1,042
  • 10
  • 15