$getFiles = Get-ChildItem -Path $readDir -File -Include "*.doc","*.docx","*.xlsx"-Recurse | %{
if(($_ -match "\.doc$") -or ($_ -match "\.docx$")){
$Doc = $word.Documents.Open($_.fullname)
$nameDoc = $fileSaveLoc + $_.Name.Replace(".docx",".txt").replace(".doc",".txt")
$Doc.saveas([ref] $nameDoc, [ref] 5)
$Doc.close()
if((Get-ChildItem "I:\temp\").length -ne 0){
$locations = (Get-Item "I:\temp\"), (Get-ChildItem "I:\temp\" -Directory -recurse) | % {
Get-ChildItem -File $_.FullName | Select-String -List -Pattern '^\d{3}-?\d{2}-?\d{4}$' |
% Path
}
if($locations -ne $null){
$locations | out-file "I:\temp\SSN_FILES.txt"
#Get-ChildItem "I:\temp\" -exclude "fullpath.txt","SSN_FILES.txt" | remove-item
}else{
Get-ChildItem "I:\temp\" -exclude "fullpath.txt","SSN_FILES.txt" | remove-item
}
}
}
elseif($_ -match "\.xlsx$"){
$workbook = $excel.Workbooks.Open($_.FullName)
$csvFilePath = "I:\temp\" + $_.Name.Replace(".xlsx",".csv")
#$csvFilePath = $_.FullName -replace "\.xlsx$", ".csv"
$workbook.SaveAs($csvFilePath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSV)
$workbook.Close()
if((Get-ChildItem "I:\temp\").length -ne 0){
$locations = (Get-Item "I:\temp\"), (Get-ChildItem "I:\temp\" -Directory -recurse) | % {
Get-ChildItem -File $_.FullName | Select-String -List -Pattern '^\d{3}-?\d{2}-?\d{4}$' |
% Path
}
if($locations -ne $null){
$locations | out-file "I:\temp\SSN_FILES.txt"
#Get-ChildItem "I:\temp\" -exclude "fullpath.txt","SSN_FILES.txt" | remove-item
}else{
Get-ChildItem "I:\temp\" -exclude "fullpath.txt","SSN_FILES.txt" | remove-item
}
}
}
}
So this basically says:
check for a file matching doc/docx/xlsx
convert them into a file that can be parsed through
parse through each file at every iteration and compare it to a regex
if the regex is not null, then output it to a file with the file path
otherwise, delete it and anything else that was created except for two files
restart the process at the next file
Now the problem I am encountering is that the files aren't being deleted. I can't get them to be removed when they are created, when I know they don't match the regex. Setting ($locations -eq $true) doesn't solve that issue because it never goes into the first conditional statement.
The folder should contain only the two files that were created and possibly the ones that match the regex.
Related
I have this script I received to check folders and subfolders on a network drive. I wonder how it could be modified into checking only folders and subfolder and write in the CSV if there is any folder with more then 40.000 files in it and the number of files. The image show a sample output from the script as it is now and I do not need it to show any files as it currently do.
$dir = "D:\test"
$results = #()
gci $dir -Recurse -Depth 1 | % {
$temp = [ordered]#{
NAME = $_
SIZE = "{0:N2} MB" -f ((gci $_.Fullname -Recurse | measure -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB)
FILE_COUNT = (gci -File $_.FullName -Recurse | measure | select -ExpandProperty Count)
FOLDER_COUNT = (gci -Directory $_.FullName -Recurse | measure | select -ExpandProperty Count)
DIRECTORY_PATH = $_.Fullname
}
$results += New-Object PSObject -Property $temp
}
$results | export-csv -Path "C:\temp\output.csv" -NoTypeInformation
Instead of executing so many Get-ChildItem cmdlets, here's an approach that uses robocopy to do the heavy lifting of counting the number of files, folders and total sizes:
# set the rootfolder to search
$dir = 'D:\test'
# switches for robocopy
$roboSwitches = '/L','/E','/NJH','/BYTES','/NC','/NP','/NFL','/XJ','/R:0','/W:0','/MT:16'
# regex to parse the output from robocopy
$regEx = '\s*Total\s*Copied\s*Skipped\s*Mismatch\s*FAILED\s*Extras' +
'\s*Dirs\s*:\s*(?<DirCount>\d+)(?:\s+\d+){3}\s+(?<DirFailed>\d+)\s+\d+' +
'\s*Files\s*:\s*(?<FileCount>\d+)(?:\s+\d+){3}\s+(?<FileFailed>\d+)\s+\d+' +
'\s*Bytes\s*:\s*(?<ByteCount>\d+)(?:\s+\d+){3}\s+(?<BytesFailed>\d+)\s+\d+'
# loop through the directories directly under $dir
$result = Get-ChildItem -Path $dir -Directory | ForEach-Object {
$path = $_.FullName # or if you like $_.Name
$summary = (robocopy.exe $_.FullName NULL $roboSwitches | Select-Object -Last 8) -join [Environment]::NewLine
if ($summary -match $regEx) {
$numFiles = [int64] $Matches['FileCount']
if ($numFiles -gt 40000) {
[PsCustomObject]#{
PATH = $path
SIZE = [int64] $Matches['ByteCount']
FILE_COUNT = [int64] $Matches['FileCount']
FOLDER_COUNT = [int64] $Matches['DirCount']
}
}
}
else {
Write-Warning -Message "Path '$path' output from robocopy was in an unexpected format."
}
}
# output on screen
$result | Format-Table -AutoSize
# output to CSV file
$result | Export-Csv -Path "C:\temp\output.csv" -NoTypeInformation
I am trying to copy all files recursively from a:\ to b:\, except those whose metadata is present in a:\list.txt. The list.txt pattern is LastWriteTimeYYYY-MM-DD HH:MM:SS,size,.fileextension, for example:
2001-01-31 23:59:59,12345,.doc
2001-01-31 23:59:59,12345,.txt
2001-01-31 23:59:00,456,.csv
...so any and all files, anywhere in the a:\ dir tree, matching these metadata should not be copied.
I seem to be having trouble with the Where-Object in order to exclude the items on the list.txt, but copy everything else:
$Source = "C:\a"
$Target = "C:\b"
$List = Import-Csv list.txt -Header LastWriteTime,Size,Name
$Hash = #{}
ForEach ($Row in $List){
$Key = ("{0},{1},.{2}" -F $Row.LastWriteTime,$Row.Size,$Row.Name.Split('.')[-1].ToLower())
IF (!($Hash[$Key])) {$Hash.Add($Key,$Row.Name)}
}
$Hash | Format-Table -Auto
Get-Childitem -Path $Source -Recurse -File | Where-Object {$Hash -eq $Hash[$Key]}| ForEach-Object {$Key = ("{0},{1},{2}" -F ($_.LastWriteTime).ToString('yyyy-MM-dd HH:mm:ss'),$_.Length,$_.Extension.ToLower())
#$Key
If ($Hash[$Key]){
$Destination = $_.FullName -Replace "^$([RegEx]::Escape($Source))","$Target"
If (!(Test-Path (Split-Path $Destination))){MD (Split-Path $Destination)|Out-Null}
$_ | Copy-Item -Destination $Destination
}
}
I propose you a simplification of your code :
$Source = "C:\a\"
$Target = "C:\b\"
New-Item -ItemType Directory $Target -Force | Out-Null
$List = Import-Csv list.txt -Header LastWriteTime,Length,Extension
Get-Childitem $Source -Recurse -File | %{
$File=$_
$exist=$List | where {$_.LastWriteTime -eq $File.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss') -and $_.Length -eq $File.Length -and $_.Extension -eq $File.Extension} | select -first 1
if ($exist -ne $null) {continue}
New-Item -ItemType Directory $File.DirectoryName.Replace($Source, $Target) -Force | Out-Null
Copy-Item $File.FullName $File.FullName.Replace($Source, $Target) -Force
}
I am trying to move files based on a list of prefix from a text file to match a portion of the actual files which are dynamically generated.
Example of prefix in text file:
103 1stCity
25 2ndCity
302 3rdCity
There are no commas and each city is a new line in the text file (not CSV format).
Example of files to search:
103 1stCity 20170901 12387.txt
129 OtherCity 20170905 354568.txt
Here is what I have:
$file_list = Get-Content "P:\some\path\to\PrefixOfClientNames.txt"
$search_folder = "J:\FilesToSearch_SomeStayHere"
$destination_folder = "J:\SomeFilesGetMovedHere"
foreach ($file in $file_list) {
$file_to_move = Get-ChildItem -Path $search_folder |
Where-Object { $_.Name -like $file }
if ($file_to_move) {
Move-Item $file_to_move $destination_folder -WhatIf
}
If all matching files should go to the same destination folder you'd build a regular expression from the prefix file and anchor it at the beginning of the string:
$prefixes = Get-Content 'P:\some\path\to\PrefixOfClientNames.txt' |
ForEach-Object { [regex]::Escape($_) }
$pattern = '^{0}' -f ($prefixes -join '|')
then use a pipeline for moving matching files:
Get-ChildItem -Path $search_folder | Where-Object {
$_.Name -match $pattern
} | Move-Item -Destination $destination_folder -WhatIf
Other method
$file_list = Get-Content "C:\Temp\prefix.txt" | %{$_ + '*'}
$search_folder = "C:\Temp\searchfolder"
$destination_folder = "C:\Temp\searchfolder"
Get-ChildItem $search_folder -file -Recurse | %{
$name=$_.BaseName
$fullname=$_.FullName
if (($file_list | where { $name -like $_} | select -First 1).Count -gt 0)
{
Move-Item $fullname $destination_folder -WhatIf
}
}
Im currently trying to get a script to do the following:
search a file location and obtain any .txt,.ini,.config file.
filter this file on an input from the user.
move all the files to a location.
Im having a problem trying to feed in the variable and its probably really simple but im currently struggling to figure out what I need to do without splitting the string up manually. any ideas?
$QQ = Read-Host -Prompt "String your searching for:"
$QL = Read-Host -Prompt "Enter the file location you wish to search:"
$FT = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".txt"} | % {$_.fullname}
$FI = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".ini"} | % {$_.fullname}
$FC = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".config"} | % {$_.fullname}
$FTS = Get-Content -Path "$FT" -Filter "$QQ"
$FIS = Get-Content -Path "$FI" -Filter "$QQ"
$FCS = Get-Content -Path "$FC" -Filter "$QQ"
$FD = "C:\Search-$QQ"
$FD1 = Get-ChildItem $FD
function folder {
if ($FD -eq $Null) {New-Item "$FD" -ItemType directory}}
function search{
if ($FTS -ne $null){Copy-Item -Path $ft -Destination "$fd" | Write-Host "$FT" | Format-List}
if ($FIS -ne $null){Copy-Item -path $fi -Destination "$fd" | Write-Host "$FI" | Format-List}
if ($FCS -ne $null){Copy-Item -Path $fc -destination "$fd" | Write-Host "$FC" | Format-List}
}
folder
search;
An example of the error being received is: (obviously an issue with it treating the multiple files in the string as one)
Get-Content : Cannot find path 'C:\test\Test\1c.config C:\test\Test\2c.config C:\test\Test\3c.config' because it does not exist.
right off the bat I see the folder function will not work property as $FD will never be null since you assign a string to it, if you need to check if the folder exists use Test-Path. but in relation to your actual script the problem is your use of get-content which is trying to read all of your files contents into an array of strings. from the looks of it you should just be working with the arrays returned by Get-ChildItem and use Test-Path to check your paths rather than that weird Get-Content method you are using(since the content of the files doesn't matter, just whether they exist or not). You also need to incorporate a loop to act on each array element individually rather than trying to work on them in groups like you are.
(This Script is to mass search application files to find values like server id's or passwords hardcoded etc)
Fixed code:
$QQ = Read-Host -Prompt "String your searching for:"
$QL = Read-Host -Prompt "Enter the file location you wish to search:"
$FT = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".txt"} | % {$_.fullname}
$FI = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".ini"} | % {$_.fullname}
$FC = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".config"} | % {$_.fullname}
$FTS = Get-Content -Path $FT -Filter "$QQ"
$FIS = Get-Content -Path $FI -Filter "$QQ"
$FCS = Get-Content -Path $FC -Filter "$QQ"
$FD = "C:\Support\Search-$QQ"
$FD1 = Test-Path $FD
function folder {
if ($FD1 -eq $false) {New-Item "$FD" -ItemType directory}
}
function search{
if ($FTS -ne $null){Copy-Item -Path $ft -Destination "$fd" | Write-Host "$FT" | Format-List}
if ($FIS -ne $null){Copy-Item -path $fi -Destination "$fd" | Write-Host "$FI" | Format-List}
if ($FCS -ne $null){Copy-Item -Path $fc -destination "$fd" | Write-Host "$FC" | Format-List}
}
folder
search;
try this
$QL = Read-Host -Prompt "Enter the file location you wish to search:"
if (-not (Test-Path $QL))
{
write-host "Specified path ($QL) doesnt exists "
return;
}
$QQ = Read-Host -Prompt "String your searching for:"
$FD = "C:\Search-$QQ"
New-Item "C:\Search-$QQ" -ItemType directory -Force | out-null
Get-ChildItem -Path "$QL" -recurse -include "*.txt", "*.ini", "*.config" |
select-string -Pattern "$QQ" -SimpleMatch |
%{ Copy-Item -Path $_.Path -Destination "$fd" -Force ; $_.Path }
I am working with a script to clear old files off our file server. We are using this line in the script to find all files older than a certain date:
$oldFiles = Get-ChildItem $oldPath -Recurse | Where-Object { $_.lastwritetime -le $oldDate }
My question is, how do I ignore a certain directory in the $oldPath? For instance, if we had the following:
root
dir1
dir 2
subdir 1
subdir 2
dir 3
subdir 1
dir 4
And we want to ignore dir 2 and all subdirectories when building the list
Final working script:
$oldPath = "\\server\share"
$newDrive = "I:"
$oldDate = Get-Date -Date 1/1/2012
$oldFiles = Get-ChildItem $oldPath -Recurse -File | Where-Object {($_.PSParentPath -notmatch '\\Ignore Directory') -and $_.lastwritetime -le $oldDate }
$oldDirs = Get-ChildItem $oldPath -Recurse | Where-Object {$_.PSIsContainer -and ($_.PSParentPath -notmatch '\\Ignore Directory')} | select-object FullName
$oldDirs = $oldDirs | select -Unique
foreach ($oldDir in $oldDirs) {
$strdir = $newDrive + "\" + ($oldDir | Split-Path -NoQualifier | Out-String).trim().trim("\")
if (!(Test-Path $strdir)) {
Write-Host "$strdir does not exist. Creating directory..."
mkdir $strdir | Out-Null
} # end if
} # end foreach
foreach ($file in $oldFiles) {
$strfile = $newDrive + "\" + ($file.FullName | Split-Path -NoQualifier | Out-String).trim().trim("\")
Write-Host "Moving $file.FullName to $strfile..."
Move-Item $file.FullName -Destination $strfile -Force -WhatIf
} # end foreach
$oldfiles | select pspath | Split-Path -NoQualifier | Out-File "\\nelson\network share\ArchivedFiles.txt"
Modify your Where-Object condition to:
... | Where-Object {($_.PSParentPath -notmatch '\\dir 2') -and ($_.lastWriteTime -le $oldDate)}
Also, you probably want to filter out directory items as well so that $oldFiles contains only files e.g.:
$oldFiles = Get-ChildItem $oldPath -Recurse | Where {!$_.PSIsContainer -and ($_.PSParentPath -notmatch '\\dir 2') -and ($_.lastWriteTime -le $oldDate)}
If you're on PowerShell v3 you can use a new parameter on Get-ChildItem to simplify this to:
$oldFiles = Get-ChildItem $oldPath -Recurse -File | Where {($_.PSParentPath -notmatch '\\dir 2') -and ($_.lastWriteTime -le $oldDate)}
Something like this should work:
$exclude = Join-Path $oldPath 'dir 2'
$oldFiles = Get-ChildItem $oldPath -Recurse | ? {
-not $_.PSIsContainer -and
$_.FullName -notlike "$exclude\*" -and
$_.LastWriteTime -le $oldDate
}
Try $oldFiles = Get-ChildItem $oldPath -Recurse -Exclude "dir 2" | Where-Object { $_.lastwritetime -le $oldDate}