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
@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?
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.
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…
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.
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
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 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.
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
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:
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.
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)
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
}
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:
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.
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