I have a source folder with 10-80 files jpg images
I need to copy them into a destination folder with subfolders and replace each existing files with its version from the source folder BUT only when such file is already present there
If there is not such files there do not place a copy
Can I do that with batch or shell? And what is better and faster?
PowerShell can make quick work of this. Assuming the photos have the same name in the source and destination, this should work.
# Get the source photos
$SourcePhotos = Get-ChildItem -Path '.\Source'
# Loop through each of our source photos
foreach ($SourcePhoto in $SourcePhotos) {
# Recuresively search the destination for the photo from the source
$DestinationPhoto = Get-ChildItem -Path '.\Destination' -Filter $SourcePhoto.Name -Recurse
# If we find the photo in the destination...
if ($DestinationPhoto) {
# Copy from source to destination forcefully to overwrite the one at the destination
Copy-Item $SourcePhoto.FullName $DestinationPhoto.FullName -Force
}
}
Related
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
I have the below 5 folders on the desktop.
Each of these folders has a sub-folder word and the word sub-folder contains file document.xml. For example, This is the path of the file
(C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document\Australian Citizenship (Transitionals and Consequentials) Act 2007\word\document.xml).
I would like to move the document.xml out of the sub-folder word to the parent folder "Australian Citizenship (Transitionals and Consequentials) Act 2007" and rename it as "Australian Citizenship (Transitionals and Consequentials) Act 2007".
I want to do the same process for all the document.xml files in the rest of the four folders.
Please advise me if there is a way to do this with power shell or batch script.
Thanks,
Venkat
I believe this will do your job. Give it a try.
$Root = "C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document"
$Folders = Get-ChildItem -Path $Root
Foreach($Fld in $Folders)
{
If(Test-Path "$($Fld.FullName)\word\document.xml")
{
# Move the file document.xml and rename it
Move-Item -Path "$($Fld.FullName)\word\document.xml" -Destination "$($Fld.FullName)\$($Fld.Name).xml"
#Deletes Word folder
Remove-Item "$($Fld.FullName)\word"
}
}
I wanted to update an app.
In order to do this, i want to test if files AND folders from $path_new exists in $path_old. If so, delete those from $path_old and copy those from $path_new, if not, keep them on $path_old.
So, i'll keep everything except updated files.
Edit : Using PowerShell.
Edit : So if an user added a personnal folder in the app mmain folder, he'll keep it.
Maybe i'll look like this :
$FileName = "C:\Share\test.txt"
if (Test-Path $FileName)
{
Remove-Item $FileName
}
But it's not exactly what i want. I think i'll have to add a "for" loop, but how, where ?
This will copy files from the source directory to the target directory. 'Personal files', or files not existant in the source directory, will be ignored while files that exist in both folders are overwritten.
In addition to that, I'd like to point out that your approach to this problem is inefficient so to speak. It's a lot more work to search for files existing in both directories, deleting those, and then copying those over from the new directory to the old directory. Instead you should just use existing file copy actions such as Copy-Item as they have built-in functionality to overwrite existing files.
$target = ".\old"
$source = ".\new"
Get-ChildItem -Path $source | Copy-Item -Destination $target -Recurse -Force -Container
Parameters
-Recurse Copy all files and folders recursively.
-Force Copy items that cannot otherwise be changed, such as copying over a read-only file or alias.
-Container This will retain the folder structure when doing recursive copy actions.
I have a folder with thousands of files (let's say .txt) with different names:
VBH_V001.txt
VDD_V001.txt
DTG_V001.txt
ADC_V001.txt
DFD_V001.txt
etc....
I need to create directories in that folder with the name of each file and then move those files to directories. The result should be for example Folder with the the name VBH (without _V001.txt) and in that folder I should have VBH_V001.txt file. How can I do that. Please advise.
cd <path to your folder>
$files = Get-ChildItem -file;
ForEach ($file in $files)
{
$folder = New-Item -type directory -name $file.BaseName;
Move-Item $file.FullName $folder.FullName;
}
This script creates a directory for each file and moves the file to this directory. To exclude _V001 from the directory name, you can call the TrimEnd method on $file.BaseName -
$file.BaseName.TrimEnd ("_V001")
Step by step.
First of all, go to the directory that contains your files.
Get a list of objects that represent your files by using the Get-ChildItem cmdlet with the -file attribute. These objects contain properties - such as BaseName, FullName, and so on.
Save this list to the $files variable.
Loop through the list with ForEach.
In the ForEach body, create a directory for each file with the New-Item cmdlet. The -type attribute specifies the type for the new item (-type directory), for the -name attribute substitute the $file.BaseName property. BaseName property returns a string with the name of the file without the extension and path.
Save the object of the newly created directory into the $folder variable.
Move the file using the Move-Item cmdlet. This cmdlet requires two attributes: source path and destination path. As the source path, use the FullName property of the file object, and the FullName property of the directory object for the destination path. FullName property returns the name of the file or directory that contains the full path, for example D:\directory\file.txt for a file and D:\directory\anotherDirectory for a directory.
It's not a big deal, actually, and without shortcuts it looks like a plain English.
What you basically need to do, is to:
Get a list of files in a selected folder through PowerShell.
Create new folders in a loop by
using the New-Item cmdlet which have name made by using substring of a name of a selected file.
For each of the files, move the file to the new location, using the Move-Item cmdlet.
Hope that helps.
I'm trying to feed the results of a Get-ChildItem call through io.compression.zipfile to create a zip file of "E:\Applications_Server_Test", excluding two folders "BACKUP" and "BACKUP2".
However, Powershell seems to be interpreting this as "$items = a string of directories and file names" instead of a recursive collection of directories and files I want to zip. I can find tutorials on using Get-ChildItem to exclude directories and I can find tutorials on how to zip a full directory or zip multiple directories but I can't find anything on zipping directories with exclusions. Can somebody tell me where I'm going wrong?
$source = "E:\Applications_Server_Test"
$destination = "E:\AST_Dump.zip"
$items = Get-ChildItem $source -Recurse | ?{ $_.fullname -notmatch "\\backup\\?" }
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($items, $destination)
Thanks!
At the moment you are trying to archive the object $items, not the folder. Unfortunately this is not gonna work.
Try to move the backup folders at the same drive (this will just change records at the drive and not move any data), then to archive the whole "E:\Applications_Server_Test" folder and to move back the backup folders.
Other option is to use ZipArchive.CreateEntry method and to add file by file in to the archive. But this is not so elegant ;)