Copy files based on existence of other files (Windows) - windows

I have a folder of images in jpg format called "finalpics" and also another folder ("sourcepics") which has several subfolders containing RAW files in various formats.
I need a script (batch file?) that will copy all the files from "sourcepics" and its subfolders to another folder ("sourcefinal") only if that file exists in "finalpics".
As an example:
"finalpics" contains files called mypic1.jpg, mypic2.jpg, mypic3.jpg.
"sourcepics" contains files called mypic1.dng, mypic2.psd, mypic3.cr2, yourpic1.dng, yourpic2.psd, yourpic3.cr2.
I'd want the script to copy the 'mypic' files but not the 'yourpic' files to "sourcefinal".
There's over a thousand jpgs in "finalpics" but probably 40,000 files in the various subfolders of "sourcepics".
Hope that makes sense.
Thanks for looking.

I think this PowerShell code will do what you're after; it will copy files of the same name (ignoring file extension) from "SourcePics" to "SourceFinal" if they exist in FinalPics:
# Define your folder locations:
$SourcePicsFolder = 'C:\SourcePics'
$FinalPicsFolder = 'C:\FinalPics'
$SourceFinalFolder = 'C:\SourceFinal'
# Get existing files into arrays:
$SourcePics = Get-ChildItem -Path $SourcePicsFolder -Recurse
$FinalPics = Get-ChildItem -Path $FinalPicsFolder -Recurse
# Loop all files in the source folder:
foreach($file in $SourcePics)
{
# Using the basename property (which ignores file extension), if the $FinalPics
# array contains a basename equal to the basename of $file, then copy it:
if($FinalPics.BaseName -contains $file.BaseName)
{
Copy-Item -Path $file.FullName -Destination $SourceFinalFolder
}
}
Note: There is no filtering based on file type (e.g. it will copy all files). Also, if your 'SourcePics' folder has two images of the same filename but in different subfolders, and a file of this name also exists in 'FinalPics', then you may get an error about file already existing when it tries to copy for the second time. To overwrite, use the -Force parameter on the Copy-Item command.
I tested the above code with some .dng files in 'SourcePics' and .jpg files in 'FinalPics' and it worked (ignoring the yourpic files).

Related

How can I convert part of a filename to become the file extension?

I downloaded a backup folder of about 3,000 files from our email service provider. None of the files have an associated filetype; instead the file extension was appended to the name of each individual file. For example:
community-involvement-photo-1-jpg
social-responsibility-31-2012-png
report-02-12-15-pdf
I can manually change the last dash to a period and the files work just fine. I'm wondering if there is a way to batch convert all of the files so they can be sorted and organized properly. I know in the Command Line I can do something like ren *. *.jpg but there are several different file types contained in the folder, so it wouldn't work for all of them. Is there any way I can tell it to convert the last "-" in each file name into a "." ?
I'm on Windows 10; unable to install any filename conversion programs unless I want to go through weeks of trouble with the IT group.
$ordner = "c:\temp\pseudodaten"
$Liste = (get-childitem -path $Ordner).Name
cd $ordner
foreach ($Datei in $Liste) {
$Length = $datei.length
$NeuerName=$Datei.Substring(0,$Length-4)+"."+$datei.Substring($Length - 3, 3)
rename-item -Path $Datei -NewName $NeuerName
}

Batch file to compress subdirectories individually with Windows native tools

I've seen variations of this question answered, but typically using something like 7zip. I'm trying to find a solution that will work with the capabilities that come with windows absent any additional tools.
I have a directory that contains several hundred subdirectories. I need to individually compress each subdirectory....so I'll wind up with several hundred zip files, one per subdirectory. This is on a machine at work where I don't have administrative privileges to install new software...hence the desire to stay away from 7zip, winRar, etc.
If this has already been answered elsewhere, my apologies...
Never tried that myself, but there is Compress-Archive:
The Compress-Archive cmdlet creates a zipped (or compressed) archive file from one or more specified files or folders. An archive file allows multiple files to be packaged, and optionally compressed, into a single zipped file for easier distribution and storage. An archive file can be compressed by using the compression algorithm specified by the CompressionLevel parameter.
Because Compress-Archive relies upon the Microsoft .NET Framework API System.IO.Compression.ZipArchive to compress files, the maximum file size that you can compress by using Compress-Archive is currently 2 GB. This is a limitation of the underlying API.
Here's a sample script I just hacked together:
# configure as needed
$source = "c:\temp"
$target = "d:\temp\test"
# grab source file names and list them
$files = gci $source -recurse
$files
# target exists?
if( -not (test-path $target)) {
new-item $target -type directory
}
# compress, I am using -force here to overwrite existing files
$files | foreach{
$dest = "$target\" + $_.name + ".zip"
compress-archive $_ $dest -CompressionLevel Optimal -force
}
# list target dir contents
gci $target -recurse
You may have to improve it a bit when it comes to subfolders. In the above version, subfolders are compressed as a whole into a single file. This might not exactly be what you want.
Get-ChildItem c:\path\of\your\folder | ForEach-Object {
$path = $_.FullName
Compress-Archive -Path $path -DestinationPath "$path.zip"
}
I put this, as a quick snippet. Don't hesitate to comment if this does not fit with your request.
In a folder X, there are subfolders Y1, Y2...
Y1.zip, Y2.zip... will be created.
use PowerShell go the the path that you would like to compress, do:
$folderlist = Get-ChildItem "."
foreach ($Folder in $folderlist) { Compress-Archive -path $Folder.Name -destinationPath "$($Folder.Name).zip"}

Script to compare two different folder contents and rename them based on minimum similarity

Story:
I have multiple folders with 1000+ files in each that are named similar to each other but are slightly different but they relate to the same content.
For example, in one folder I have files named quite simply "Jobs to do.doc" and in another folder "Jobs to do (UK) (Europe).doc" etc.
This is on Windows 10, not Linux.
Question:
Is there a script to compare each folder's content and rename them based on minimum similarity? So the end result would be to remove all the jargon and have each file in each folder (multiple) the same as one another but STILL remain in the retrospective folder?
*Basically compare multiple folder content to one folders contents and rename them so each file in each folder is named the same?
Example:
D:/Folder1/Name_Of_File1.jpeg
D:/Folder2/Name_Of_File1 (Europe).jpeg
D:/Folder3/Name_of_File1_(Random).jpeg
D:/folder1/another_file.doc
D:/Folder2/another_file_(date_month_year).txt
D:/Folder3/another_file(UK).XML
I have used different file extensions in the above example in hope someone can write a script to ignore file extensions.
I hope this make sense. So either a script to remove the content in brackets and keep the files integrity or rename ALL files across all folders based on minimum similarity.
The problem is its 1000+ files in each folder so want to run it as an automated job.
Thanks in advance.
If the stuff you want to get rid of is always in brackets then you could write a regex like
(.*?)([\s|_|]*\(.*\))
Try something like this
$folder = Get-ChildItem 'C:\TestFolder'
$regex = '(.*?)([\s|_|]*\(.*\))'
foreach ($file in $folder){
if ($file.BaseName -match $regex){
Rename-Item -Path $file.FullName -NewName "$($matches[1])$($file.extension)" -Verbose #-WhatIf
}
}
Regarding consistency you could run a precheck using same regex
#change each filename if it matches regex and store only it's new basename
$folder1 = get-childitem 'D:\T1' | foreach {if ($_.BaseName -match $regex){$matches[1]}else{$_.BaseName}}
$folder2 = get-childitem 'D:\T2' | foreach {if ($_.BaseName -match $regex){$matches[1]}else{$_.BaseName}}
#compare basenames in two folders - if all are the same nothing will be returned
Compare-Object $folder1 $folder2
Maybe you could build with that idea.

Extracting multiple files into their respective directories in windows

I'm posting this question after extensive searches did not yield a solution to my problem.
Here's the problem: I have a folder in windows, with multiple sub folders. Each of them has 1 or more compressed (rar) folders:
-Master_folder
sub_folder1
rarfolder1
Sub_folder2
rarfolder1
and so on
Is there a way to extract the folder that sub_folderX (where X varies from 1 to 300) contains, into sub_folderX itself, and so on for all other sub folders?.
All posts/solutions out there on extracting multiple files simultaneously (even using CLI) talk about extracting everything into a single location. I observed similar results when experimenting with the Winrar GUI options.
However, i don't want to put them in a single location since the extracted folders have the same name. Their location within their outer folder is what differentiates them.
If you are open to scripting, you can recursively iterate over the subfolders using command line winrar and some batch scripting.
#Root drive where rar files are located
$Directory = "T:\*"
$rar = Get-ChildItem -path $Directory -Recurse -Include *.rar
foreach($line in $rar){
$unradir = $line.Directory
$rarFileLocation = $line.VersionInfo.FileName
C:\"Program Files (x86)"\WinRAR\unrar.exe e -ro- $rarFileLocation $unradir
}

Script to search for files and rename files

I have about 11000 different files in hundreds different folders, sub folders and sub-sub folders in following location \\mastercorrespondence, and I need to rename some of the files and copy corresponding file from K:\CDS_TOOL_MANUAL_OVERRIDES daily in their own subfolder.
In short it should perform following steps
Look for any PDF format documents in K:\ CDS_TOOL_MANUAL_OVERRIDES folder.
For each document in K:\ CDS_TOOL_MANUAL_OVERRIDES look for PDF document with identical file name held in the \\mastercorrespondenceā€ any sub-directory.
If corresponding file found then rename file in \\mastercorrespondence sub-directory as <Original Filename>_<Overwritten>_<dd.mm.yy>
Move the file from K:\ CDS_TOOL_MANUAL_OVERRIDES folder to the same location as it counterpart in the \\10.5.13.10\mastercorrespondence sub-directory.
If any documents did not have a corresponding file in \\mastercorrespondence sub-directory then write a message to log file stating names of unmatched files.
Folder Structure is Like.
\\mastercorrespondence\SIPP\21\201201\01
\\mastercorrespondence\SIPP\21\2012022
\\mastercorrespondence\ISA\10201201\201202\02
\\mastercorrespondence\ISA\10201201\201203
\\mastercorrespondence\ISA\10201201\201204
\\mastercorrespondence\ISA\10201201\201205
Here's a starting point in PowerShell, if that works for you:
#Look for any PDF format documents in K:\ CDS_TOOL_MANUAL_OVERRIDES folder.
$pdfFiles = Get-ChildItem -Path K:\CDS_TOOL_MANUAL_OVERRIDES -filter "*.pdf"
#For each document in K:\ CDS_TOOL_MANUAL_OVERRIDES look for PDF document with identical file name held in the \\mastercorrespondenceā€ any sub-directory.
$referenceFiles = Get-ChildItem -Path \\mastercorrespondence -recurse -filter "*.pdf"
$pdfFiles | %{
$_ = $pdfFile
$matched = ""
#I don't fully understand what you're asking for, but it seems to me that the move and rename are on the same file, and technically a rename is a move... so I've combined them.
$referenceFiles | %{if ($_.Name -eq $pdfFile.Name) {$matched = $_} }
if ($matched -ne "") {
$destinationName = ($pdfFile.Name + "_OverWritten_"+(Get-Date -format dd.MM.yy))
Move-Item -Path $_.FullName -Destination ($matched.DirectoryName + "\" + $destinationName)
}
else{
#If any documents did not have a corresponding file in \\mastercorrespondence sub-directory then write a message to log file stating names of unmatched files.
("Unable to locate matching file: " + $pdfFile.FullName) | Out-File -Append -FilePath "K:\mover.log"
}
}

Resources