Jason Stewart
Jason Stewart

Reputation: 21

powershell if file doesnt exist log string to file

I have many file paths stored in a DB. I need to check if the files actually exist. I've done this before but lost the script for it and need some help.

I put all the paths in a text file and want to loop over them, and check if they exist. if they don't exist, I want to put the nonexistent path in a log file.

Something like this:

# ! equals -not

$log = "e:\pshell\notExists.log"
$log | out-file $log

$list = Get-Content "e:\pshell\files.txt"

Foreach ($file in $list)
{
  CHECK IF FILE EXISTS
  IF IT DOESNT then Write-Output $file
}

little help?

Upvotes: 2

Views: 3064

Answers (3)

Frode F.
Frode F.

Reputation: 54871

If you inputfile is one filepath per line, try:

$log = "e:\pshell\notExists.log"

Get-Content "e:\pshell\files.txt" | Where-Object {
    #Keep only paths that does not exists
    !(Test-Path $_)
} | Set-Content $log

Upvotes: 2

mjolinor
mjolinor

Reputation: 68243

$log = "e:\pshell\notExists.log" 

Get-Content "e:\pshell\files.txt" |
where {!(test-path $_)} |
add-content $log

Upvotes: 0

James Woolfenden
James Woolfenden

Reputation: 6651

test-path?

$log = "e:\pshell\notExists.log" $log | out-file $log

$list = Get-Content "e:\pshell\files.txt"

Foreach ($file in $list) 
{ 
   If (!(test-path $file))
   {
      Write-Output $file
   }
}

Upvotes: 4

Related Questions