Loop through a directory and get a missing sequence file

by FerroShell at 2013-01-08 01:54:15

Hi All,

I am new in PowerShell scripting and I need support in the following problem:
I have a directory that carries the year as its name (2013) which contains a group of .TIF files with a naming convention YYYY999999 (YYYY = 2013 and 999999 = a sequence number that starts with 000001). All files should be in sequence (i.e. 2013000001.TIF then 2013000002.TIF, 2013000003.TIF…).

I have missing files that I need to list so I need to loop through this directory and get missing files (a missing sequence that doesnt have a file name for it)

Can you please help.
by DonJ at 2013-01-08 08:30:00

$numbers = 1..999999
foreach ($number in $numbers) {
$path = "C:\2013\2013$number.TIF"
if (Test-Path $path) {
# file exists
} else {
# file does not exist
}
}


This is just an example. You’ll have to modify this to fit your specific needs, but this is the basic logic. Rather than looping through the files, I’m looping through the sequence numbers.
by ArtB0514 at 2013-01-08 11:48:25
And you can make Don’s loop run a bit quicker by finding the highest number in use and stopping when you reach it instead of going through all possible numbers.

$Last = [INT](Get-ChildItem "C:\2013\2013*.TIF")[-1].BaseName.Substring(4)
$numbers = 1..$Last
by nohandle at 2013-01-09 08:34:43
[quote="ArtB0514"]And you can make Don’s loop run a bit quicker by finding the highest number in use and stopping when you reach it instead of going through all possible numbers.[/quote]Nice idea, but by this approach you rely on having only the correct files in the directory. Also it produces terminating error if there is no file in the folder .)
by ArtB0514 at 2013-01-09 10:32:56
I think that we already know that the files in the folder have the correct names:
[quote]contains a group of .TIF files with a naming convention YYYY999999 (YYYY = 2013 and 999999 = a sequence number that starts with 000001)[/quote]

If there are no relevant files in the folder, then a terminating error will confirm that there are no missing files. If seeing a terminating error for a folder is a problem, it would be a simple task to enclose the script block in Try-Catch.
by nohandle at 2013-01-10 00:46:21
ArtB0514:
[quote="ArtB0514"]If there are no relevant files in the folder, then a terminating error will confirm that there are no missing files. If seeing a terminating error for a folder is a problem, it would be a simple task to enclose the script block in Try-Catch.[/quote]
Using a terminating error to inform you the script was successfull is hardly intuitive :slight_smile: If there is posibility of terminating error (that will inform you and stop your script), it is of course easy to add a catch for apropriate exception type, but you have to be aware there can be such exception first.

I am not trying to talk you down by any chance, I am also not saying your solution does not fit the problem. I am just trying to specify the conditions under which your solution can be used and things the OP should be aware of.