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"
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.