简体   繁体   中英

How to loop through files (full path) in PowerShell

How do I do the equivalent in PowerShell? Note that I require the full path to each file.

# ksh
for f in $(find /app/foo -type f -name "*.txt" -mtime +7); do
   mv ${f} ${f}.old
done

I played around with Get-ChildItem for a bit and I am sure the answer is there someplace.

I'm not sure what mtime does here is the code to do everything else

gci -re -in *.txt "some\path\to\search" | 
  ?{ -not $_.PSIsContainer } |
  %{ mv $_.FullName "$($_.FullName).old" }

This seems to get me close to what I need. I was able to combine some of the information from Jared's answer with this question to figure it out.

foreach($f in $(gci -re -in hoot.txt "C:\temp")) {
   mv $f.FullName "$($f.FullName).old"
}

In the interest of sharing the wealth here is my function to simulate *nix find.

function unix-find (
   $path,
   $name="*.*",
   $mtime=0) 
   {
   gci -recurse -include "$name" "$path" | 
      where-object { -not $_.PSIsContainer -and ($_.LastWriteTime -le (Get-Date).AddDays(-$mtime)) } |
      foreach { $_.FullName }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM