powershellforeachnew-item

PowerShell: Creating a set amount of files based on a parameter, with a running number to differentiate the files


So I have an assignment where I have to create an PowerShell script that takes three parameters, "$foldername", "$filename" and "$number".

The script checks if the folder "$foldername" exists and if not, creates it. After that it creates as many new files named "$filename" as "$number" specifies. After that it reports how many files have been created and lists them.

What I have so far.

Param (
    [string]$foldername,
    [string]$filename,
    $number=1
)

if ((Test-Path -Path $foldername) -ne $true) {
    new-item -path $foldername -ItemType directory #if the folder doesn't exist, create it.
}
$new_file= $foldername+"\$_"+$filename #save the path and name of the new file to an variable
if ((Test-Path -Path $new_file* -PathType leaf) -eq $true) {
    Write-Host "$filename already exists in $foldername"
    break #if a file with a name that contains $filename in it exists in $foldername, break and do not create any new files.
}   
$null=1..$number | foreach { new-item -path $foldername -name $_$filename } #create new files using foreach.
write-host ("Created $number new files") #tell the user how many files were created
Get-ChildItem -path $foldername | where-object Name -like *$filename* | format-table Name #show the created files in a table format, formatted by name

There are a few problems and scuffed solutions in this script, but the main problem is the creation of the new files. Since the name of the new files come from $filename, simply running the script like so:

./script.ps1 -foldername C:\users\example\testing -filename "test.txt" -number 5

Would not work since it tries to create 5 files named "test.txt" and will just return errors.

I sort of solved it by using "foreach" and naming the files $_$filename which creates

1test.txt
2test.txt
...
5test.txt

But I found out that the correct way would be:

test1.txt
test2.txt
...
test5.txt

The number should be running in the filename somehow, but I am not sure how to do that.

Bonus points if you figure out how to check if the $filename files already exist in the target folder.


Solution

  • It's good to use Test-Path however I don't see a need for it here, you can use $ErrorAction = 'Stop' so that if the folder exists the script would instantly stop with a warning message. On the other hand, if the folder is a new folder there is no way the files already exist.

    Param (
        [parameter(Mandatory)]
        [string]$FolderName,
        [parameter(Mandatory)]
        [string]$FileName,
        [int]$Number = 1
    )
    
    $ErrorActionPreference = 'Stop'
    
    try {
        $newFolder = New-Item -Path $FolderName -ItemType Directory
    }
    catch {
        # If the folder exists, show this exception and stop here
        Write-Warning $_.Exception.Message
        break
    }
    
    $files = 1..$Number | ForEach-Object {
        # If this is a new Folder, there is no way the files already exist :)
        $path = Join-Path $newFolder.FullName -ChildPath "$FileName $_.txt"
        New-Item -Path $path -ItemType File
    }
    
    Write-Host 'Script finished successfully.'
    $newFolder, $files | Format-Table -AutoSize
    

    EDIT: I might have missed the point where you want to create the files in the folder even if the folder already exists, in that case you could use the following:

    Param (
        [parameter(Mandatory)]
        [string]$FolderName,
        [parameter(Mandatory)]
        [string]$FileName,
        [int]$Number = 1
    )
    
    $ErrorActionPreference = 'Stop'
    
    $folder = try {
        # If the Folder exists get it
        Get-Item $FolderName
    }
    catch {
        # If it does not, create it
        New-Item -Path $FolderName -ItemType Directory
    }
    
    $files = 1..$Number | ForEach-Object {
        $path = Join-Path $folder.FullName -ChildPath "$FileName $_.txt"
        try {
            # Try to create the new file
            New-Item -Path $path -ItemType File
        }
        catch {
            # If the file exists, display the Exception and continue
            Write-Warning $_.Exception.Message
        }
    }
    
    Write-Host "Script finished successfully."
    Write-Host "Files created: $($files.Count) out of $Number"
    $files | Format-Table -AutoSize