Powershell , remove folder with the same name like .zip - powershell

I must delete files which have been extracted from a zip file, into a folder named after the zip file, i.e.:
\test1.zip -> \test1
My script must find the folder which have the same name as the zip file and delete this folder.

Get a list of all of the Zip files in the directory, then loop over the results and delete any folder with the same name minus the extension, also known as the BaseName.
Get-ChildItem -Filter *.zip | `
ForEach-Object { if (Test-Path $_.BaseName) {
Remove-Item -Recurse -Force $_.BaseName }
}
You can enter the entire command on one line, I have split it up so that it is easy to read on here. I used the following commands in this example:
Get-ChildItem - Creates a object in the pipeline for each file with a .zip extension
ForEach-Object - Simply allows you to perform an action for each object in the pipeline.
Remove-Item - note the use of -Recurse and -Force ensures that the folder is removed even if it contains files, you will not be asked to confirm.

Related

Remove everything before \

I need to copy a lot of files and use the same sort of folder structure where the files needs to go.
So for instance if I have the following two documents:
\\Server1\Projects\OldProject\English\Text_EN.docx
\\Server1\Projects\OldProject\English\Danish\Text_DA.docx
I would need to move them to a new place on the server, but they need to be in the same "language folder". So I need to move them like this:
\\Server1\Projects\OldProject\English\Text_EN.docx -> \\Server1\Projects\NewProject\English\Text_EN.docx
\\Server1\Projects\OldProject\English\Danish\Text_DA.docx -> \\Server1\Projects\NewProject\English\Danish\Text_DA.docx
The issue here is, that I would need to take names of the "language" folder and create them in the NewProject folder.
How would I be able to take and remove everything before the \, so I end up with only having the "language" folders like English\ and English\Danish
If the goal it to just replace the 'OldProject' folder with 'NewProject' in the file path you could use replace to make the change to the file path:
$filePath = Get-ChildItem \\Server1\Projects\OldProject\English\Text_EN.docx
Copy-Item $filePath.FullName -Destination ($filepath.FullName -replace "\bOldProject\b", "NewProject")
The '\b' is used to do a regex EXACT match for anything inside the tags.
Try the following, which, for each input file:
constructs the target dir. path by replacing the old project dir.'s root path with the new one's, thereby effectively replicating the old dir.'s subdirectory structure.
makes sure that the target dir. exists
then copies the input file to the target dir.
$oldProjectRoot = '\\Server1\Projects\OldProject'
$newProjectRoot = '\\Server1\Projects\NewProject'
Get-ChildItem -Recurse -Filter *.docx -LiteralPath $oldProjectRoot |
ForEach-Object {
# Construct the target dir. path, with the same relative path
# as the input dir. path relative to the old project root.
$targetDir =
$newProjectRoot + $_.Directory.FullName.Substring($oldProjectRoot.Length)
# Create the target dir., if necessary (-Force returns any preexisting dir.)
$null = New-Item -Force -Type Directory $targetDir
$_ # Pass the input file through.
} |
Copy-Item -Destination { $targetDir } -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

Rename bulk files with given name, in directory and subfolders using PowerShell

I’m looking for a solution to rename all files with given name in the directory and subfolders.
E.g.
There is a directory called c:\blah and in this folder, there is one file called BaR.txt and one folder called foo. In the foo folder, there is another file called bAr.txt.
I want to find all files with name bar.txt and change the name of each file to “neo.txt”. It must also be possible to rename the files to lower case as well e.g. bar.txt.
So far, I’ve tried to do this manually using the Windows Explorer in Windows10 but for some weird reason when I try to rename the bulk files there is extra “(1)” sign added to the end of each file name.
I’ve tried to play with PowerShell and I created a command
$_.name -replace 'bar.txt','neo.txt'
But it didn’t work for me.
To do this, you need 2 cmdlets: Get-ChildItem and Rename-Item.
This should work:
Get-ChildItem -Path 'C:\Blah' -Filter 'bar.txt' -Recurse | ForEach-Object {
Rename-Item -Path $_.FullName -NewName 'neo.txt'
}
However, if inside the foo folder there are more subfolders, this code will rename all files called bar.txt (case-insensitive) it finds in all of these subfolders.
If you do not want to go any deeper than the first subfolder in C:\Blah (the one called foo), you can use the -Depth parameter on the command like this:
Get-ChildItem -Path 'C:\Blah' -Filter 'bar.txt' -Recurse -Depth 1 | ForEach-Object {
Rename-Item -Path $_.FullName -NewName 'neo.txt'
}

Powershell Recursive Copy Except Files found in Exclusion directory

Hi I have a folder Called "A" and folder "A" has files and sub folders within it. I also have another folder directory called "Exclusion" with some copied files and folders from "A" within it. I'm looking for a Powershell script or Command Line option that will COPY & MOVE all the objects from A that are NOT found in the Exclusion directory to a new folder directory called "Output".
Thanks,
-B
Use Get-ChildItem to get a list of files in your exclusion directory, then take only the names of the files and hold those in an array.
Optionally use New-Item with the -Force parameter to ensure that your output directory exists before sending files there.
Next use Get-ChildItem to iterate through all files in our source (A) directory, use Where-Object and the -notin operator to exclude any files which have the same names as those gathered from your exclusion directory, then use Move-Item to move the files to your destination (Output) directory.
[string[]]$filenamesToExclude = Get-ChildItem -Path 'c:\somewhere\exclusion' -Recurse | Select-Object -ExpandProperty Name
New-Item -Path 'c:\somewhere\output\' -ItemType 'Directory' -Force | Out-Null #ensure the target directory exists / don't output this command's return value to the pipeline
Get-ChildItem -Path 'c:\somewhere\A' -Recurse | Where-Object {$_.Name -notin $filenamesToExclude} | Move-Item -Destination 'c:\somewhere\output\'

Rename a bulk of files based on a txt file

I am trying to rename some configuration files that reside into a folder. Some of them have the ".disabled" extentions, some don't.
My intention: foreach file in files-to-change.txt (relative path to the .config file, one under the other), if the file has the ".disabled" extension, remove it, if it doesn't have it, add it. This needs to apply only to the files in the .txt source file.
Basically the files
app_config\file1.config
app_config\CBS\file2.config.disabled
app_config\file3.config
app_config\CBD\Testing\file4.config.disabled
reside in the txt file and they need to match with the files in the destination folder in which I need to change the extension.
I miss the login in creating a proper script to have this completed.
where it says -path you will need to change this to reflect the location of your files.
You can use rename-item cmdlet, here is a simple script i created :
$name=(Get-ChildItem -Path 'C:\testing\New folder\').FullName
foreach ($item in $name) {
if ($item.Contains("disabled"))
{
rename-item -Path $item -NewName $item.Replace(".disabled","") -ErrorAction SilentlyContinue
}
else
{
rename-item -path $item -newname $item.Replace(".config.",".config.disabled.") -ErrorAction SilentlyContinue
}
}

PowerShell - Rename file to the name of the folder in which it is located

I don't know if this would be best done in PowerShell, but basically I have a lot of movies with names that are incorrect. The folder name for each movie is correct however.
Within a folder, I want to go through each folder and rename an .mp4 file to the same name as the folder.
Each folder has only an .mp4 file in it and a .jpg file, but I want to rename just the .mp4 file (although renaming both really wouldn't be bad either.)
Is there a simple way to do this in PowerShell?
A readable version:
Get-ChildItem -Attributes Directory D:\Videos | ForEach-Object {
Get-ChildItem -Path $_ *.mp4 | Rename-Item -NewName "$_.mp4"
}
The first Get-ChildItem gets all the directory objects within D:\Videos and ForEach-Object iterates over each of those directories as $_ in the following block.
Inside the block, Get-ChildItem is used again to get an mp4 file from the given directory via the -Path option. Finally, Rename-Item is used to rename the video file without moving it from its current directory.
Something like this should work:
# run from your D:\Movies (or whatever) folder
# Go through all subfolders of the folder we're currently in, and find all of the .MP4
# files. For each .MP4 file we find...
ls -Recurse -Filter *.mp4 | %{
# Get the full path to the MP4 file; use it to find the name of the parent folder.
# $_ represents the .MP4 file that we're currently working on.
# Split-Path with the -Parent switch will give us the full path to the parent
# folder. Cast that path to a System.IO.DirectoryInfo object, and get the
# Name property, which is just the name of the folder.
# There are other (maybe better) ways to do this, this is just the way I chose.
$name = ([IO.DirectoryInfo](Split-Path $_.FullName -Parent)).Name
# Tell the user what we're doing...
Write-Host "Renaming $_ to $($name).mp4..."
# Rename the file.
# We have to provide the full path to the file we're renaming, so we use
# $_.FullName to get it. The new name of the file is the same as that of the
# parent folder, which we stored in $name.
# We also remember to add the .MP4 file extension back to the name.
Rename-Item -Path $_.FullName -NewName "$($name).mp4"
}
Here's a cross version example:
Get-ChildItem D:\temp\*\*.mp4 | Rename-Item -NewName {$_.Directory.Name +'.mp4'}