Chase
Chase

Reputation: 584

VBA to Move Files From Directory Based on Filename Length

We recently imported > 100,000 .tif files to a directory on our shared drive. All of these files should have the same naming convention: 0123456789.tif ("10 digits followed by extension .tif"). Most do, but a good handful do not. There are some that are either greater than 14 characters (10 for filename + 4 for extension), or they are less than 14 characters.

Is there a way to basically say:

"If in directory x and length(filename) <> 14 characters, then move to directory y"

(where directory y is just some other folder)?

Thank you much.

Upvotes: 1

Views: 329

Answers (1)

Uri Goren
Uri Goren

Reputation: 13700

You need to use the Dir() Function to list all the files, and the Name operator to move the files

Something like this should work

Dim srcDir as String,dstDir as String,f as String
srcDir="C:\"
dstDir="D:\"
f=Dir(srcDir)
Do While f<>""
  If Len(f)<>14 Then Name srcDir & "\" & f As dstDir & "\" & f
  f=Dir()
Loop

Upvotes: 1

Related Questions