locked
Deleting files of different extension type RRS feed

  • Question

  • Hi All.

    I have the script below.

    get-childitem "D:\files" -recurse -force -include *.dat *.avi | remove-item -force 

    The above doesnt work on .dat and .avi file types

    I would like to convert it to a function/Ps file whereby it takes 2 parameters, path and also the extension file types i.e .dat or .avi or .doc etc .

    Thanks in advance.

    Tuesday, October 29, 2019 10:46 AM

All replies

  • Bad syntax:

    get-childitem D:\files\* -recurse -force -include *.dat,*.avi | remove-item -force 
    

    You need a comma in a list of strings,

    When in doubt read all of the help for the CmdLet and try the examples until you understand the command.

    help get-childitem -online


    \_(ツ)_/

    Tuesday, October 29, 2019 11:29 AM
  • Thanks for this.

    I was thinking, if I put the extension types in an array and loop through it, this way If a new extension file needs to be added, it will just be a parameter and not require a code change.

    Tuesday, October 29, 2019 12:11 PM
  • Changing an array is the same as a code change.

    You need commas in an array on a command line. 

    You can put the masks in a file and load them if that is what you mean.

    No matter any of that.  Your syntax was wrong.  Try the command now and you will see that it works.


    \_(ツ)_/

    Tuesday, October 29, 2019 12:18 PM
  • I am not sure why this isnt working.

    Do I need to escape $types

    $folder = 'D:\temp'
    $types = '*.txt, *.exe'
    
    get-childitem $folder -recurse -force -include $types | remove-item -force -Verbose
    

    Tuesday, October 29, 2019 1:00 PM
  • Please look at and test my code above then you will see the mistakes you are making.


    \_(ツ)_/

    Tuesday, October 29, 2019 1:51 PM
  • $folder = 'D:\temp\*'
    $types = '*.txt,*.exe'
    
    get-childitem $folder -recurse -force -include $types | remove-item -force -Verbose

    Looked at it again, works when the $types is not a variable.

    Tuesday, October 29, 2019 2:27 PM
  • Now go and read the help that I posted and read how to use the parameters. Pay special attention to the type declaration of the parameters. You are not doing what the syntax and parameters require.  You also need to learn how PowerShell works when commands are types and parsed. You need top learn the very most basic things about PS. Stop guessing and read the documentation.; You cannot learn by guessing and asking others to read the docs for you.


    \_(ツ)_/

    Tuesday, October 29, 2019 2:37 PM
  • Didnt realise that is what you meant, string array.

    String array works, from a web form I guess users can pass the parameter below to the file.

     

    @("*.txt" , "*.exe")

    Tuesday, October 29, 2019 2:56 PM
  • A textbox set to multiline is a string array for strings on separate lines.

    $textbox.Lines <== this is a string array.


    \_(ツ)_/

    Tuesday, October 29, 2019 3:00 PM
  • A string isn't an array.

    $types = @('*.txt,'*.exe')


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, October 29, 2019 3:17 PM
  • A string isn't an array.

    $types = @('*.txt,'*.exe')


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    still cannot get it to work.

    I have the script in a Ps file, and once I pass the value ' @('*.txt','*.exe')' as the types parameter, nothing happens.



    • Edited by MrFlinstone Friday, November 1, 2019 6:51 PM
    Friday, November 1, 2019 6:50 PM
  • A string isn't an array.

    $types = @('*.txt,'*.exe')


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    still cannot get it to work.

    I have the script in a Ps file, and once I pass the value ' @('*.txt','*.exe')' as the types parameter, nothing happens.



    Drop the outer single quotes.

    $types = '*.txt','*.exe'
    get-childitem D:\files\* -recurse -force -include  $types  | remove-item -force 
    help about_Quoting_Rules


    \_(ツ)_/

    Friday, November 1, 2019 8:33 PM
  • Oops. Sorry. There's a missing single quote in the example.

    $types = @('*.txt','*.exe')

    or

    $types = '*.txt','*.exe'

    The extra "@()" decoration isn't necessary. I just use it to make the declaration of a list stand out.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Friday, November 1, 2019 9:06 PM
  • A string isn't an array.

    $types = @('*.txt,'*.exe')


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    still cannot get it to work.

    I have the script in a Ps file, and once I pass the value ' @('*.txt','*.exe')' as the types parameter, nothing happens.



    Drop the outer single quotes.

    $types = '*.txt','*.exe'
    get-childitem D:\files\* -recurse -force -include  $types  | remove-item -force 
    help about_Quoting_Rules


    \_(ツ)_/

    Param 
    (
    	[Parameter(Mandatory=$true)] [string] $types
    )
    
    
    get-childitem 'C:\files\*' -recurse -force -include $types | remove-item -force -Verbose
    
    still doesnt work.

    Friday, November 1, 2019 9:33 PM
  • Your parameter type is incorrect. Try "[string[]]".

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, November 1, 2019 9:42 PM
  • A string isn't an array.

    $types = @('*.txt,'*.exe')


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    still cannot get it to work.

    I have the script in a Ps file, and once I pass the value ' @('*.txt','*.exe')' as the types parameter, nothing happens.



    Drop the outer single quotes.

    $types = '*.txt','*.exe'
    get-childitem D:\files\* -recurse -force -include  $types  | remove-item -force 
    help about_Quoting_Rules


    \_(ツ)_/

    Param 
    (
    	[Parameter(Mandatory=$true)] [string] $types
    )
    
    
    get-childitem 'C:\files\*' -recurse -force -include $types | remove-item -force -Verbose
    still doesnt work.

    What does "still doesn't work" mean?  Run without the remove to see the output.

    Wrapping in a function will not get you what you want if you change the input to a string.  try [string[]] or just leave the type enforcement off.


    \_(ツ)_/

    Friday, November 1, 2019 9:44 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 1:01 PM