If you ever need to create dummy data for testing uploads, disk IO, or, what not, here is a quick way to generate large volumes of files.
Note: if the files are large, this will be a little slower, and, will quickly, eat up disk space. That's my shout out out of caveat emptor.
2..1000 | % {
copy C:\testing\000001.pdf ("{0:00000.pdf"} -f $_)
}
The notation I am using 1..1000 | % is something I got recently from
Larry Weiss, my own personal Powershell guru in the cloud, on my Technet post:
How can I reduce memory usage in scripts that operate over large collections?
The alternative, as he points out in the post, was my original format:
for($i=2;$i -le 1000;$i++) {
copy C:\testing\000001.pdf ("{0:00000.pdf"} -f $i)
}
As Larry illustrated, you can use the .. notation for quick array generation. With this syntax, you specify the min and max of the array. Pipe this to a foreach loop, % {}, and use the copy command. On thing that I use which is a little bit of a short cut is the zero fill format specifier:
"{0:00000.pdf"} -f $_
It saves me a ton of work. What this does is takes the first value on the right of the -f, in this case, the pipelined value, and, fits it into the 5-digit string in front of .pdf. If you did this manually, without specifiers, it would look like this, and, be much slower,
2..9 | % { copy .\00001.pdf 0000$_.pdf}
10..99 | % { copy .\00001.pdf 000$_.pdf}
100..999 | % { copy .\00001.pdf 00$_.pdf}
Granted, this may be a simple script, but, if you need to do something odd, like generate 1000 identical copies of a file with sequential naming, hey, now you know what to do.
0 comments:
Post a Comment