0

I have a basic text file with lines of information. Each line is a requested file to open.

I want a simple script to loop each line, opening each file until the document is empty.

For example

images.txt contains:

pic1.jpg
pic2.jpg
pic3.jpg
pic4.jpg

I've tried creating an array and can get the information to echo correctly, but having issues setting up a basic loop and having it stop when there are no more lines!

I've had some coding experience before but I'm pretty new to PS! Any help is appreciated :)

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
J V
  • 3
  • 3
  • 1
    PLEASE, post your code in your Question so that we can discuss it. – Lee_Dailey Nov 16 '20 at 17:07
  • I'm having trouble setting up the loop and necessary options to delete lines after opening. $file_data = Get-Content *.txt echo $file_data[2] (works) echo $file_data[3] echo $file_data[6] Invoke-Expression -Command "PathToFiles\$file_data[1]" So I'm able to read the file and store it into an array, echo each line and it shows the correct values, but using a few methods to count/delete a line has been confusing to me =/ – J V Nov 16 '20 at 17:14
  • @JV I closed this against a duplicate target that shows how to loop through the lines - but you need to think slightly differently about "removing lines" from a file than when you're editing it in a text editor - you don't "remove a line" from a file, instead, you read the file and then you write back everything _except_ for the lines you want to remove – Mathias R. Jessen Nov 16 '20 at 17:22
  • foreach($line in Get-Content .\runme.txt) { if($line -match $regex){ Invoke-Expression -Command "pathtofiles\$line" } } This appears to be opening the files that match which is what I wanted, and it's even acting as a loop in reading the entire contents of the text file so it *should* work :P Thanks guys – J V Nov 16 '20 at 18:34
  • @JV - glad to know that you got it working! [*grin*] **_however,_** next time, _please add your code to the Question, not in a comment. the 2nd is quite hard to read ... – Lee_Dailey Nov 16 '20 at 18:39

0 Answers0