I’m trying to copy everything from folder2 to dest.
Current structure:
c:\root\folder1\folder2
C:\dest
There are thousands of folder1s and thousands of folder2s under root. I need all the folder2s plus contents in dest.
Any help much appreciated
I tried it before with this but it fails:
$files = Get-ChildItem “c:\root\folder1”
Get-ChildItem $files | Move-Item -Destination “c:\dest”
Related
I'm trying to clean up a directory with a ton of files with windows powershell, and so far all the other StackOverflow posts haven't seemed to help me crack my issue.
I have a parent directory named /1/
I have a sub directory named /1/j/
I want to have all of the files in directory /1/ with (J) in any part of their names (including the parenthesis) moved into the /j/ sub directory. Example filename would be: "example filename (J).smc"
Here is the code I have so far that's not working:
$source = 'F:\1\'
$destination = 'F:\1\j'
Get-ChildItem $source -filter *.smc -recurse | Select-String -List -Pattern "(J)" | ForEach-Object {
Move-Item $PSItem.Path -Destination $destination
}
I feel like it's something simple, so I apologize if it is! Thanks for your help!!!
Made it harder than it had to be. Thanks Olaf!
Solution:
Get-ChildItem -Path 'F:\1\' -Filter *'(j)'*.smc | Move-Item -Destination 'F:\1\j'
I'm working on a project cleaning up a file server's folder redirection folders. Would like to ask for some help with a PS script that would move the files in user folders into new "Documents" sub-folders, as we're facing manually performing this for a lot of profiles.
A tree example currently looks like:
―FolderRedirection
―――JContoso
――――――File.docx
――――――File.pptx
―――MDerby
――――――File.docx
――――――File.pptx
I'd like to be able to achieve:
―FolderRedirection
―――JContoso
――――――Documents
―――――――――File.docx
―――――――――File.pptx
―――MDerby
――――――Documents
―――――――――File.docx
―――――――――File.pptx
This should work, bear in mind for future questions you should provide at least a minimal attempt at solving the problem.
The inline comments should help you with the code logic.
# get all folders in `FolderRedirection`
foreach($dir in Get-ChildItem .\FolderRedirection -Directory) {
# create a new `Documents` folder in each sub folder
$dest = $dir | Join-Path -ChildPath Documents
$dest = New-Item $dest -ItemType Directory -Force
# get all files in each folder and move them to the new folder
$dir | Get-ChildItem -File | Move-Item -Destination $dest
}
I have many folders, some of which have subfolders. I want to find and copy to special directory all files of one type, for example .txt, from them. I use this script and it works, but it copies all folders on the way to my file too. How can I avoid this? I thought about deleting all empty folders after copying, but I'm sure that the more right way exists.
$folders = [IO.Directory]::GetDirectories("S:\other","*","AllDirectories")
Foreach ($dir in $folders) {
Copy-Item -Path $dir -Filter *.txt -Recurse -Destination 'D:\FinishFolder'
}
Use Get-ChildItem to discover the files, then copy them 1 by 1:
Get-ChildItem S:\other -Filter *.txt -Recurse |Copy-Item -Destination 'D:\FinishFolder'
I am trying to clear directories but I want to leave folders that have csv files inside. I tried to use this command:
Remove-Item -Path "$Using:MyPath" -Exclude "*.csv", "*.end" -Recurse -Force -ErrorAction Stop
But it will not work with the structure below. If a csv file is inside
Sub2Folder Remove-Item will remove SubFolder with all it's contents and I will lose my csv files. Is there a way to avoid this situation and not to delete folders if any of it's subfolders has a csv file inside?
MainFolder
SubFolder
Sub2Folder
somefile.csv
I made a small script which does exaclty this. Ask me if you need more help ;)
$subfolders = Get-Childitem "$Using:MyPath\*" -Directory
#Get all folders
#foreach folder in the folders
foreach ($subfolder in $subfolders) {
if (!(Get-Childitem $subfolder.FullName -Recurse -Filter "*.csv")){ #If subfolder empty
Remove-Item $subfolder.FullName -Force -Recurse #Remove this folder
}
}
Edit: Thanks to Grzegorz Ochlik I did some changes to the script. (-Directory switch and no measure-object
If I helped you, please mark this post as the answer :D
This is working for me:
Get-ChildItem "$Using:MyPath" -Exclude "*.csv","*.end" -Recurse -File | Foreach {Remove-Item $_.Fullname}
This will list All files except the wildcarded files and remove them.
Can I somehow exclude a folder when I compress an archive like this?
$compress = Compress-Archive $DestinationPath $DestinationPath\ARCHIVE\archiv-$DateTime.zip -CompressionLevel Fastest
Now it always saves the whole folder structure of $destinationpath to the archive, but since the archive is in the same folder, it always gets zipped into a new archive, making the archive double in size every time I run the command.
Get all the files you want to compress, excluding the files and folders you don't want compressed and then pass that to the cmdlet
# target path
$path = "C:\temp"
# construct archive path
$DateTime = (Get-Date -Format "yyyyMMddHHmmss")
$destination = Join-Path $path "ARCHIVE\archive-$DateTime.zip"
# exclusion rules. Can use wild cards (*)
$exclude = #("_*.config","ARCHIVE","*.zip")
# get files to compress using exclusion filer
$files = Get-ChildItem -Path $path -Exclude $exclude
# compress
Compress-Archive -Path $files -DestinationPath $destination -CompressionLevel Fastest
you can use -update option of Compress-Archive. Select your subdirs with Get-ChildItem and Where
like it:
$YourDirToCompress="c:\temp"
$ZipFileResult="C:\temp10\result.zip"
$DirToExclude=#("test", "test1", "test2")
Get-ChildItem $YourDirToCompress -Directory |
where { $_.Name -notin $DirToExclude} |
Compress-Archive -DestinationPath $ZipFileResult -Update
I know this question is rather old, but wanted to post my solution here. This solution has worked for me and I hope it may help someone else having the same issue.
I took the ideas from the previous answers and developed them a bit.
So generally speaking what you need to do is to create two lists, one for the files in the root directory and another one for directories (excluding the directory you'd want to omit). Then you need to concatenate these two lists together and put them into -Path parameter of Compress-Archive cmdlet.
Voila! It will create a .zip archive with all files and directories we need, preserving the directory structure.
$files = Get-ChildItem -Path /RootDir -File
$directories = Get-ChildItem -Path /RootDir -Recurse -Directory -Exclude DirToExclude
Compress-Archive -Path $($files + $directories) -DestinationPath Archive.zip