Quickly create new files with a script

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.
LVL 9
samiam41Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael B. SmithManaging ConsultantCommented:
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

0
samiam41Author Commented:
Thank you Expert!  Where do I specify number of files to create, where the dir is and what extension the files will have?
0
ITguy565Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

ITguy565Commented:
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
0
David Johnson, CD, MVPOwnerCommented:
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

0
samiam41Author 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!
0
ITguy565Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.