Link to home
Create AccountLog in
Avatar of Sukpal Johal
Sukpal Johal

asked on

Powershell looping and launching through folders

Hi,
I am trying to run a Powershell script that will loop through a folder directory which contains sub folders and what I want to do is launch everything within the main folder directory and wait for the user input to continue before launching everything in each sub folders one by one but waiting for the user input before continuing down all the sub folders. So folder A contents are launched, the script waits for the user to say continue, once continue the script launches folder B's contents etc. Once complete down all sub folders, prompt that all files have now been launched.
Avatar of ITguy565
ITguy565
Flag of United States of America image

Hmm, well in theory that wouldn't be hard to accomplish.. Do these files need to be launched in any particular order?
Hi

Are the files executables or txtfiles or something?
This shows you the files and waits for keystroke after each file...I need to know what you want to execute

$files = get-ChildItem -Path c:\temp\folder -Recurse -File

foreach($file in $files)
{
	write-host $file
	pause "Press key to continue"
}

Open in new window

Wait...it doesnt matter...

that should work..

$files = get-ChildItem -Path c:\temp\folder -Recurse -File

foreach($file in $files)
{
	start $file
	pause "Press key to continue"
}

Open in new window

Avatar of Sukpal Johal
Sukpal Johal

ASKER

Hi, the files within each subfolder and the main folder can be launched in any order but the loop will need to move alphabetically. So Folder A, Folder B etc and not Folder A, Folder C and back to Folder B.
Sorry I think I misinterpreted the question a bit...

You want to execute everything in the mainfolder....THEN ask for continue...THEN exeute everything in the next subfolder, I assume sorted by Name? After EACH subfolder it asks the user to execute everything in the next folder?
Hi
Thomas U. Close but I don't want to write the name but to launch the files.  I.E. Invoke
That is very close to what I was working on :P

$workingdir = "c:\somepath"

set-location -path $workingDir
$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName

foreach ($folder in $foldernames){
    foreach ($file in $folder){
        start $file
    }
    pause "Enter a Key to Continue"
}

Open in new window

Correct  Thomas U that is exactly what I am after
I think we are near the solution...ITguy565 code is almost working. mine does not refelct the pause between the folders, but we get to that ;)
ITguy565 this is close but the script actually does not launch anything. I dont think "start" works.  I have changed the directory and tested the path test-path and it was true. So there is an error within this.

Start : This command cannot be run due to the error: The system cannot find the file specified.
At line:10 char:9
+         Start $file
+         ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
hmmm.. If you open powershell and just type start into the prompt what does it say?
I have come close but it launches the folder as well as the file but each user input is made per execution so press enter - folder 1 launches, press enter folder 2 launches, press enter, folder 1 content launches, press enter folder 1 second conent launches, press enter folder 2 content launches.

What I want is, Press Enter, Every file within folder 1 to launch but not the folder. Press enter, every file within folder 2 launch etc.

This is what I have so far:

$path = "C:\Users\Administrator\Downloads\test"

$files = Get-ChildItem $path -Recurse

#Write-Host $files

foreach ($file in $files){
    invoke-item $file.FullName
    Read-Host -Prompt "Press any key to continue"
}
I took ITguy565's code and altered it a little bit. If that works now as a PS1 file below, you can give him the credit ;)

cheers
Thomas

$workingdir = "c:\temp\hash"

set-location -path $workingDir

$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

foreach ($folder in $foldernames){
	$files = get-ChildItem -Path $folder -File | % { $_.FullName }
	write-host $files
	start $files
pause "Enter a Key to Continue"
}

Open in new window


you can remove the "write-host $files" thats just for control
ITguy565 Start itself works and it asks for the filepath which I enter manually and it launches the folder but within the script it will not launch the files etc.
Thomas U very close but it has still failed.

Start-Process : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'FilePath'. Specified method is not supported.
At line:12 char:8
+     start $files
+           ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.StartProcessCommand
it works here without error...
hmm but I think there is still an issue with the root folder....not correct...need more time
Try this
$workingdir = "c:\testdir"

set-location -path $workingDir

$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

foreach ($folder in $foldernames){
    $files = get-ChildItem -Path $folder -File | % { $_.FullName }
        foreach ($file in $files){
	write-host $file
    start $file
        }
pause "Enter a Key to Continue"
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Thomas U
Thomas U
Flag of Switzerland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
works for me Thomas..
Thomas U - Unfortunately it has not worked for me. The files are all launched within every folder and the pause for the user interaction never happens.

Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
At line:26 char:2
+     $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotImplementedException
 
Press any Key to continue with folder: 2
Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
At line:26 char:2
+     $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotImplementedException
ITguy565 yours actually worked in launching files from the subfolders but did not launch from the main directory.  Alos is there a chance that instead of having the "PAUSE" there is a pop up box?
Where do you try to run that script. I mean Windows Version ? Powershell Version?

If only the ReadKey is the problem, comment those two lines out (write a # before the lines 25 and 26) instead create a line and write pause
Thomas U. I like your script but with a slight issue. The Main directory files are launched alongside the 1st subfolder, from there the prompt works and only the 2nd subfolders files are launched. Any chance of getting the main directory to launch first and then navigate through the sub directories.

$workingdir = "C:\Users\Administrator\Downloads\test"

set-location -path $workingDir


#execute all files within the workingdir, no recurse
$rootfolder = Get-ChildItem -file #| % { $_.FullName }
foreach ($file in $rootfolder){
      start $file
}

# get all folders in $workingdir
$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

#loop through folders and execute, pause after folder
foreach ($folder in $foldernames){
      #get all files in the subfolder with fullpath
      $files = get-ChildItem -Path $folder -File | % { $_.FullName }
            # need loop through all files
            foreach ($fileSub in $files){
          
            start $fileSub
            }
      # Waits for user to continue
      Write-Host "Press any Key to continue with folder:" $folder
      #$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    Read-Host -Prompt "Press any key to continue"
      
}
I have it.

$workingdir = "C:\Users\Administrator\Downloads\test"

set-location -path $workingDir


#execute all files within the workingdir, no recurse
$rootfolder = Get-ChildItem -file #| % { $_.FullName }
foreach ($file in $rootfolder){
      start $file
}

Read-Host -Prompt "Press any key to continue"

# get all folders in $workingdir
$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

#loop through folders and execute, pause after folder
foreach ($folder in $foldernames){
      #get all files in the subfolder with fullpath
      $files = get-ChildItem -Path $folder -File | % { $_.FullName }
            # need loop through all files
            foreach ($fileSub in $files){
          
            start $fileSub
            }
      # Waits for user to continue
      Write-Host "Press any Key to continue with folder:" $folder
      #$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    Read-Host -Prompt "Press any key to continue"
      
}
wonderful ;) nice to hear that it worked.
Thank you guys for your help