I am trying to make a script in powershell to delete all folders in C:\Temp which contains a *.sr_processed file.
I already have this but this only deletes the file and not the folder it was in.
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { $_.Delete()}
Your are telling it to delete the file. To delete the folder do something like:
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { Remove-Item –path $_.Directory.Fullname}
If you have multiple .sr_processed files in a folder it might attempt to delete it more than once. And generally deleting a folder you are globbing is bad practice. So a better idea would be to gather up the folders in a list/hash and delete them at the end.
That would look something like:
# declare array
$foldersToDelete = #()
# fill array with folder names
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { $foldersToDelete += $_.Directory.Fullname}
# sort and make unique
$foldersToDelete = $foldersToDelete | Sort-Object | Get-Unique
# delete folders
$foldersToDelete | Remove-Item –path $_
This is typed from memory, so you might want to adjust it.
Related
I have a long list of folders. Most of the folders follow the "name_#name" format. I have some that don't follow that structure. I want to move all the folders (and the sub-folders/files within) that DON'T have "_" in the folder name.
For example:
test_#12352
moose_#4532
horse_#84462
cow24
fish3
Moved:
cow24
fish3
I think Move files when they contain a specific word? could be modified to make it work...just not sure how. I'm used to just using GUI, this is my first time using PowerShell
When I tried using that code in that link it didn't work with my situation
What you want to do is just filter the list before you move any files
So you can use the following to pick up all the files you want
$Files = Get-childItem -Path $Path -File
You can then filter it down. My favourite way is to pipe the variable into Where-Object and play around with the individual properties and match types. Since you don't want to include the _ we can use a -notmatch "_" to exclude those values
$Files = Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"}
And finally, you can move the files
$Files | move-item -path $_.FullName -Destination $Destination
Or as a one liner
Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"} | move-item -path $_.FullName -Destination $Destination
*Please note I haven't really tested this code. So test it out yourself before you run it
I need to move set of specific files having different extension to another folder.
I have following filtered files in the directory.
file1.txt
file2.xml
file3.dll
I have kept the above files in the variable $files and I need to move each of files to another folder.
Below is the code I tried.
foreach ($fileType in $files) {
Get-ChildItem -Path C:\Files -Filter "$fileType*." -Recurse |
Move-Item -Destination C:\Dest
}
I am getting following error
Get-ChildItem : Illegal characters in path.
At line:1 char:38
+ ... lude_files){Get-ChildItem -Path C:\Files
Appreciate if anyone can help on this?
An easy way to do this
ls C:\files | Foreach {
Move-Item -Path C:\files\$filetype -Destination C:\dest
}
If all the files you are after share a common 'starts-with' name like file as in your example, the below should do what you want. It uses the -Include parameter where you can add an array of (in this case) extensions to look for.
Get-ChildItem -Path 'C:\Files' -Filter 'file*' -Include '*.txt','*.xml','*.dll' -Recurse |
Move-Item -Destination 'C:\Dest'
Note: the -Include parameter only works when also used together with the -Recurse switch, OR by appending \* after the path (like in C:\Files\*)
I am trying to convert code from bash to PowerShell like this:
In bash:
find ./searchfolder -type f -name "something" | xargs cp -t ./destinationfolder
I mean "find" command finds
./searchfolder/something
./searchfolder/0.15/something
./searchfolder/0.25/something
and "copy" command copies the files in the new directory (with preserving folder structure)
./destinationfolder/something
./destinationfolder/0.15/something
./destinationfolder/0.25/something
How can i do that? Thanks in advance.
How about using the Filter parameter on the Copy-Item command? This might get you close.
cp ".\searchfolder" -Recurse -Filter "something" -Destination ".\destinationfolder"
cp is an Alias of Copy-Item BTW.
This is assuming you are looking for text files in C:\temp and moving them to C:
Get-Childitem –Path C:\Temp -Recurse -Include *.txt* | ForEach-Object { mv $_.fullName c:\ }
Regards!
I am assuming this will be the folder structure. Only one Subdirectory inside Sourcefolder.
SourceFolder
--f1
-test.txt
--f2
-test.txt
--f3
-other.txt
and copy folders which has file named test
Destination
--f1
-test.txt
--f2
-test.txt
Script
cd D:\Vincent\PSTesting
$Path = 'Searchfolder'
Get-ChildItem $Path -Recurse -Filter *Test* -File | foreach {
$SourceFolder = $_.Directory -replace "^.*$Path"
Copy-Item -Path $Path\$SourceFolder -Destination "D:\Vincent\PSTesting\Destinationfolder\" -Verbose -Recurse
}
I am currently trying to run a script I wrote. It works great, but I need it to also search and remove from the hidden folders as well. It does not seem to have any effect on hidden folders... Here is my script.
Get-ChildItem C:\ -Include saplogon.ini -Recurse | foreach ($_) {Remove-Item $_.fullname}
$src_dir = "\\xxxxxxxxxx\xxxxxxxxxxxx\saplogon\saplogon.ini"
$dst_dir = "C:\Windows"
$file = Get-ChildItem $src_dir
Copy-Item $file -Destination $dst_dir
[System.Environment]::SetEnvironmentVariable('SAP_LOGON_INI', 'C:\Windows\saplogon.ini', 'Machine')
You are missing the the -Force parameter.
The code below is using alias so it won't require horizontal scrolling. Know that gci is Get-ChildItem.
Note that you will only be able to get access if you have permission.
gci c:\ -Include saplogon.ini -Recurse -Force | foreach ($_) {remove-item $_.fullname}
At this point, you probably already took care of the non-hidden files. If you want to run the script again, but only for hidden files (and not non-hidden files), you can do that with the -Hidden flag.
Again, you will only be able to get access if you have the permission.
gci c:\ -Include saplogon.ini -Recurse -Hidden | foreach ($_) {remove-item $_.fullname}
I'm trying to copy all *.csproj.user files recursively from C:\Code\Trunk to C:\Code\F2.
For example:
C:\Code\Trunk\SomeProject\Blah\Blah.csproj.user
Would get copied to:
C:\Code\F2\SomeProject\Blah\Blah.csproj.user
My current attempt is:
Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination
C:\Code\F2 -Recurse -WhatIf
However I get:
What if: Performing operation "Copy Directory" on Target "Item:
C:\Code\Trunk Destination: C:\Code\F2\Trunk".
First, it wants to put them all in a new folder called F2\Trunk which is wrong. Second, it doesn't list any of the files. There should be about 10 files to be copied over.
What's the correct syntax for the command? Thanks!
Update:
Okay, it seems to have something to do with the fact that C:\Code\F2 already exists. If I try copying the files over to a destination that does not exist, it works.
I want to overwrite any existing .csproj.user files in the destination.
You guys are making this hideously complicated, when it's really simple:
Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse
Will copy the Directory, creating a "Trunk" directory in F2. If you want to avoid creating the top-level Trunk folder, you have to stop telling PowerShell to copy it:
Get-ChildItem C:\Code\Trunk | Copy-Item -Destination C:\Code\F2 -Recurse -filter *.csproj.user
While the most voted answer is perfectly valid for single file types, if you need to copy multiple file types there is a more useful functionality called robocopy exactly for this purpose with simpler usage
robocopy C:\Code\Trunk C:\Code\F2 *.cs *.xaml *.csproj *.appxmanifest /s
Seen this before, and I don't know why PowerShell can't seem to get it right (IMHO). What I would do is more cumbersome but it works.
$Source = 'C:\Code\Trunk'
$Files = '*.csproj.user'
$Dest = 'C:\Code\F2'
Get-ChildItem $Source -Filter $Files -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Copy-Item $_.FullName -Destination $Path -Force
}
I tried Jaykul answer and it did not work for me. I had to change it as below to get it to work. I also created the C:\Code\F2 folder before it worked.
Get-ChildItem C:\Code\Trunk -Recurse -filter *.csproj.user | Copy -Destination C:\Code\F2
Answer 1 looked good, and I changed to Move-Item for my purposes. However I found that in each folder it recursively went through, it only moved the first file. Below is my complete script which also includes some conversion of doc files to pdf's:
$Source = 'C:\Users\sgrody\Desktop\NSPM-Vol1'
$MoveFiles = '*.PDF'
$Dest = 'C:\Users\sgrody\Desktop\MedPassPDF'
$Folders = Get-ChildItem $Source -Directory -Recurse
ForEach ($Folder in $Folders)
{
$wdFormatPDF = 17
$word = New-Object -ComObject word.application
$word.visible = $false
$folderpath = "$($Folder.FullName)\*"
$fileTypes = "*.docx","*doc"
Get-ChildItem -path $folderpath -include $fileTypes |
foreach-object
{
$path = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
"Converting $path to pdf ..."
$doc = $word.documents.open($_.fullname)
$doc.saveas([ref] $path, [ref]$wdFormatPDF)
$doc.close()
}
$word.Quit()
}
Get-ChildItem $Source -Filter $MoveFiles -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Move-Item $_.FullName -Destination $Path -Force
}
}
Recently I had to replace a file present in several sub folders, I did it as below.
foreach($file in (Get-ChildItem File_you_want_to_be_copied.txt -Recurse)) {$target=$file.directoryname; Copy-Item -path C:\Source_Path\File_you_want_to_be_copied.txt -Destination $target}