Delete specific folders in many folders and subfolders - loops

So I've noticed that Windows creates hidden folders called "blabla.jpg.files" if you have enabled the thumbnail view in your picture folder. I sync my picture folder to my phone and NAS and would like to remove those hidden folders from there with powershell.
Since my picture folder also has subfolders which also have subfolders and so on, I would like to create a little loop.
The script should just scan in all subfolders of one folder for hidden folders with the name "*.files" and remove them.
Can somebody help me with this one.

You don't need to create loop as Get-ChildItem can search recursively for the specified item e.g.:
Get-ChildItem <path> -r -attributes h+d *.files | Remove-Item -r -whatif
The above command requires features that are new in PowerShell v3. If the list of folders to be deleted looks correct, remove the -whatif to actually delete them.
BTW I don't see that Windows creates these folders on my system. It does create a hidden file called Thumbs.db. Perhaps it is some other program that is creating these hidden folders?

Related

How Can I Batch Rename Files in a Folder?

I have a folder with hundreds of files.
I need to rename the files in this folder one by one; but this takes a lot of time.
I did some research and found that it can export names from a text file to files in any folder I want.
But the videos contained insufficient information for me to implement it.
What I've been able to do so far:
I was able to transfer file names to TXT file with CMD command.
Note: After exporting the filenames I found some errors in the sorting.
You can see the related error in the "Example Files II" section.
Example files I:
Files and their real names.
Example files II:
I exported the file names to txt file with CMD; but the order of the fil names is not the same as in the TXT file with slight differences.
Note: This is not a big problem for now; but I would like to know if there is a way to do the correct sorting.
Export File Names
Example files III:
I batch corrected the filenames with EmEditor.
Now I need to automatically replace these names with the files in the folder.
Here I don't know how to do this.
If anyone can provide practical information on this subject, I would be glad.
Corredted File Names
You can use Powershell and a regular expression to rename files. Go to the folder in Explorer, select Open Windows Powershell on the File menu, and enter the following:
Get-ChildItem *.zip | Rename-Item -NewName { $_.Name -replace '-[0-9]{4}-[0-9]{2}-[0-9]{2}(-[0-9]{2}-[0-9]{2}-[0-9]{2}\-utc)?\.zip$','.zip' }
References:
Use Regex / Powershell to rename files
Replacement operator

Replacing files in a *.zip file without unzipping (extracting) in Powershell

Hi everyone!
Currently, I am trying to create a script in Powershell that will replace/delete files in a *.zip file without extracting it.
I know it is possible to do manually by using applications like 7Zip and WinRAR or by simply opening a zip archive in Folder Explorer, but I am looking for a way to automate this action.
I am new to powershell and to programming in general, so I cannot come up with any preliminary code. I tried to search for answers on the net, but failed to find anything that would work for me.
So the question is:
Lets say we have a zip archive called Photos (Photos.zip). The archive contains 5 images. How do I replace/delete images in Photos.zip without extracting it in Powershell?
Any code/ideas/links to helpful resources would be highly appreciated.
I used the following solution.
First I moved old files from the archive to a temporary folder by using this code:
#Path to the temporary folder
$pathToTemporaryFolder = "C:\...\Temporary"
#Path to a file that will be moved from the archive to the temporary folder
$pathToFileToBeMoved = "C:\...\Photos.zip\My Photos\My Image.png"
(New-Object -COM Shell.Application).NameSpace("$pathToTemporaryFolder").MoveHere("$pathToFileToBeMoved")
Then, I copied some new files to the archive:
#Path to the folder with a new file
$pathToFolderWithNewFile = "C:\...\New Images\My New Image.jpeg"
#Path to a folder in the archive where the new file will be copied to
$pathToFolderInArchive = "C:\...\Photos.zip\My Photos"
(New-Object -COM Shell.Application).NameSpace("$pathToFolderInArchive").CopyHere("$pathToFolderWithNewFile")
Finally I deleted the temporary folder
Remove-Item -Path "C:\...\Temporary" -Recurse
This is how I deleted files from the archive and copied new files to it without unzipping/extracting.

While Zipping files suing batch script it Zips the folder structure as well

I have files at location D:\data\Generate which needs to be Zipped and copied to
D:\data\Upload directory
I used command
set generate=D:\Data\Generate
set upload=D:\Data\Upload
cd %generate%
zip - * >> %upload%\%%i.zip
If I run this command from cmd it works fine but while running it from a
scheduler (ex: Control-M) it actually copies all the files from Control-m config directory into the zip folder.
If I explicitly mention the directory under whose the files needs to be zipped
zip - %generate%*.* >> %upload%\%%i.zip
the final zip folder actually contains the whole directory structure too instead of just the files.
ex: Zip file contains Data folder, Generate folder and the files under Generate folder
Can someone please help with this?
ok I got some clue with this.
This is a problem with Windows itself, for ex:
You open CMD
You are currently in directory C
then you run a command cd D:\data
even after this when you do dir, it will list out all the jobs in C directory only.
if you run D: after the above CD it will actually go into D:\data directory and you can work on that directory
I am sure you are past this since two (2) years ago. Here is a way to do it. I did not understand what you wanted the .zip archive filename to be. There is nothing in the source code presented that would produce %%i.
powershell -NoProfile -Command "Get-ChildItem -File -Path 'D:\Data\Generate' | Compress-Archive -DestinationPath 'D:\data\Upload\i.zip'"

Copy Selected Files to another folder via the Context Menu

I'm trying to copy selected items in a folder and paste them into a different folder via a script shortcut in the context menu.
This script is a shortcut to Powershell.exe that calls the .ps1 to execute. Technically, the script works, but it's only copying the file that is actually right-clicked instead of the group of selected files. In the screenshot, if I selected both "Saved Pictures" and "Screenshots", but right-clicked "Saved Pictures", it would only copy "Saved Pictures" even though "Screenshots" is also selected.
Function Collection{
#Selects the Item's Current Path
param($SourceFile)
#Copy the selected file to the Document Collection folder
Copy-Item $SourceFile -Destination (New-Item "$Env:UserName\My Documents\temp" -Type Directory -Force) -Recurse -Force
}
Collection $args[0]
I guess my main question for you guys is How do I copy all selected files instead of just the file that is clicked?
Hit the Windows and R key to open the Run dialog, type this and hit Enter :
shell:sendto
You can add a shortcut to your destination folder in the opened system folder to add it to the Send to list
This seems to fit what you want to achieve here.
Source : howtogeek

Mac OS10.6.8 - A way of deleting or removing files from a folder, using a list of file names?

I have a folder with 2508 files (jpg and pdf) in it on my drive. I have a list in a .txt file of about 1000 files which I want to remove from that folder - either by deleting to trash, or removing to another folder.
Is there a utility - or possibly commands I can put into the terminal - which I can use to do this, without manually moving files while looking through my list?
(Context: The list is a list of orphaned image files put out by Dreamweaver - which I want to remove from the images folder of a given site)..
Any help appreciated.
Thanks.
Put this in the terminal:
cat filename-of-list-with-files | xargs rm

Resources