# Copying files in a range

**URL:** <https://forums.powershell.org/t/copying-files-in-a-range/16712>\
**Category:** PowerShell Help\
**Created:** [June 2, 2021, 12:15pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712 "2021-06-02T12:15:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 2, 2021, 12:15pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/1 "2021-06-02T12:15:09Z")

</div>

Hi, im trying to get this script to work. this script searches for all .doc files inside multiple folders, copy’s them and rename any duplicates.

the folder where it searches is now named ‘folder1’ inside that folder there are alot of other folders named ‘account\_1’ until ‘account\_800’ how can i edit the script so that it searches inside ‘Folder1’ and then only account\_4 to account\_30 for example. i dont want to search in 1,2 and 3 and everything after account\_30.

i tried with select-object -first $number but that doesnt fully work. as it counts the number of .doc files found and not the number of folders i want to search in.

this is what i got so far.

$dest = “Destination of new folder with todays date”  
$folderName = (Get-Date).tostring(“dd-MM-yyyy”)  
New-Item -itemType Directory -Path $dest -Name $FolderName

function fcopy ($SourceDir,$DestinationDir)  
{

```
Get-ChildItem $SourceDir -Filter "*.doc" -Recurse | Where-Object { $_.PSIsContainer -eq $false } | ForEach-Object ($_) {

	$SourceFile = $_.FullName
	$DestinationFile = $DestinationDir + $_

	if (Test-Path $DestinationFile) {
		$i = 0
		while (Test-Path $DestinationFile) {
			$i += 1
			$DestinationFile = $DestinationDir + $_.basename + "--COPY--" + $i + $_.extension
		}
	} else {
		Copy-Item -Path $SourceFile -Destination $DestinationFile -Force
	}
	Copy-Item -Path $SourceFile -Destination $DestinationFile -Force
}

```

}  
fcopy -SourceDir "C:\Source\Directory\Folder1" -DestinationDir "C:\Users.…\Desktop\Scripts$foldername"

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 2, 2021, 1:02pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/2 "2021-06-02T13:02:00Z")

</div>

You can specifiy a range in PowerShell using `..`

So you could do something like this:

```auto
$range = 4..30
foreach ($n in $range) {
    fcopy -SourceDir "C:\Source\Directory\Folder1\Account_$n" -DestinationDir "C:\Users…\Desktop\Scripts\$foldername"    
}

```

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 2, 2021, 1:19pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/3 "2021-06-02T13:19:21Z")

</div>

> [@matt-bloomfield](#):
>
> `Account_$n`

thank you, i encountered this all over google with $range but didnt work. probably a typo somewhere that i couldnt find. thank you for you solution.

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 3, 2021, 8:51am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/4 "2021-06-03T08:51:16Z")

</div>

@matt-bloomfield the script work flawless locally but when i edit the -SourceDir and -DestinationDir to network locations it doesnt work. i dont get an error it just searches endlessly. when the -SourceDir has a local location and -DestinationDir a network location it works. the adres is like this: \lan.something.eu\abc\AAA-SomeThing\ABC\Accounts\Account\_123. i tried it with and without backslash and " ’ but none of that works. am i missing something?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 3, 2021, 9:14am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/5 "2021-06-03T09:14:12Z")

</div>

I can’t see a reason why that wouldn’t work. You will need double quotes `"` around the path to ensure the variables are expanded and the UNC paths should start with a double backslash `\\`.

Can you copy a file manually between the network locations? e.g.

`Copy-Item \\server1.lan.something.eu\abc\source\Account_123\testfile.txt\\server2.lan.something.eu\destination\FolderAccount_123\`

If that works with `Copy-Item` it’s probably a problem with your fCopy function. If it doesn’t work, then perhaps it’s a permissions problem.

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 3, 2021, 9:46am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/6 "2021-06-03T09:46:57Z")

</div>

i can copy manually and with the copy-item command it is possible too. i got another script before this one and that one worked (without renaming duplicates and range) so permission isnt a problem. only difference is that this script has the function command and the other one doenst have that. so i guess its somehting in the fcopy…

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 3, 2021, 10:46am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/7 "2021-06-03T10:46:00Z")

</div>

Ok, I would replace the `Copy-Item` lines with `Write-Host` so you can see if the paths you think you’re generating are the paths you’re actually generating.

```auto
Write-Host "-Path $SourceFile -Destination $DestinationFile -Force"

```

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 7, 2021, 7:07am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/8 "2021-06-07T07:07:23Z")

</div>

it doesnt show anything, when i set the search on the network path, powershell only shows that the directory is created. i tried a few adjustments in the script and still no luck. i have an older script that works but without the range en renaming duplicates. im thinking to start over with a new script in the hope dat somehow it wil work for some reason 🙂

$number = 6  
$dest = “path”  
$files = Get-ChildItem -Path “path” -Filter “\*.doc” -Recurse | Select-Object -First $number  
$folderName = (Get-Date).tostring(“dd-MM-yyyy”)

New-Item -itemType Directory -Path $dest -Name $FolderName

foreach ($file in $files) {  
$file\_path = Join-Path -Path $dest -ChildPath $file.Name

```
$i = 1

while (Test-Path -Path $file_path) {
    $i++
    $file_path = Join-Path -Path $dest -ChildPath
    "$($file.BaseName)_$($i)$($file.Extension)"
}

Copy-Item -Path $file.FullName -Destination $dest$folderName

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 7, 2021, 9:29am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/9 "2021-06-07T09:29:20Z")

</div>

I know it’s not an answer to your actual question but I could imagine it could be a solution for your actual challenge.

I’d consider using a combination of [Windows Shadow Copies](https://docs.microsoft.com/en-us/windows-server/storage/file-server/volume-shadow-copy-service) and backup software to save several different versions of one file. There are tools able to store whole folder structures without wasting too much space. One of them would be [HardlinkBackup](http://www.hardlinkbackup.de).

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 7, 2021, 9:51am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/10 "2021-06-07T09:51:22Z")

</div>

thanks, i will look into those links you provided. its probably the easiest to use those programs but i cant let this go without getting it to work. i recently started with powershell so wanna know how why this is working or i have sleepless nights about it 😉

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 7, 2021, 11:31am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/11 "2021-06-07T11:31:05Z")

</div>

I have tested it with network locations and it works as expected for me. Obviously, I’m testing on a much smaller scale than your several hundred folders.

The only thing I’ve noticed is in both your original post and a subsequent reply (#7 in this thread). You’ve not got any slashes in the destination. It should look something like this:

```auto
$range = 1..2

foreach ($n in $range) {

        fcopy -SourceDir "\\myFileServer\Data\_z_Temp\old_account$n" -DestinationDir "\\myFileServer\Data\_z_Temp\new\$folderName\"    

}

```

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 7, 2021, 12:11pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/12 "2021-06-07T12:11:45Z")

</div>

Hey Matt,

thanks for testing, i think i just found the ‘bug’ as i said before the whole script works 100% locally on my pc. i search for “\*.doc” and i find .docx files. when i switch all paths to the network locations the filter stops working. so when i set the network locations and i write docx instead of doc. then it works suprisingly. so now i trying to apply multiple filters and then it should work how i want.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 7, 2021, 1:19pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/13 "2021-06-07T13:19:06Z")

</div>

Good find 👍 - I don’t remember seeing that being mentioned before but that’s a nasty inconsistency. Looks like it’s fixed in 7.x though.

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [June 7, 2021, 1:32pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/14 "2021-06-07T13:32:16Z")

</div>

very frustrating, but thanks for your help

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [April 6, 2022, 10:47am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/15 "2022-04-06T10:47:27Z")

</div>

Hello again,

This is an older topic, this was part of bigger script. and i want to add a command that it searches in specific folders, but the range variable doesnt work. the below script searches in USername1 and gives back the number 6 as i have 6 docx files in there. but when i edit $range to a different setup like you can see below. the range doesnt work. i tried various thing and loops etc. i dont have to see where the .docx files are. i only need to see the number. any help is appreciated (again) 😉

```auto
Write-host "Checking if there are any files..." -f Yellow -NoNewline
$range = (1)
#$range = @(1)
#$range[1..6]

$folder = (Get-ChildItem -Path "C:\testfolder\Username$range" -filter *.docx -recurse -file | Measure-Object).Count

foreach ($document in $folder)
{
  write-host $folder files found -f green
}
if ($document -eq 0)
{
write-host nothing, i quit... -f red -NoNewline
start-sleep -s 3
exit
}
Else
{
write-host continue! -f green
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 6, 2022, 11:54am UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/16 "2022-04-06T11:54:11Z")

</div>

It’s not clear what you’re actually trying to do. You named your variable `$folder` but you’re searching for files. If you want to search a range of folders you will have to create an array of the paths to this folders. The `-Path` parameter does not work with regex but it takes one or more paths.  
So if it’s what I guess it is this should work:

```auto
$PathList = 1..6 | ForEach-Object { 'C:\testfolder\Username{0}' -f $_}
$folder = (Get-ChildItem -Path $PathList -Filter *.docx -Recurse -File | Measure-Object).Count

```

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [April 6, 2022, 12:15pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/17 "2022-04-06T12:15:42Z")

</div>

i i have a main folder named “users” and inside there i have “User1”, “User2” etc etc.

i want the script to count the docx files inside “user10” til “user16” or “User1” til “user5” for example

The variable $folder is a poor choice of word of translation from me 🙂 . but in the real script is called countingfiles.

dunno if my question is more clear now, but the solution doesnt work

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 6, 2022, 12:28pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/18 "2022-04-06T12:28:41Z")

</div>

> [@Stefmeister](#):
>
> i i have a main folder named “users” and inside there i have “User1”, “User2” etc etc.

In the code you shared the path name was `C:\testfolder\Username .... ` and of course I took your code to create my suggestion. So when you adapt my suggestion with your correct names it should work. You can check by outputting the array `$PathList`.

---

<div class="post-metadata">

**Author:** ![Stefmeister](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Stefmeister](https://forums.powershell.org/u/Stefmeister)\
**Post date:** [April 6, 2022, 12:38pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/19 "2022-04-06T12:38:17Z")

</div>

ignore my previous post. forgot to change the 1 to 6 numbers to the corresponding number at my pc… i did change the rest  
so yeah your answer did work 🙂 thank you very much

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 6, 2022, 12:41pm UTC](https://forums.powershell.org/t/copying-files-in-a-range/16712/20 "2022-04-06T12:41:38Z")

</div>

> [@Stefmeister](#):
>
> so yeah your answer did work

cool. I’m glad it helped. 👍🏽

[Next page](https://forums.powershell.org/t/copying-files-in-a-range/16712.md?page=2)
