I have folder with files as below:
D:\TESzz\Background\BG_Flash-Zootopia-HD-Desktop-Wallpapers.jpg
D:\TESzz\Background\BG_NimmHBD.jpg
D:\TESzz\Background\BG_Note5.jpg
D:\TESzz\Icons\150x150.jpg
D:\TESzz\Icons\Bathroom-gender-sign.png
D:\TESzz\Icons\brocoli.png
D:\TESzz\Icons\Carrot_Clipart_PNG_Image.png
D:\TESzz\Icons\File.txt
D:\TESzz\Icons\garlic.png
D:\TESzz\Icons\ICONS.txt
D:\TESzz\Icons\NoppNimm-1.jpg
D:\TESzz\Icons\NoppNimmIcon.jpg
D:\TESzz\Icons\NoppNimmIcon.png
D:\TESzz\Icons\NoppProfie.jpg
D:\TESzz\Icons\NoppProfileSerious.jpg
D:\TESzz\Icons\pork.png
D:\TESzz\Icons\Profile.jpg
D:\TESzz\Icons\Questionmark.png
D:\TESzz\Icons\sugar.png
D:\TESzz\Icons\Tree.png
D:\TESzz\Icons\wheel.png
I want to export list of file to each sub-folder under "D:\TeSzz" as below
D:\TESzz\Icons\Icons.csv
D:\TESzz\Background\Background.csv
I have my code as below. but it will create "FileList.csv" instead of "Icons.csv" or "Background.csv". :(
Get-ChildItem -path "D:\TESzz\" -directory | ForEach-Object {Get-ChildItem -file "$($_.fullname)" | Export-Csv "$(Join-path $_.fullname 'FileList.csv')"}
Could someone help me on this please?
I would introduce a variable for the current directory. You don't get your expected result because you are naming the output always as FileList.csv.
Get-ChildItem -path "D:\TESzz\" -directory |
ForEach-Object {
$directory = $_
Get-ChildItem -file $directory.FullName |
Export-Csv (Join-path $directory.FullName "$($directory.BaseName).csv") -Force
}
In the above, notice that where you had FileList, that's now $($directory.BaseName), and therefore different for each directory.
You can put the parent directory name in a variable, then use it to form the output file name:
$parentDir = "D:\TESzz";
Get-ChildItem -path $parentDir -directory | ForEach-Object {Get-ChildItem -name $parentDir\$_ | Export-Csv -path $(Join-path $parentDir\$_ ($_.Name+'.csv'))}
Here, we use $parentDir to create the export path.
Related
I have this simple cmdlet that correctly copies files and folders to a second directory:
Copy-Item -Path 'G:\xyz\Test\A' -Recurse -Destination 'G:\xyz\Test\B\'
However I am unable to tweak it to only copy the latest file in each folder within its folder (i.e. also copying the folder structure). I have written the following, but this doesn't copy folder names and does not go down all the hierarchies of sub-folders.
Get-ChildItem -Path 'G:\xyz\Test\A' -Directory | ForEach-Object {
Get-ChildItem -Path 'G:\xyz\Test\A' -File -Recurse |
Sort-Object LastWriteTime | Select-Object -Last 1 |
Copy-Item -Destination 'G:\xyz\Test\B\'
}
Could someone please identify my errors!
A slightly different approach of collecting the directories first then iterating through them to retrieve the files.
If ( -not (Test-Path -Path "G:\Test\B")) {
$Null = New-Item -ItemType "Directory" "G:\Test\B"
}
Get-ChildItem -Path 'G:\Test\A' -Directory -Recurse |
#Sort to insure dirs are created shortest to longest
Sort-Object FullName |
ForEach-Object {
$DestPath = $($_.FullName).Replace("`\Test`\A","`\Test\`B")
If ( -not (Test-Path -Path "$DestPath")) {
$Null = New-Item -ItemType "Directory" "$DestPath"
}
Get-ChildItem -Path "$($_.FullName)" -File |
Sort-Object LastWriteTime |
Select-Object -Last 1 |
Copy-Item -Destination "$DestPath"
}
First error is that you don't pass the current directory from the outer Get-ChildItem to the inner one. The inner one currently always loops over the same sub directory.
Also, when you copy individual files in a Get-ChildItem pipeline, you have to build up the destination path on your own, in order to keep the relative source directory structure:
$source = 'G:\xyz\Test\A'
$destination = 'G:\xyz\Test\B\'
# Set base directory for outer Get-ChildItem and Resolve-Path -Relative
Push-Location $source
try {
Get-ChildItem -Directory | ForEach-Object {
Get-ChildItem -Path $_.Fullname -File -Recurse |
Sort-Object LastWriteTime | Select-Object -Last 1 |
ForEach-Object {
# Make the current file path relative to $source
$relativePath = Resolve-Path $_.Fullname -Relative
# Build up the full destination file path
$destinationFullPath = Join-Path $destination $relativePath
# Create destination directory if not already exists (-force)
$null = New-Item (Split-Path $destinationFullPath -Parent) -ItemType Directory -Force
# Copy the current file
Copy-Item -Path $_.Fullname -Destination $destinationFullPath
}
}
}
finally {
Pop-Location # Restore the current directory
}
Resolve-Path -Relative makes the given path relative to the current directory. In order to get paths relative to the source directory, we set the current directory to the source directory path using Push-Location.
The try and finally blocks make sure the current directory is restored even in case of a script-terminating error (exception).
Here is my current script and it works fine. Not efficient running same code twice but I don't know how to combine the wildcards... anyway on to the bigger issue.
The below code searches through my $sourceDir, excludes the files listed in $ExclusionFiles, copies all folders and structure as well as any .jpg or any .csv files, then puts them into the $targetDir.
$sourceDir = 'c:\sectionOne\Graphics\Data'
$targetDir = 'C:\Test\'
$ExclusionFiles = #("InProgress.jpg", "input.csv", "PCMCSV2.csv")
# Get .jpg files
Get-ChildItem $sourceDir -filter "*.jpg" -recurse -Exclude $ExclusionFiles | `
foreach{
$targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length);
New-Item -ItemType File -Path $targetFile -Force;
Copy-Item $_.FullName -destination $targetFile
}
# Get .csv files
Get-ChildItem $sourceDir -filter "*.csv" -recurse -Exclude $ExclusionFiles | `
foreach{
$targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length);
New-Item -ItemType File -Path $targetFile -Force;
Copy-Item $_.FullName -destination $targetFile
}
My list of files in the main $sourceDir that I need to exclude is getting longer and there are folders I want to exclude as well. Can someone tell me how to,
Copy only a list of specific files in the $sourceDir
Exclude certain folders in $sourceDir from copying
Combine the wildcard search for .jpg and .csv into one statement
I'm still learning so any help would be greatly appreciated!
This is a case where a little bit of Regex will go a long way:
You can filter multiple extensions by using a pretty basic match:
$extensions = 'jpg', 'csv'
$endsWithExtension = "\.(?>$($extensions -join '|'))$"
Get-ChildItem -Recurse |
Where-Object Name -Match $endsWithExtension
You can exclude a list of specific files with one more Where-Object and the -In parameter:
$extensions = 'jpg', 'csv'
$endsWithExtension = "\.(?>$($extensions -join '|'))$"
$ExcludeFileNames = #("InProgress.jpg", "input.csv", "PCMCSV2.csv")
Get-ChildItem -Recurse |
Where-Object Name -Match $endsWithExtension |
Where-Object Name -NotIn $ExcludeFileNames
From there on in, your Foreach-Object is basically correct (nice touch making sure the file exists by using New-Item, though I'd personally assign it's output to null and -PassThru the Copy-Item).
Get-ChildItem $sourceDir -Recurse |
Where-Object Name -Match $endsWithExtension |
Where-Object Name -NotIn $ExcludeFileNames |
Foreach-Object {
$targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length);
New-Item -ItemType File -Path $targetFile -Force;
Copy-Item $_.FullName -destination $targetFile
}
I am trying to copy the latest file from every folder/sub-folder into a same file structure on a different drive.
Latest file from source copied to the same name corresponding destination.
The destination folder hierarchy already exists & cannot be copied over or recreated. This & other versions are not behaving. Can anyone help?
$sourceDir = 'test F Drive\Shares\SSRSFileExtract\'
$destDir = 'test X Drive\SSRSFileExtract\'
$date = Get-Date
$list = Get-ChildItem -Path $sourceDir | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
foreach ($item in $list)
{
Copy-Item -Verbose -LiteralPath $item.FullName -Destination $destDir -Force |
Get-Acl -Path $item.FullName | Set-Acl -Path $destDir\$(Split-Path -Path $item.FullName -Leaf)
}
Get-ChildItem –Path $destDir -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-5))} | Remove-Item -Verbose -Recurse -Force
I found a solution for this which copies/moves all the files from all sub folders in to all corresponding sub folders:
Powershell: Move all files from folders and subfolders into single folder
The way your code retrieves the list of files will only return one single object because of Select-Object -First 1. Also, because you don't specify the -File switch, Get-ChildItem will also return DirectoryInfo objects, not just FileInfo objects..
What you could do is get an array of FileInfo objects recursively from the source folder and group them by the DirectoryName property
Then loop over these groups of files and from each of these groups, select the most recent file and copy that over to the destination folder.
Try:
$sourceDir = 'F:\Shares\SSRSFileExtract'
$destDir = 'X:\SSRSFileExtract'
Get-ChildItem -Path $sourceDir -File -Recurse | Group-Object DirectoryName | ForEach-Object {
# the $_ automatic variable represents one group at a time inside the loop
$newestFile = $_.Group | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
# construct the target sub directory
# you could also use $_.Name (the name of this group) instead of $newestFile.DirectoryName here, because
# we grouped on that DirectoryName file property.
$targetDir = Join-Path -Path $destDir -ChildPath $newestFile.DirectoryName.Substring($sourceDir.Length)
# if you're not sure the targetpath exists, uncomment the next line to have it created first
# if (!(Test-Path -Path $targetDir -PathType Container)) { $null = New-Item -Path $target -ItemType Directory }
# copy the file
$newestFile | Copy-Item -Destination $targetDir -Force -Verbose
# copy the file's ACL
$targetFile = Join-Path -Path $targetDir -ChildPath $newestFile.Name
$newestFile | Get-Acl | Set-Acl -Path $targetFile
}
Apparently you would also like to clean up older files in the destination folder
Get-ChildItem –Path $destDir -File -Recurse |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-5).Date} |
Remove-Item -Verbose -Recurse -Force
Be aware that the final code to remove older files could potentially remove all files from a subfolder if all happen to be older than 5 days..
I want to combine the content of all the files in my subfolders in one file. However, I want to exclude the root folder from this search.
I'm very close with the following command:
Get-ChildItem -include *.sql -rec | ForEach-Object {gc $_; ""} | out-file final.sql
The problem is that, as the foreach is recursive, it also finds out the output file (final.sql), which creates an infinite loop. This powershell commands never ends and the final.sql file gets larger and larger with time.
How can I exclude the current directory from my search?
Important: I don't want to explicitly mention the path, as different users will have a different file system.
Try this:
$mainPath = 'C:\files\test'
$directories = Get-ChildItem -Path $mainPath -Directory
$destFile = $mainPath + '\final.sql'
Remove-Item -Path $destFile -ErrorAction SilentlyContinue | Out-Null
foreach( $directory in $directories ) {
Get-ChildItem -Path $directory.FullName -include *.sql -rec | ForEach-Object {gc $_; ""} | Out-File $destFile -Append
}
The user f6a4 answered correctly, but if you don't want to use the fully qualified directory name, you better use this version:
$directories = Get-ChildItem -Path $mainPath -Directory
Remove-Item -Path final.sql -ErrorAction SilentlyContinue | Out-Null
foreach( $directory in $directories ) {
Get-ChildItem -Path $directory.FullName -include *.sql -rec | ForEach-Object {gc $_; ""} | Out-File final.sql -Append
}
You can run this file using powershell by creating the following .ps1 file:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './GlobalVersionPowershellBatch.ps1'"
My script looks for all the files in folder1 and checks if this file exist in folder2. if it exists then I want to delete the file from folder2 and move the file from Folder1 to folder3.
$folder1 = "D:\folder1"
$folder2= "D:\folder2"
$folder3 = "D:\folder3"
$a = Get-ChildItem $folder1 | select -ExpandProperty basename
$a | foreach {
Get-ChildItem -Path $folder2 -filter *$_* -Recurse
}
Now if I use pipe at the end of for each loop I can either delete or move but not both. How do I handle this situation?
Neither move-item or remove-item output anything to the pipe. However both have a -PassThru switch parameter to allow further processing.
This will do the job:
$A | foreach { Get-ChildItem -Path $folder2 -filter $_ -Recurse} | foreach {Remove-Item $_.FullName; Copy-Item $folder1\$_.BaseName $Folder3}
Get-ChildItem -File -Include(Get-ChildItem -File $folder1) $folder2\* |
foreach { remove-item $_ ; move-item (join-path $folder1 $_.BaseName) -destination $folder3}