How can I copy the folder names to the filenames during the copy. At the moment the script copies only files from the subdirectory without all folder and sub-foldername. However, I want which takes the absolute pathname while copying.
Here an example:
Folder1
Subfolder1
Subfolder2
Subfolder3
Subfolder4
File1
Folder2
Then a copied file would be called so:
Folder1_Subfolder1_Subfolder2_Subfolder3_Subfolder4_File.pdf
Powershell code:
Get-ChildItem –path C:\12 -Recurse -Include "*.???" |
Foreach-Object { copy-item -Path $_.fullname -Destination c:\12 }
Ok, if I'm understanding you correctly, you want a file like:
C:\12\foo\bar\baz.txt
To be copied to:
C:\12\foo_bar_baz.txt
If I'm understanding that correctly, then something like the following should work:
function Copy-FileUsePath {
[cmdletbinding()]
param(
[parameter(Mandatory=$true,Position=0)][string]$Path,
[parameter(Mandatory=$true,Position=1)][string]$Destination
)
$files = Get-ChildItem -Path $Path -Recurse -Include "*.???"
foreach ( $file in $files ) {
$discard = $Destination.Length
if( $Destination -notlike "*\" ) {
$discard++
}
$newFileName = $file.FullName.Replace('\', '_').Remove( 0, $discard )
$newFile = $Destination + '\' + $newFileName
Write-Verbose "Copy-Item -Path $file.fullname -Destination $newFile"
Copy-Item -Path $file.fullname -Destination $newFile
}
}
To use this, save it to a file (I called it Copy-FileUsePath.ps1), and you can do the following:
. .\Copy-FileUsePath.ps1
Copy-FileUsePath -Path C:\12 -Destination C:\export
Related
I'm trying to create an array that will remove files from the destination path and then copy from the source path to the destination path. I've created a .txt document on the build server with a list of files with their relative path. When I run the below block of code it's removing all contents in folder B and copy Folder A(without any contents) to Folder B.
This is what I'm running
$files = get-content "C:\files.txt"
foreach ($filepath in $files)
{
$source = '\\Server1\Folder A' + $filepath
$dest = '\\Server2\Folder B' + $filepath
foreach ($removefile in $dest)
{
rd $removefile -recurse -force
}
foreach ($addfile in $source)
{
cp $addfile -destination $dest
}
}
Soda,
I've tried your suggestion but it's trying to remove from/copy to the incorrect directory.
Code:
$targetList = Get-Content "C:\MCSfiles.txt"
foreach ($target in $targetList) {
$destPath = Join-Path "\\Server2\MCSWebTest" $target
$destFiles = Get-ChildItem $destPath
foreach ($file in $destFiles) {
Remove-Item $file -Recurse -Force
}
$sourcePath = Join-Path "\\Server1\WebSites\McsWeb2" $target
$sourceFiles = Get-ChildItem $sourcePath
foreach ($file in $sourceFiles) {
Copy-Item $file -Destination $destPath
}
}
Error:
Remove-Item : Cannot find path 'C:\Program Files (x86)\Jenkins\jobs\MCSTest\workspace\App_Code' because it does not exist.
At C:\Users\SVC-VI~1\AppData\Local\Temp\jenkins5893875881898738781.ps1:9 >char:1
9
+ Remove-Item <<<< $file -Recurse -Force
+ CategoryInfo : ObjectNotFound: (C:\Program >File...kspace\App_Co
de:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov
eItemCommand
Copy-Item : Cannot find path 'C:\Program Files (x86)\Jenkins\jobs\MCSTest\works
pace\App_Code' because it does not exist.
At C:\Users\SVC-VI~1\AppData\Local\Temp\jenkins5893875881898738781.ps1:16 >char:
18
+ Copy-Item <<<< $file -Destination $destPath
+ CategoryInfo : ObjectNotFound: (C:\Program >File...kspace\App_Co
de:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyI
temCommand
Soda,
Neither of the suggestions work. It's still removing everything in the destination directory and adding the source directory folder to the destination directory without files. I'm a little lost here.
you are missing backslashes in your paths, use Join-Path to build your strings to avoid that (or maybe leading backslashes are included in files.txt)
you iterate on $dest and $source but these are strings, not file collections, which you should be able to retrieve with Get-ChildItem $dest and Get-ChildItem $source, for instance
Also, for readability, you should not use aliases in scripts (rd and cp)
PS: I believe your script produces errors, you should include them in your question (you can edit it)
EDIT regarding comments:
Try this (untested, remove -WhatIf's to process):
$targetList = Get-Content "D:\files.txt"
foreach ($target in $targetList) {
$destPath = Join-Path "D:\destination" $target
Remove-Item $destPath -Recurse -Force -WhatIf
$sourcePath = Join-Path "D:\source" $target
Copy-Item $sourcePath -Destination $destPath -Recurse -WhatIf
}
EDIT2: I have corrected and simplified the code above, my logic was slightly off.
This could even be simpler, and better, to group remove statements and run them before the copy statements, like:
$targetList = Get-Content "D:\files.txt"
#remove all
$targetList | ForEach-Object {
Remove-Item (Join-Path "D:\destination" $_) -Recurse -Force -WhatIf
}
#copy all
$targetList | ForEach-Object {
Copy-Item (Join-Path "D:\source" $_) (Join-Path "D:\destination" $_) -Recurse -Force -WhatIf
}
Both snippets have been tested with a sample folder structure.
Just for the sake of completeness, the error you got with my first attempt was due to passing the $file objects to the processing cmdlets, instead of full paths ($file.FullName).
The issue is you are trying to loop too many times. The outer loop processes the files one at a time. There should be no inner loop. Also, you aren't validating the file exists before issuing a deletion.
$files = Get-Content "C:\test.txt"
$source_base="C:\TEMP\source\"
$dest_base="C:\TEMP\dest\"
foreach ($filepath in $files)
{
$source = $source_base + $filepath
$dest = $dest_base + $filepath
if(Test-Path $dest) {rd $dest -recurse -force}
cp $source -destination $dest
}
so, trying to rename any file found in the current script directory and all subfolders. Doesn't want to work. This is what I have so far...what am I doing wrong? The issue I'm having is that it is not renaming files in the subfolders.
<# Renames all files in the working directory to have an extension of .hd #>
$files = Get-ChildItem $PSScriptRoot -Recurse | Where-Object {$_.Extension -match ".jpg|.jpeg|.png|.bmp|.gif|.3gp|.mp4|.webm|.mkv"}
ForEach ($file in $files) {
$filenew = $file.Name + ".hd"
Rename-Item $file $filenew
}
you are passing a filesystem object to path parameter of rename-item instead of a string path.
change this :
Rename-Item $file $filenew
to this:
Rename-Item -Path $file.fullname -NewName $filenew
You could also simplify your script to this:
Get-ChildItem -Path $Path -Include *.jpg,*.png,*.mp4 -Recurse |
Rename-Item -NewName { $_.Name + '.hd' } -WhatIf
Note: remove -whatif to apply the rename action
Anyway, so $file.Name contains the extension so you need $file.Basename
$files = Get-ChildItem $PSScriptRoot -Recurse | Where-Object {$_.Extension -match ".jpg|.jpeg|.png|.bmp|.gif|.3gp|.mp4|.webm|.mkv"}
ForEach ($file in $files) {
$filenew = $file.BaseName + ".hd"
Rename-Item $file $filenew
}
I am working on folders that contain many types and sizes of files within them. What I want to do is move files that are not contained in one folder into a new folder. I have embedded a picture link that helps illustrate what I am aiming to do.
I would like test123.pdf to be moved to a new location because it's not contained within the other folder. Below I have some code that simply compares the contents of each folder and outputs which file is out of place. I have been researching some things online, but have come up empty. Can anyone help me proceed?
Disclaimer: I know the path is wrong, but I can't show it for security reasons.
$Folder1 = Get-ChildItem -Recurse -path "Enter Path here"
$Folder2 = Get-ChildItem -Recurse -path "Enter the Path here"
Compare-Object -ReferenceObject $Folder1 -DifferenceObject $Folder2
It sounds like you want to compare the contents of two folders, ID the file(s) that are not present in both folders, and then move them to a third folder. To accomplish this, you can define your 2 paths in variables, compare the contents of the folders, grab the full path names of the different items, and then move them to the new destination.
$path1 = yourfirstpath
$path2 = yoursecondpathforcomparing
$path3 = yourdestinationpath
diff (ls $path1 -recurse) (ls $path2 -recurse) | ForEach {$_.InputObject.FullName} | Move-Item -Destination $path3
diff = Compare-Object , ls = Get-ChildItem
This will do the work, any file that is not in both Folder1 & Folder2 will be moved to Folder 3
$Folder1 = 'C:\folder1'
$Folder2 = 'C:\folder2'
$Folder3 = 'C:\folder3'
foreach ($file in Get-ChildItem -Recurse $Folder1)
{
if (-not(Test-Path "$Folder2\$file"))
{
Move-Item $file.FullName $Folder3
}
}
This will compare by file name, and maintain the directory structure the extra files were found in, and handle the recursion properly. The output of the code demonstrates the directory structure I tested with.
# be explict here to ensure everything is lowercase
# so when this is cut and pasted, it does not break
# when you enter the real path
$d1 = "d:\test\one".ToLower()
$d2 = "d:\test\two".ToLower()
$f1 = gci -Recurse $d1 -File | % {$_.FullName.ToLower()}
$f2 = gci -Recurse $d2 -File | % {$_.FullName.ToLower()}
"f1"
$f1
"`nf2"
$f2
$same = #()
$extra = #()
foreach ($f in $f2)
{
$f2tof1path = $f.Replace($d2, $d1)
if ($f1.Contains($f2tof1path) -eq $false)
{
$extra += $f
}
else {
$same += $f
}
}
"`nSame"
$same
"`nExtra"
$extra
"`nMOVE"
$folder3 = "d:\test\three"
foreach ($f in $extra)
{
# move files somewhere, create dir if not exists
$dest = $f.Replace($d2,$folder3)
$destdir = $(Split-Path -Parent -Path $dest)
if (!(Test-Path $destdir))
{
# remove quotes to do new-item, keep them to show what it will do
"New-Item -ItemType Directory -Force -Path $destdir"
}
# remove quotes to do move-item, keep them to show what it will do
"Move-Item $f $dest"
}
Output
f1
d:\test\one\1.txt
d:\test\one\2.txt
d:\test\one\sub\1.txt
d:\test\one\sub\2.txt
f2
d:\test\two\1.txt
d:\test\two\2.txt
d:\test\two\3.txt
d:\test\two\sub\1.txt
d:\test\two\sub\2.txt
d:\test\two\sub\3.txt
Same
d:\test\two\1.txt
d:\test\two\2.txt
d:\test\two\sub\1.txt
d:\test\two\sub\2.txt
Extra
d:\test\two\3.txt
d:\test\two\sub\3.txt
MOVE
New-Item -ItemType Directory -Force -Path d:\test\three
Move-Item d:\test\two\3.txt d:\test\three\3.txt
New-Item -ItemType Directory -Force -Path d:\test\three\sub
Move-Item d:\test\two\sub\3.txt d:\test\three\sub\3.txt
If this does not solve your problem, it should get you 99% there. Play around with the code, have fun, and good luck.
I have a root directory that consists of many folders and sub folders. I need to check whether a particular file like *.sln or *.designer.vb exists in the folders or subfolders and output the result in a text file.
For Eg:
$root = "C:\Root\"
$FileType = ".sln",".designer.vb"
the text file will have result somewhat like below:
.sln ---> 2 files
.sln files path ---->
c:\Root\Application1\subfolder1\Test.sln
c:\Root\Application2\subfolder1\Test2.sln
Any help will be highly appreciated!
Regards,
Ashish
Try this:
function Get-ExtensionCount {
param(
$Root = "C:\Root\",
$FileType = #(".sln", ".designer.vb"),
$Outfile = "C:\Root\rootext.txt"
)
$output = #()
Foreach ($type in $FileType) {
$files = Get-ChildItem $Root -Filter *$type -Recurse | ? { !$_.PSIsContainer }
$output += "$type ---> $($files.Count) files"
foreach ($file in $files) {
$output += $file.FullName
}
}
$output | Set-Content $Outfile
}
I turned it into a function with your values as default parameter-values. Call it by using
Get-ExtensionCount #for default values
Or
Get-ExtensionCount -Root "d:\test" -FileType ".txt", ".bmp" -Outfile "D:\output.txt"
Output saved to the file ex:
.txt ---> 3 files
D:\Test\as.txt
D:\Test\ddddd.txt
D:\Test\sss.txt
.bmp ---> 2 files
D:\Test\dsadsa.bmp
D:\Test\New Bitmap Image.bmp
To get the all the filecounts at the start, try:
function Get-ExtensionCount {
param(
$Root = "C:\Root\",
$FileType = #(".sln", ".designer.vb"),
$Outfile = "C:\Root\rootext.txt"
)
#Filecount per type
$header = #()
#All the filepaths
$filelist = #()
Foreach ($type in $FileType) {
$files = Get-ChildItem $Root -Filter *$type -Recurse | ? { !$_.PSIsContainer }
$header += "$type ---> $($files.Count) files"
foreach ($file in $files) {
$filelist += $file.FullName
}
}
#Collect to single output
$output = #($header, $filelist)
$output | Set-Content $Outfile
}
Here's a one-liner to determine if at least one file with extension .txt or .ps1 exists in the directory $OutputPath:
(Get-ChildItem -Path $OutputPath -force | Where-Object Extension -in ('.txt','.ps1') | Measure-Object).Count
Explanation: the command tells you the number of files in the specified directory matching any of the listed extensions. You can append -ne 0 to the end, which returns true or false to be used in an if block.
This will search the directory $root and its subdirectories for files of type $FileType, including hidden files and excluding directories:
$root = "C:\Root\";
$FileType = "*.sln", "*.designer.vb";
$results = Get-ChildItem -Path $root -Force -Recurse `
| Where-Object {
if ($_ -isnot [System.IO.DirectoryInfo])
{
foreach ($pattern in $FileType)
{
if ($_.Name -like $pattern)
{
return $true;
}
}
}
return $false;
}
Note that I've modified the strings in $FileType to be formatted as a wildcard pattern. Then group the files by extension:
$resultGroups = $results | Group-Object -Property 'Extension';
Then loop through each group and print the file count and paths:
foreach ($group in $resultGroups)
{
# $group.Count: The number of files with that extension
# $group.Group: A collection of FileInfo objects
# $group.Name: The file extension with leading period
Write-Host "$($group.Name) ---> $($group.Count) files";
Write-Host "$($group.Name) files path ---->";
foreach ($file in $group.Group)
{
Write-Host $file.FullName;
}
}
To write the results to a file instead of the console, use the Out-File cmdlet instead of the Write-Host cmdlet.
I have this bit of powershell script but i can't get the $DirectoryName to behave as expected.
1,2,3 |
foreach {
$count = $_;
$x = gci -Path \\myserver-web$count\d$\IISLogs\ -include *.log -recurse
$x | Copy-Item -Destination D:\ServerLogsAndBackups\IIS\w$count\$_.DirectoryName_$_.Name -whatIf
}
When I run this though I get
What if: Performing operation "Copy File" on Target "Item: \myserver-web1\d$\IISLogs\W3SVC1165836668\ex101224.log Destination: D:\ServerLogsAndBackups\IIS\w1\1.DirectoryName_1.Name".
What I want it to be is
W3SVC1165836668_ex101206.log
where my directory structure is like:
\\myserver-web1\d$\IISLogs\W3SVC1165836668
\\myserver-web1\d$\IISLogs\W3SVC1165837451
\\myserver-web1\d$\IISLogs\W3SVC1165836966
\\myserver-web1\d$\IISLogs\W3SVC1165812365
with files called ex101206.log in each folder
Cheers
You need to evaluate the $_.Directoryname_$_.Name part. Like so,
$x | Copy-Item -Destination $(D:\ServerLogsAndBackups\IIS\w$count\$_.DirectoryName_$_.Name) -whatIf
1,2,3 |
foreach {
$count = $_;
gci -Path \\myserver-web$count\d$\IISLogs\ -include *.log -recurse | % { $dirName = $_.directoryname.Substring($_.directoryname.LastIndexOf("\")+1); $logname = $_.Name; $_ | Copy-Item -Destination $("D:\ServerLogsAndBackups\IIS\w"+$count+"\"+$dirname+"_"+$logName) -whatif }
}