Share via


how to create multiple copies of a file with renamed filenames

Question

Friday, October 24, 2014 11:22 AM

hi,

 I want to create multiple copes of a  file [ say abc.docx] which has 3.5 MB of size  and want to rename it through power shell.

so, in the end, i want to get abc_1.docx,abc_2.docx, abc_3.docx ....abc_10.docx in the same folder with the same size. i mean, after running the ps script i should get the foldwer size set to 35 MB .

can anyone help, how to achieve this?

I am newbie in powershell. 

i tried the below :

$File = Get-ChildItem -Path "D:\ABCFolder\" -Filter Copying.bat -Recurse

for($i = 0; $i -lt $File.Count; $i++)
{
   Copy-Item $File[$i].FullName ("D:\ABCFolder\" + $File[$i].BaseName + $i +".doc")
}

help is appreciated!





All replies (12)

Friday, October 24, 2014 3:10 PM ✅Answered

i have made small change

and i am able to get the renamed file with full content :-)

thanks !!

here is the code:

$Files = Get-ChildItem -Path "D:\abcfolder\" 

for($i = 0; $i -lt 100; $i++)
{
foreach ($File in $Files)
{
    1..100 | %{ Copy-Item $File.FullName ("D:\abcfolder\" + $File.BaseName + "_$i.docx") }
}
}

hope this helps anyone...


Friday, October 24, 2014 3:46 PM ✅Answered

That's because you have 

$ext = $file[$x].Extension$name = $file[$x].BaseName

on the same line, it should be 

$ext = $file[$x].Extension
$name = $file[$x].BaseName

Friday, October 24, 2014 3:53 PM ✅Answered

Hi SaMoIPP,

your solution will overwrite the same file 100 times :)
There is no point in combining the inner and outer foreach loops. I recommend selecting one of those (my proposed solution up there uses the inner loop, but suit yourself)

Cheers,
Fred

There's no place like 127.0.0.1


Friday, October 24, 2014 11:38 AM | 2 votes

Hello SaMoIPP,

you can try this:

$Files = Get-ChildItem -Path "D:\ABCFolder\" -Filter Copying.bat -Recurse

foreach ($File in $Files)
{
    1..100 | %{ Copy-Item $File.FullName ("D:\ABCFolder\" + $File.BaseName + "_$_.doc") }
}

First of all, I changed your for-loop into a foreach loop. Far easier to read and maintain in my opinion.

Then inside the loop I created a collection of numbers (1 to 100) and for each of those, it creates a copy of the file, appending the number.

Cheers,
Fred

There's no place like 127.0.0.1


Friday, October 24, 2014 11:49 AM | 2 votes

This should work, you'll need to specify how many copies you want inside the second for loop - 

$File = Get-ChildItem -Path "D:\ABCFolder" 
$count = $file.Count


for ($x = 0; $x -lt $count; $x++) {$ext = $file[$x].Extension
$name = $file[$x].BaseName
for($i = 0; $i -lt 100; $i++)
{

   Copy-Item $File[$x].FullName -destination "D:\ABCFolder\$name`_$i$ext"
}

Friday, October 24, 2014 11:51 AM | 1 vote

Hi,

Create folder called fso on c: drive and try below command on powershell

1..100 | % { New-Item -Path c:\fso -Name "abc_$_.docx" -Value (Get-Date).toString() -ItemType file}

Mariappan S

Thanks, Mariappan Shanmugavel


Friday, October 24, 2014 12:20 PM

thx for the replies. i have created a test document with 3.5 MB file and i ran the above command , i didnt  see the files with 3.5 MB size  in the renamed fiels. though it created the  10 copies of this file,  with abc_1 abc_2...but the size of the  file was 1KB.

i  am looking for a renamed/copy  of the file which has the same size. ie , all the 100 copycat files must have 3.5 mb size.

is there any extra command/syntax i need to add . pls help

1..100 | % { New-Item -Path c:\fso -Name "abc_$_.docx" -Value (Get-Date).toString() -ItemType file}







also, just noticed that, after renaming the file , i was unable to open the contents of any of the copied files.


Friday, October 24, 2014 1:09 PM | 1 vote

That's because with Marriappans method nothing is being copied, you are just 100 new files. FWN's method is probably the best for simplicity. 


Friday, October 24, 2014 1:35 PM | 1 vote

$File = Get-ChildItem -Path "D:\ABCFolder\" -Filter Copying.Bat -Recurse

for($i = 0; $i -lt $File.Count; $i++)
{
   1..10 | Copy-Item $File[$i].FullName "$($File[$I].DirectoryName)\$($File[$i].BaseName)_$_$($File[$i].Extension)"
}

Friday, October 24, 2014 3:14 PM

You were given 3 working ways of doing it... 


Friday, October 24, 2014 3:17 PM

but the below one didnt work:

$File = Get-ChildItem -Path "D:\ABCFolder\" -Filter Copying.Bat -Recurse

for($i = 0; $i -lt $File.Count; $i++)
{
   1..10 | Copy-Item $File[$i].FullName "$($File[$I].DirectoryName)\$($File[$i].BaseName)_$_$($File[$i].Extension)"
}
am getting the below  erro when executing this code:

$File = Get-ChildItem -Path "D:\mfolder\" 
$count = $file.Count


for ($x = 0; $x -lt $count; $x++) 
{
$ext = $file[$x].Extension$name = $file[$x].BaseName
for($i = 0; $i -lt 100; $i++)
{

   Copy-Item $File[$x].FullName -destination "D:\mfolder\$name_$i$ext"
}
}

/////////////////////////////error below://///////


At D:\mfolder\createcopies2.ps1:7 char:27
+ $ext = $file[$x].Extension$name = $file[$x].BaseName
+                           ~~~~~
Unexpected token '$name' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken




Saturday, October 25, 2014 10:57 AM

thanks for your time and reply....

let me try ...will update soon...