Move only New files to a different directory - Powershell - powershell

Running the following in powershell. Goal is to move files from folder 2 to folder 1, but only new files. Also would like to know which files were moved, sort like a log file if possible
Here is what i have so far :
$DownloadFolder = 'C:\FOLDER1'
$KeepFolder = 'C:\folder2'
$DownloadFiles = Get-ChildItem -Path $DownloadFolder
$KeepFiles = Get-ChildItem -Path $KeepFolder
$FileDiffs = Compare-Object -ReferenceObject $DownloadFiles -DifferenceObject $KeepFiles
$FileDiffs | foreach {
$copyParams = #{
'path' = $_.InputObject.Fullname
}
$Downloadll = $copyParams.path
if ($_.SideIndicator -eq '=>')
{
Copy-Item $Downloadll -Destination $KeepFolder -force
}
}
Trying to compare folder 1 and folder 2 and move ONLY new files to folder1. But getting the following error:
"" Cannot overwrite with the item filename.txt with itself

I would look into using the "robocopy" command.
It can move the newer files only, and it gives you the log you wanted.
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
https://serverfault.com/questions/129098/how-to-get-robocopy-running-in-powershell

There is a colon after $downloadfolder on the first line.
It's spelled wrong also.
You want <= not =>. Otherwise it tries to copy to the first folder.
Add -passthru to copy-item to get the output.

Related

Powershell: ForEach Copy-Item doesn't rename properly when retrieving data from array

I am pretty new to PowerShell and I need some help. I have a .bat file that I want to copy as many times as there are usernames in my array and then also rename at the same time. This is because the code in the .bat file remains the same, but for it to work on the client PC it has to have the username as a prefix in the filename.
This is the code that I have tried:
$usernames = Import-Csv C:\Users\Admin\Desktop\usernames.csv
$file = Get-ChildItem -Path 'C:\Users\Admin\Desktop\generatedbat\' -Recurse
foreach ($username in $usernames)
{
ForEach-Object {Copy-Item $file.FullName ('C:\Users\Admin\Desktop\generatedbat\' + $username + $File.BaseName + ".bat")}
}
This copies everything and it kind of works but I have one problem.
Instead of having this filename: JohnR-VPNNEW_up.bat
I get this: #{Username=JohnR}-VPNNEW_up.bat
Any help? Thanks!
So you have one .bat file C:\Users\Admin\Desktop\generatedbat\VPNNEW_up.bat you want to copy to the same directory with new names taken from the usernames.csv --> Username column.
Then try
# get an array of just the UserNames column in the csv file
$usernames = (Import-Csv -Path 'C:\Users\Admin\Desktop\usernames.csv').Username
# get the file as object so you can use its properties
$originalFile = Get-Item -Path 'C:\Users\Admin\Desktop\generatedbat\VPNNEW_up.bat'
foreach ($username in $usernames) {
$targetFile = Join-Path -Path $originalFile.DirectoryName -ChildPath ('{0}-{1}' -f $username, $originalFile.Name)
$originalFile | Copy-Item -Destination $targetFile -WhatIf
}
I have added switch -WhatIf so you can first test this out. If what is displayed in the console window looks OK, then remove that -WhatIf safety switch and run the code again so the file is actually copied
I kept the code the same but instead of using a .csv file I just used a .txt file and it worked perfectly.

Powershell dropping characters while creating folder names

I am having a strange problem in Powershell (Version 2021.8.0) while creating folders and naming them. I start with a number of individual ebook files in a folder that I set using Set-Location. I use the file name minus the extension to create a new folder with the same name as the e-book file. The code works fine the majority of the time with various file extensions I have stored in an array beginning of the code.
What's happening is that the code creates the proper folder name the majority of the time and moves the source file into the folder after it's created.
The problem is, if the last letter of the source file name, on files with the extension ".epub" end in an "e", then the "e" is missing from the end of the created folder name. I thought that I saw it also drop "r" and "p" but I have been unable to replicate that error recently.
Below is my code. It is set up to run against file extensions for e-books and audiobooks. Please ignore the error messages that are being generated when files of a specific type don't exist in the working folder. I am just using the array for testing and it will be filled automatically later by reading the folder contents.
This Code Creates a Folder for Each File and moves the file into that Folder:
Clear-Host
$SourceFileFolder = 'N:\- Books\- - BMS\- Books Needing Folders'
Set-Location $SourceFileFolder
$MyArray = ( "*.azw3", "*.cbz", "*.doc", "*.docx", "*.djvu", "*.epub", "*.mobi", "*.mp3", "*.pdf", "*.txt" )
Foreach ($FileExtension in $MyArray) {
Get-ChildItem -Include $FileExtension -Name -Recurse | Sort-Object | ForEach-Object { $SourceFileName = $_
$NewDirectoryName = $SourceFileName.TrimEnd($FileExtension)
New-Item -Name $NewDirectoryName -ItemType "directory"
$OriginalFileName = Join-Path -Path $SourceFileFolder -ChildPath $SourceFileName
$DestinationFilename = Join-Path -Path $NewDirectoryName -ChildPath $SourceFileName
$DestinationFilename = Join-Path -Path $SourceFileFolder -ChildPath $DestinationFilename
Move-Item $OriginalFileName -Destination $DestinationFilename
}
}
Thanks for any help you can give. Driving me nuts and I am pretty sure it's something that I am doing wrong, like always.
String.TrimEnd()
Removes all the trailing occurrences of a set of characters specified in an array from the current string.
TrimEnd method will remove all characters that matches in the character array you provided. It does not look for whether or not .epub is at the end of the string, but rather it trims out any of the characters in the argument supplied from the end of the string. In your case, all dots,e,p,u,b will be removed from the end until no more of these characters are within the string. Now, you will eventually (and you do) remove more than what you intended for.
I'd suggest using EndsWith to match your extensions and performing a substring selection instead, as below. If you deal only with single extension (eg: not with .tar.gz or other double extensions type), you can also use the .net [System.IO.Path]::GetFileNameWithoutExtension($MyFileName) method.
$MyFileName = "Teste.epub"
$FileExt = '.epub'
# Wrong approach
$output = $MyFileName.TrimEnd($FileExt)
write-host $output -ForegroundColor Yellow
#Output returns Test
# Proper method
if ($MyFileName.EndsWith($FileExt)) {
$output = $MyFileName.Substring(0,$MyFileName.Length - $FileExt.Length)
Write-Host $output -ForegroundColor Cyan
}
# Returns Tested
#Alternative method. Won't work if you want to trim out double extensions (eg. tar.gz)
if ($MyFileName.EndsWith($FileExt)) {
$Output = [System.IO.Path]::GetFileNameWithoutExtension($MyFileName)
Write-Host $output -ForegroundColor Cyan
}
You're making this too hard on yourself. Use the .BaseName to get the filename without extension.
Your code simplified:
$SourceFileFolder = 'N:\- Books\- - BMS\- Books Needing Folders'
$MyArray = "*.azw3", "*.cbz", "*.doc", "*.docx", "*.djvu", "*.epub", "*.mobi", "*.mp3", "*.pdf", "*.txt"
(Get-ChildItem -Path $SourceFileFolder -Include $MyArray -File -Recurse) | Sort-Object Name | ForEach-Object {
# BaseName is the filename without extension
$NewDirectory = Join-Path -Path $SourceFileFolder -ChildPath $_.BaseName
$null = New-Item -Path $NewDirectory -ItemType Directory -Force
$_ | Move-Item -Destination $NewDirectory
}

Get-ChildItem can't find one specific directory?

I am writing a PowerShell script which is supposed to take a look at a directory, sort the children by name (numbered names) and check if there's a specific file inside. If there is, it's supposed to be copied somewhere, if not it's supposed to look at the next one. This is my code so far:
#[...]
$notExist = $true
#$LatestCiClient = [some directory from earlier in the script]
$buildDirectories = Get-ChildItem -Path $LatestCiClient | Sort Name -Descending
while ($notExist) {
$currentDir = $buildDirectories | Select-Object -First 1
$assembliesDir = Get-ChildItem -Path $currentDir.FullName -Include Desktop-Assemblies #Breakpoint
$script:exe = Get-ChildItem -Path $assembliesDir -Include SomeFile.exe | Select -First 1
if ($Script:exe.Exists) {
$notExist = $false
} else {
if ($buildDirectories.Count -gt 0) {
$buildDirectories = $buildDirectories | Select -Skip 1
} else {
$script:NoneFound = $true
$script:notExist = $false
}
}
}
#[more Powershell that is supposed to copy $script:exe]
I am getting the numbered directories ($buildDirectories), in the debugger I see the whole list.
I enter the while block (breakpoint is set at "#Breakpoint"), select the first directory to check ($currentDir`, which is again there and correct) and look for a folder called "Desktop-Assemblies".
I am looking at it, in the Explorer, right now, the folder is there, it's filled, it's not hidden (it is read-only but that shouldn't matter?), I am not doing anything I haven't done several times in the script - nothing. $assembliesDir is empty.
I tried using "Desktop-Assemblies", I tried Desktop*, I tried without the -Include . If I use $currentDir it says
Cannot find path [WeirdPath] because it doesn't exist
Weird Path being the FolderName of my $currentDir but the rest of the path is C:\Users\MyUserName.
If I use $currentDir.FullName it finds all the directories, INCLUDING the one I am looking for. I should probably add that the whole directory that is being searched is on another computer, it's a network drive.
Did you use this?
Get-ChildItem -Path $currentDir.FullName | Where-Object {$_.Name -eq 'Desktop-Assemblies'}

Script help rename - copy - move

I am trying to write a PowerShell script to do the following.
Rename files in source (FTP folders) directories with it's "current_name_datetime.csv" as per a source file "Source_list.csv" this file has the directories "source,destination" I want this script to look into.
Copy newly renamed files to backup directories as per destination in Source_list.csv this file has the directories "source,destination" I want this script to look into.
Move newly renamed files to final destination directory which is not in my current script.
Source_list.csv contents
cscenter,Costume_Supercenter
fkimports,FKImports
My Script:
$sdfiles = Get-Content c:\!tony\Source_list.csv
$sourceDir = "c:\test\"
$destinationDir = "c:\testing\"
Get-ChildItem $sourceDir -Recurse -Include $sdfiles "*.csv"|
ForEach-Object{
$newname= "{0}{1}_{2}.csv" -f $destinationDir, $_.BaseName, [datetime]::Now.ToString('MM-dd-yyyy-hh-mm-ss')
$_|Copy-Item -Include ,$sdfiles -Destination $newname -whatif }
Error:
What if: Performing operation "Copy Directory" on Target "Item: C:\test\cscenter Destination: C:\testing\cscenter_10-01-2015-12-22-24.csv".
I see in the error that it is trying to copy the directory not the single file in each directory and creating a new folder using the original folder name and renaming the folder and appending the date/time stamp.
Confused. The -Include parameter should only be accepting a single array of strings, throwing "*.csv" on to the end of it won't work AFAIK. Additionally It will be interpreting the whole line of the CSV, ie searching for the file "cscenter,Costume_Supercenter" so shouldn't be returning anything. At least that's what I see when I replicate this on my computer.
Lastly you've tried to filter the files, piped that to Copy-Item and tried to filter it again?
I'd take a more straightforward approach:
$sdfiles = Import-CSV c:\!tony\Source_list.csv -Header #("File", "Folder")
$sourcedir = "c:\test\"
$destinationdir = "c:\testing\"
$sdfiles | ForEach-Object {
$path = $sourcedir + $_.File + ".csv"
$folder = $destinationdir + $_.Folder + '\'
if (!(Test-Path $folder)) { New-Item -Type Directory -Path $folder }
if (Test-Path ($path))
{
$newname = "{0}{1}_{2}.csv" -f $folder, $_.File, (Get-Date).ToString('MM-dd-yyyy-hh-mm-ss')
Copy-Item -Path $path -Destination $newname -whatif
}
else { Write-Error "File $($_.File) not found" }
}
It's a bit chunkier but much easier to read and tweak to your liking. Note that Import-CSV does require PowerShell v3. Let me know if you've got v2 and need help tweaking it for a two-dimensional array.
I also recommend looking into Microsoft's MVA courses on PowerShell, they are excellent resources for starting out.

How do I select files in a folder based on part of filename and zip them in Powershell?

I'm fairly new to Powershell(using Powershell 2.0 btw) and am trying to make a script that does several things(this is my 3rd script or so). I have most things in place but the last thing remaining is to group files of different types (xml, tfw and tif) in a folder, based on the first part of the filename(first three characters) and then zip these files into several zip-files with name like the first 3 characters, either in the same location or in a new one.
Sample of folder content:
001.tif
001.tfw
001.metadata.xml
002.tif
002.tfw
002.metadata.xml
003.tif
003.tfw
003.metadata.xml
003_svel.tif
003_svel.tfw
003_svel.metadata.xml
Wanted result:
001.zip containing 001.tif, 001.tfw, 001.metadata.xml
002.zip containing 002.tif, 002.tfw, 002.metadata.xml
003.zip containing 003.tif, 003.tfw, 003.metadata.xml, 003_svel.tif,
003_svel.tfw and 003_svel.metadata.xml
I have installed 7-zip to do the zipping and am using the commandline version. I've used 7-zip local on some testfiles and got it to work, but then it was only tif-files. I have a source folder where I search for the latest created folder and then process the files in it.
This is what I have so far(Powershell 2.0):
$dir_source = "c:\Test"
$new_folder = Get-ChildItem $dir_source -Recurse |
Where { $_.PSIsContainer} |
Sort-Object LastWriteTime -Descending |
Select-Object -ExpandProperty Fullname <-First 1
Get-ChildItem $new_folder -recurse -Exclude metafile.xml |
Group-Object {$_.Name.Substring(0,3)}
This gives me a list of grouped files in the lates created folder based on the first 3 characters in the filename. It also show what files are in each group.
Like below:
Count Name Group
----- ---- -----
3 003 {C:\Test\20150708 063255_B\003.metafile.xml, C:\Test\20150708 063255_B\003.tfw, C:\Test\20150708 063255_B\003.tif}
6 004 {C:\Test\20150708 063255_B\004.metafile.xml, C:\Test\20150708 063255_B\004.tfw, C:\Test\20150708 063255_B\004.tif,C:\Test...
6 009 {C:\Test\20150708 063255_B\009.metafile.xml, C:\Test\20150708 063255_B\009.tfw, C:\Test\20150708 063255_B\009.tif,C:\Test...
Now my next step ist to take these groups and zip them. Ideally create these zip-files in a different destination directory (I believe I can change this when setting the $directory- variable in the script below.)
foreach ($group in $dataset) {
$name = $file.name
$directory = $file.DirectoryName
$zipFile = $file.Name + ".zip"
sz a -t7z "$directory\$zipfile" "$directory\$name"
This last code is causing some trouble. I either get the message:
7-Zip (A) 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 Error:
c:\Test\Dest_test460.zip is not supported archive System error:
Incorrect function.
,or
WARNING: Cannot find 1 file 7-Zip (A) 9.20 Copyright (c) 1999-2010
Igor Pavlov 2010-11-18 Scanning \460: WARNING: The system cannot
find the file specified.
,or it starts zipping all files on my userprofile into a zip-file. Depending on changes I do to the $group-value. I believe there are one ore more basic errors in my script causing this, and this is where I'm asking for some help. It may be that I am approaching this the wrong way by first grouping the files I want and then try to zip them?
Anyone that can see my error or give me some hint to what I have to do?
Thanks for your time!
Lee Holmes New-ZipFile do the job, he has two versions one of them using the ICSharpCode.SharpZipLib.dll to compress, and the other not require it, i wrapped the 2nd one into a function:
Function New-ZipFile {
param(
## The name of the zip archive to create
$Path = $(throw "Specify a zip file name"),
## Switch to delete the zip archive if it already exists.
[Switch] $Force
)
Set-StrictMode -Version 3
## Create the Zip File
$zipName = $executionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
## Check if the file exists already. If it does, check
## for -Force - generate an error if not specified.
if(Test-Path $zipName)
{
if($Force)
{
Remove-Item $zipName -Force
}
else
{
throw "Item with specified name $zipName already exists."
}
}
## Add the DLL that helps with file compression
Add-Type -Assembly System.IO.Compression.FileSystem
try
{
## Open the Zip archive
$archive = [System.IO.Compression.ZipFile]::Open($zipName, "Create")
## Go through each file in the input, adding it to the Zip file
## specified
foreach($file in $input)
{
## Skip the current file if it is the zip file itself
if($file.FullName -eq $zipName)
{
continue
}
## Skip directories
if($file.PSIsContainer)
{
continue
}
$item = $file | Get-Item
$null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
$archive, $item.FullName, $item.Name)
}
}
finally
{
## Close the file
$archive.Dispose()
$archive = $null
}
}
To use it for example:
dir c:\folder -Recurse | New-ZipFile -Path c:\temp\folder.zip
The Source file(for the one that use the ICSharpCode): http://poshcode.org/2202
Use my Previous Answer Function New-ZipFile and use with this one:
$FolderName = "C:\temp"
$Files = dir $FolderName
$prfx = #()
foreach ($file in $files)
{
$prfx += $file.Name.Substring(0,3)
}
$prfx = $prfx | Group
foreach ($Prf in $prfx)
{
$prf = $prf.name.ToString()
dir $Files | ? {$_.Name -match "^$prf"} | New-ZipFile -Path "$foldername\$prf.zip"
}
According to your example It will output 3 zip files like you want,
it will always use the first 3 letters of the file, you can the change this in this line $prfx += $file.Name.Substring(0,3) and set it different if needed.
Good Luck
Could not get the suggested solution to work, had problems configuring ICSharpCode. I also wanted to use 7zip, since it is still under some updating regime.
Ended up copying my files to temp folders based on the filenames and then zip each folder. After that delete the tempfolders with files. Ugly code, but it does the job.
# Create folder based on filename and copy files into respective folder
Get-ChildItem $new_folder -Filter *.* | Where-Object {!$_.PSIsContainer} | Foreach-Object{
$dest = Join-Path $_.DirectoryName $_.Name.SubString(0,3)
if(!(Test-Path -Path $dest -PathType Container))
{
$null = md $dest
}
$_ | Copy-Item -Destination $dest -Force
}
# Create zip-file of each folder
dir $new_folder | Where-Object { $_.PSIsContainer } | ForEach-Object { sz a -t7z -mx9 "$dir_dest\$_.zip" $_.FullName }
# Delete temp-folders
dir $new_folder | Where-Object { $_.PSIsContainer } | Remove-Item -Recurse