Delete file if it exists anywhere on the windows Device - powershell

I Am trying to delete a file which exists in like many folders which are dynamically created and I have no clue how and where to start. What would be the best approach using scripting to delete this file ?

del has a /s switch to process all subfolders:
del /s c:\googledrivesync.exe
it may take a while to scan the folder tree. Get yourself a cup of coffee...

Assuming you want to delete specific files in a directory structure (as opposed to just deleting everything), from PowerShell, you could do
dir -recurse -file -filter nameOfFileToRemove rootDirectory | remove-item
This will recurse through the file tree, looking for files matching nameOfFileToRemove and then removing them. Also nameOfFileToRemove may contain wildcard characters like "nameOfFile*.txt"

Related

Listing unwanted files recursively in a directory - Powershell

I am trying to make a Powershell script that will display a list of files and folders in a directory('s) while not displaying a specific folder and a file.
I have a bunch of directories on the C:\ that have a folder "DATA" and a "Setup.ini" file within them.
Every once in a while people go into the data folder and make copies and then place those copies of files and directories right inside of the main directory with the "DATA" and ".ini" files.
Then at the end of each month, I normally need to go through each of these directories and delete anything that is not the "DATA" folder and ".ini" file.
Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini' | Where-Object { $_.FullName -notmatch 'Data'}
This seems to sort of work and is what I have been using this month.
It runs really slow. I also noticed that one of the Directories had 2 extra folders and a zip file within it.
The script only showed one of the folders and didn't show the zip file either.
I have tried adding other things to this code, but when I do that it begins showing me data inside of the "DATA" folder which I don't want.
Any idea what I could be doing wrong or ways to make this run quicker?
I think it is taking about 7-10mins to run through 113 directories.
Thank you!
Expand the -Exclude and drop the Where-Object:
Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini','Data'
HTH

How can I copy certain file types in multiple subfolders and paste them in an empty folder while keeping the file structure of the original folders?

I am trying to copy music lyric files (.lrc) from a folder with many subfolders such as artists and albums and paste them in an empty folder, but I still want to keep the folder structure without having to create each individual folder for those files to be put in. In other words: I want to take certain files from a folder and have it automatically create the folder structure in another folder which the original file was in.
For example: I have 10 lyric files accompanied with other music files in a single folder called "ArtistName" which is a subfolder of a subfolder of a folder called "Music". These lyric files need to be in another folder called "Music2" that is currently empty, but instead of just dumping the files in the root folder, I need to recreate the folder structure in which the original lyric files were in.
Is there any way to do this? Keep in mind, I am not very experienced when it comes to programming but I know some basics. Unfortunately I might need more of an explanation than most people here. Thank you to anyone who can help!
Here's a powershell answer:
Use Copy-Item with the -Container and -Recurse switches to copy the folder structure including files to a new location.
Copy-Item -Path "Old\Path\for\Music" -Recurse -Destination "New\Path\for\Music" -Container
If you only want to copy the lyric files while retaining the folder structure use a -Filter
Copy-Item -Path "Old\Path\for\Music" -Recurse -Filter "*.lrc" -Destination "New\Path\for\Music" -Container
You can do this in CMD
xcopy /s yourFolder\Subfolder\*.pdf destinationFolder\myfolder
powershell, recurse option keeps the folder structure
copy-item c:\\srcFolders\\* f:\\dst -force -recurse -verbose

Copying folder structure to location that doesn't exist

I want to copy a folder, complete with subdirectories, files and files within subdirectories, preserving the structure and create them in a new location that did not previously exist. This is my PowerShell code
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\foldertwo -recurse -Container
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\folderthree -recurse -Container
foldertwo exists and is empty, folderthree does not exist.
If I run the script, the structure is created correctly in foldertwo, however folderthree gets created, but contains only all the files from the entire substructure, all at the root folderthree level. It has not recreated the subfolders within folderone, just put all the files at the root level of folderthree.
What have I done wrong?
Here's a very basic, but fully working and tested example, building on your confirmation above that I understood the issue at-hand:
$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
if (!(Test-Path "C:\Development\PowerShell\$folder"))
{
mkdir ("C:\Development\PowerShell\$folder") | Out-Null
}
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\$folder -recurse -Container
}
From what I understand, the question is about recreating the folder structure from [source] to [destination]. As using CmdLets is kind of overkill (and performance loss), I suggest simple batch command that may also be ran in powershell.
xcopy [source] [destination] /T /E
xcopy is a function to copy file and directory trees.
Help provides us info on usefult parameters on the case:
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.

backing up certain files in a folder using powershell

I have a couple of questions. I have certain binaries in a folder...
F:\program files\application\Client\
I only want to copy the latest dll's that have a certain phrase in there names. Lets say "MVCsite". I know you can use Get-ChildItem -Filter with the filter parameters, to get just child items with a .dll extension, but is there a way to look for specific files with specific keywords, or am I going to have to literally copy a list of files out of the directory and move it to a back up folder? Is there a quick and dirty command to do that? As you can tell, I am new to powershell, but I am learning fast.
gci -path $path -filter "*.dll" | where {$_.Name -match "Keyword1|Keyword2|etc"}
Simply use wildcards in your -Path parameter:
Copy-Item F:\program files\application\Client\*MVCsite*.dll X:\DestinationDir
That will copy all files in F:\program files\application\Client\ whose names contain the string "MVCsite" and have the extension ".dll". Is that what you wanted to accomplish? If not, please clarify.

What's the proper way to copy files while preserving folder structure in powershell?

I can never seem to get this right.
I have an existing folder c:\MyApps\Websites\MySite that already has an existing website running in it. I've downloaded the latest bits which are located under c:\temp\MySite\artifacts.
when I try to run this
$source "c:\temp\MySite"
$destination "c:\MyApps\Websites\MySite"
copy-item $source $destination -recurse -force
if c:\MyApps\Websites\MySite already exists then it tries to put it in c:\MyApps\Websites\MySite\artifacts, but if it doesn't already exist it copies it properly. Not sure what's going on here. Any suggestions?
Just use the robocopy command. It's designed for this kind of thing.
robocopy $source $destination /E
It's shipped with Windows 7, so it's an internal command, so it's still a 'Powershell' way, imho, but if you're wanting to use the copy command, then you are very close, but your current implementation grabs the source folder and puts it inside target (ie the result would be C:\target\source\files, not C:\target\files). When you want to make the inside of target look like the inside of source, you need to do:
cp C:\source\* C:\target -r -fo
Notice the *, this grabs the contents of the source folder. If you want target to be cleared first, just do rm -r -fo C:\target\* before the copy.
Powershell doesn't handle long paths, so you will need to use robocopy anyway if you have a long filename or deep folders.