Pages

11 December 2012

How to loop through files in a folder by windows powershell


Step 1:
Create a powershell file named as loopfiles.ps1 (you can simply create a new text file and rename it as loopfiles.ps1).

Step 2:
Open the loopfiles.ps1 file and paste the following and save it:

############################


$fso = new-object -com scripting.filesystemobject
$folder = $fso.getfolder("C:\temp")

foreach ($f in $folder.files) {
    $f.path >> filesvisited.txt
$c = Get-Content $f.path
}
############################



Step 3:
Right click on the loopfiles.ps1 and select Run with Powershell.

At the end, one should have an output file (filesvisited.txt) with the names of the files in the current folder.

2 comments:

  1. Is there anyway to do this within the QV script without needing to use Powershell and a generated text file?

    ReplyDelete
  2. May be, it can be done with a vbscript macro on QV, but I did not try it.

    ReplyDelete