Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 22, 2015 8:16 AM
I have a csv file with about 70,000 file locations eg.
\Paperwork\Images\2009\Dec\RSP11442001.TIF
\Paperwork\Documents\2012\Jul\RS126694.doc
\Paperwork\Other\2012\Jul\RSP27392.mdi
I need to write a powershell script to check if the files actually exist or not and produce a list of missings files.
This is what Ive written
ForEach (\Paperwork\files.csv) {
Get-ChildItem $Path
Test-path $Path
Write-Host $Path
}
Any ideas? I only want to write the files that dont exist
\Paperwork\files.csv is the CSV with the file locations
Thanks,
Zoe
All replies (14)
Wednesday, April 22, 2015 2:52 PM ✅Answered
Import-Csv .\filesToTest.csv | ForEach {
If (Test-Path -Path $_.File) {
Write-Output "$($_.File) exists"
} Else {
Write-Output "$($_.File) does NOT exist"
$_.File | Out-File .\missingFiles.txt -Append
}
}
Assumes a header of 'File'.
Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)
Wednesday, April 22, 2015 8:35 AM | 1 vote
Assuming that your csv has a header named path (e.g. Cell A1 has 'path' as content)
$paths = Import-Csv '\\Paperwork\files.csv'
md C:\missingpaths.txt
foreach($path in $paths){
if(test-path $path -eq $false){
write-verbose $path
Out-File -FilePath 'C:\missingpaths.txt' -InputObject $path -Append
}
}
this will write missing paths to the console and to
C:\missingpaths.txt
Wednesday, April 22, 2015 11:21 AM
Thanks Koen,
Ive opened the CSV file in excel and added 'Path' in Cell A1 and saved.
When I run your script I get the following error:
Test-Path : A parameter cannot be found that matches parameter name 'eq'.
Wednesday, April 22, 2015 12:25 PM | 1 vote
Ah, that would messy coding on my side, it needs brackets around it:
if((test-path $path) -eq $false){
Wednesday, April 22, 2015 12:54 PM | 2 votes
It is bad formand error ptroone to specify redundant logical tests. "Test" CmdLets do this which is why they are names "Test".
if(test-path $path){
** # file exists**
}else{
** Write-Host 'file not found'**
** # output file**
}
The results from any test are already $true or $false. "if" is a test of the truth.
\(ツ)_/
Wednesday, April 22, 2015 1:05 PM
Thats Koen,
That runs ok now, but it doesnt seem to actually check if the file exists - It just outputs a list of the names in the csv...
Wednesday, April 22, 2015 1:42 PM | 1 vote
@jrv, Thank you for that feedback.
One of my reasons to post here is to improve my own scripting.
#Zoe.Ohara:
If you use the code snip-it that jrv provided, anything between the else{ } brackets, be it write-host or out-file will contain the files names that don't exist.
Wednesday, April 22, 2015 1:48 PM | 1 vote
I try to not write code for people but I do try to provide direction in how to use code to get results in as simple a way as possible.
With technology it is necessary to learn to think about and research items until the basics are well understood. Handing out finished script presents no challenge and no learning.
\(ツ)_/
Wednesday, April 22, 2015 2:44 PM
Thanks both,
Ive amended the code, but It still just outputs all the contents of the csv file, whether the file exists or not..
Not sure what Im doing wrong..
Zoe
Wednesday, April 22, 2015 2:49 PM | 1 vote
test one line o the file manually
$line='\....'
dir $line.
Does it work?
\(ツ)_/
Wednesday, April 22, 2015 2:49 PM
Clear-Host$paths = Import-Csv '\\paperwork\files.csv' md c:\missingpaths.txtforeach($path in $paths){ if(test-path $path) { # file exists }else{ write-verbose $path Out-File -FilePath 'c:\\missingpaths.txt' -InputObject $path -Append }}
This is what Im using now...
Wednesday, April 22, 2015 2:57 PM | 1 vote
What is this line supposed to do?
md c:\missingpaths.txt
It will prevent you from having a file of that name.
\(ツ)_/
Thursday, April 23, 2015 9:07 AM
Thanks Mike! that did it!
Thanks for your help Jrv & Koen!
the md command was in Koens original script - when I ran it I just commented it out and created the text file manually.
Need to get get better at powershell - Thanks for your help!
Zoe
x
Thursday, April 23, 2015 12:16 PM
Cheers, you're very welcome. Glad to help out.
Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)