Use DIR or FORFILES to create a list of found files

613 views Asked by At

I have a string that is currently being used, but I only want to bring back files that were created within a certain time frame, preferably the last 14 days. Here is the current command line:

dir \\UNC FilePath\*file.TXT /S /B > C:\DIRECTORY\Results\RESULTS.TXT

I read using forfiles was the way to go, but I can't get it to work either or figure out how to write it to a txt file.

forfiles /s Y:\*file.TXT /d -14 /c "cmd /c echo @path"

I'm a web guy trying to help a friend out on some server stuff. This code is being called from an excel macro which apparently doesn't like powershell very much (idk, just what I gathered)

Essentially he wants all the file.txt created within any directory on Y: to be output to a results.txt. Is this even possible with either method?

1

There are 1 answers

0
lit On

This is a command. powershell.exe, that runs from a cmd.exe prompt. If you are on a supported Windows system, PowerShell will be available.

powershell -NoLogo -NoProfile -Command ^
    "Get-ChildItem -Recurse -File -Path 'Y:\' -Filter '*file.TXT' |" ^
    "Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-14) } |" ^
    "ForEach-Object { $_.FullName }" >"C:\DIRECTORY\Results\RESULTS.txt"