PDA

View Full Version : Open latest, partial named file in folder.



ChrisAcheson
09-25-2019, 05:04 AM
Hi

I normally use the below code, to find latest published file in folder, and open, which the below works fine.


Sub OpenLatestFile()



c00 = "\\\Output\Chris\"
Workbooks.Open Split(CreateObject("wscript.shell").exec("cmd /c dir """ & c00 & "*"" /b/s/a-d/o-d").stdout.readall, vbCrLf)(0)


End Sub


However, Now, I want to find the latest file within the folder that contains part of the file name.... 'Shopone' for example.

I have tried various things, such as the below ... which does not work for me.



Sub OpenLatestFile()



c00 = "\\\Output\Chris\"
Workbooks.Open Split(CreateObject("wscript.shell").exec("cmd /c dir """ & c00 & "*/shopone"" /b/s/a-d/o-d").stdout.readall, vbCrLf)(0)


End Sub

Is anyone able to advise, where I am going wrong?

Kind regards.

Chris

mana
09-25-2019, 05:31 AM
""" & c00 & "*shopone*""

ChrisAcheson
09-26-2019, 01:34 PM
thank you so much mana. worked perfectly, missed the closing star.

thanks again

snb
09-26-2019, 02:02 PM
Or simply:


Sub OpenLatestFile()
c00 = "\\\Output\Chris\*shopone"
Workbooks.Open Split(CreateObject("wscript.shell").exec("cmd /c dir """ & c00 & """ /b/s/a-d/o-d").stdout.readall, vbCrLf)(0)
End Sub

ChrisAcheson
09-27-2019, 05:20 AM
Thx snb.

Ill make a note of this also.