none
Create a loop statement to create files according to a value RRS feed

  • General discussion

  • I have to create a couple of files within a directory structure. The Filename stays the same, but at the end of each file, the number appears. (filename01, filename02...)
     
    Example:
    File needs to be created within directory structure: c:\temp
    File Name will stay the same (filename), but extension will increase, starting at 01 - example - filename01, filename02, and so on

    Depending on the day of the month, I will need to create either 10 files or 26 files or 53, number can very day by day.

    Directory will look something like this:
    Code:
    c:\temp
    filename01
    filename02
    filename03.....
    filename10 and so on

    The value I will change daily (so the input I will change)

    Can anyone help me with a script to create these files with the value I need to pass to the script?
    • Changed type Bill_Stewart Monday, October 2, 2017 9:50 PM
    • Moved by Bill_Stewart Monday, October 2, 2017 9:50 PM This is not "scripts on demand"
    Monday, August 21, 2017 10:05 AM

All replies

  • $I = 10
    "$filename$I"

    Just add the number to the end.

    1..10 | %{ "filename$_"}


    \_(ツ)_/

    Monday, August 21, 2017 10:27 AM
  • This forum is for scripting help and not scripting requests. What have you tried so far?

    A simple Google search could have shown you how to increment the file name in PowerShell, then you could just encapsulate the New-Item command inside a do-while statement which tells it when to stop creating files based on the number in the file name.

    Hope this helps! :)


    Monday, August 21, 2017 10:29 AM
  • Did you ever work this out ?
    Wednesday, August 23, 2017 10:33 AM
  • Hi Mark,

    Sorry, I am not a windows scripter, as I work on Unix systems.

    did not know that I could not request help on Windows script, my appologie

    I did not try anything yet, as I tried doing it the unix way :-) Does not work at all :-) Hense the reason I have asked for help here

    Thank you

    Thursday, August 24, 2017 5:33 AM
  • Unix shell script would do it almost exactly the same way. PowerShell was designed to be easy for Unix shell scripters to understand.  Many Unix commands are usable in PowerShell like "ls".

    To work in modern Windows as a technician it is absolutely necessary to learn PowerShell.


    \_(ツ)_/

    Thursday, August 24, 2017 5:56 AM
  • ok save this as newfiles.ps1

    Param (
    [String]$path = "c:\files",
    [String]$filenamein = "Newfile",
    [int]$numberoffiles = 10
    )

    for ($i = 1;  $i -le $numberoffiles; $i++){
    $filename = $filenamein + $i +".txt"
    $newfile = $path+"\"+$filename
    new-item $newfile -type file }

    then call it in powershell

    newfiles.ps1 -path path where you want the files - filename the name of the new files -numberoffiles the number of files you want

    e.g.

    newfiles.ps1 -path c:\mynewfiles -filename lotsofnewfiles -numberoffiles 10

    Thursday, August 24, 2017 7:28 AM
  • in linux to make a1000 empty files

    touch {1..1000}.txt

    Thursday, August 24, 2017 10:38 AM
  • Here's how to create 1000 empty files in PowerShell:


    PS C:\Temp> 1..1000 | % { New-Item "$_.txt" }

    If you don't want the output, pipe to Out-Null.


    -- Bill Stewart [Bill_Stewart]

    Thursday, August 24, 2017 5:59 PM