0

I'm trying to use a FOR /R loop to iterate over some particularly long-named folders (greater than 260 characters in length, hence the need to use \?\ ) I need out of the loop, the fully qualified path name and the last access time

If I do this:

for /r "\\?\c:\windows" %a in (*.bat) do echo %~fta

I get the full path and the last modified time. Even if I set DIRCMD to /TA, there seems to be no effect on the FOR /R Does anyone know a way to force that time/date to be last accessed rather than last modified? AFAIK, the long path name rules out Powershell, which was my original approach but happy to receive suggestions in that as well.

  • 1
  • Thanks, if this allows me to use the \\?\ for long path names, this might be the solution. – Dave_J Jan 27 '15 at 14:05
  • Have you checked in explorer that the last access times are being updated? If not, you need to follow this link. – AFH Jan 27 '15 at 14:20
  • It seems that this command: for /f "delims=" %%F in ('dir /o:d /t:a /s /b "c:\myPath\*"') @echo %%~tF %%F Does not output the same date as just doing dir /t:a so back to square 1 unfortunately. @AFH yes, the time stamps are there, just struggling to get at them. – Dave_J Jan 27 '15 at 14:23
  • Why not use dir in your loop, then use filters such as grep and sed or cut to extract the time stamps you require? These are Unix utilities which have been ported to Windows many times, including in MS's own Services For Unix. – AFH Jan 27 '15 at 14:31
0

Try working around this by using subst.

The situation is not too simple. But you can combine: A. Using dir to get the actual accdate (its always at same position). + B. Using debug to filter that. + C. Using subst to temporary shorthen path (bypass 255-char limit by lowering it).

  • It's a good suggestion, but the network share starts at a short path and then forks off into multiple where the total length is greater than 260. I'd have to find a way to script SUBSTing whenever the path name got close. Doable, I'm sure, but a PITA – Dave_J Jan 27 '15 at 14:04
0

The following works (somewhat crudely) in Powershell

[CmdLetBinding()]

Param(
[string]$Path,
[string]$extension = "*"
)

If ($Path.ToString().Substring($Path.Length-1,1) -eq "\")
{
    $Path = $Path.ToString().Substring(0,$Path.Length -1)
}

If ($Extension[0] -eq ".")
{
    $Extension = $Extension[1..$Extension.Length] -join ""
}

$dirlisting = cmd /c "dir \\?\$path\*.$extension /s /-c /t:a"

ForEach ($line in $dirlisting)
{
    if ($line -match "^ Directory of (.*)$")
    {
        $FolderName = $matches[1] -replace "\\\\\?\\",""
    } ElseIf ($line -match "(\d{2}/\d{2}/\d{4})\s+?(\d{2}:\d{2})\s+?(\d+?)\s+?(.+)$")
    {
    $DateAndTime = [datetime]::Parse($matches[1] + " " + $matches[2])
    $Filesize = $matches[3]
    $FileName = Join-Path -Path $FolderName -ChildPath $matches[4]

    $FileObject = New-Object PSCustomObject -Property @{
        FullPath=$FileName
        LastAccessTime=$DateAndTime
        FileSize=$FileSize
        }
    Write-Output $FileObject
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.