Quickly create new files with a script

samiam41
samiam41 used Ask the Experts™
on
As I test scripts that will delete files out of a test dir, I want to run a script that will re-create new files (x times, set as needed) so I can do additional tests rather than recreating them one file at a time.  I don't care what the file names are when they are created.  Just looking for a quick way to put those files back so I can tweak the script and continue on.  Thanks Experts.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Michael B. SmithManaging Consultant

Commented:
Param(
    [int] $howMany,
    [string] $folderName = $env:Temp
)

for( $i = 0; $i -lt $howMany; $i++ ) { " " | Out-File -LiteralPath ( Join-Path $folderName $i.ToString() ) -Force }

Open in new window

Author

Commented:
Thank you Expert!  Where do I specify number of files to create, where the dir is and what extension the files will have?
you could also do this:

$ext = "txt"
function create-TempFiles($number, ext){
for ($i=0;$i -lt $number;$i++){
    echo $null >> "tempfile_$i.$ext"
}}

Open in new window




syntax is

create-TempFiles -number 

Open in new window


or you can also do this :

create-tempfiles -number 5 -ext csv

Open in new window

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

If you want to add the directory to it you can do this :

$ext = "txt"
function create-TempFiles($number, $ext, $Directory){
set-location $directory
for ($i=0;$i -lt $number;$i++){
    echo $null >> "tempfile_$i.$ext"
}}

Open in new window


syntax :
create-TempFiles -Directory c:\tem -number 8 -ext csv
Top Expert 2016

Commented:
try this in a .ps1 file
Function New-Files
{
  <#
      .SYNOPSIS
      Creates a bunch of new files for testing

      .PARAMETER howMany
      How many Files to Create

      .PARAMETER folderName
      Which Folder to Create The files

      .PARAMETER extension
      The Extension i.e. .tmp or .txt

      .EXAMPLE
      New-Files -howMany Value -folderName Value -extension Value
      
      .Example
      New-Files -howMany 50 -extension .txt
      This will create 50 files 1.txt 2.txt 3.txt ... 49.txt in %temp%

  #>


  [CmdletBinding()]
  Param(
    [int] $howMany =1,
    [string] $folderName = $env:Temp,
    [string] $extension = ".tmp"
  )
  write-debug -Message (('This will create {0} Files in {1}' -f $howmany, $foldername))
  for( $i = 0; $i -lt $howMany; $i++ ) 
  {
    $filename = $i.ToString() + $extension
    ' ' | Out-File -LiteralPath ( Join-Path -Path $folderName -ChildPath $filename) -Force
      }
}
# just to show you how to do this..
Get-Help -Name new-files -Examples

Open in new window

Author

Commented:
Thanks for everyone's time and suggestion.  I went with the very easy single line command.  

create-TempFiles -Directory c:\tools\scripts\test -number 10 -ext txt

Take care Experts and I look forward to working with you again!
commented out :

$ext = "txt"
function create-TempFiles($number, $ext, $Directory){

 <#
      .SYNOPSIS
      Creates a bunch of new files for testing

      .PARAMETER number
      Specify the Number of Files for Creation

      .PARAMETER ext
      Identify the Extension of the file you want to create for testing

      .PARAMETER Directory
      Identify the folder you want to place the files in

      .EXAMPLE
      create-TempFiles -Directory c:\tem -number 8 -ext csv  

  #>

set-location $directory
for ($i=0;$i -lt $number;$i++){
    echo $null >> "tempfile_$i.$ext"
}}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial