I want to iterate through the folders inside the patch scripts, find every iterated result of DBChangesMain and ContentLbl and store it into a dictionary or hash table, and then print out the results how do i do this?
So far I have
$patchscripts = Get-Item "F:\folder\trunk\Source\Database\Patch Scripts"
foreach ($folders in Get-childitem $patchscripts -recurse -include *.sql )
{
if ($folders -like "*DBChangesMain*")
{
}
if ($folders -like "*ContentLbl*")
{
}
}
Write-Host $DbChanges
Write-Host $contentlbl
if you have a better way of doing this, please let me know, cheers.
I guess after your comments I'd rather go the following route:
$patchscripts = 'F:\folder\trunk\Source\Database\Patch Scripts'
$dbChangesMain = Get-ChildItem $patchScripts -Rec -Inc *DBChangesMain*.sql
$contentLbl = Get-ChildItem $patchScripts -Rec -Inc *ContentLbl*.sql
Afterwards you simply have two arrays containing the matching names of the SQL files.
Related
I want to create a script that searches through a directory for specific ".txt" files with the Get-ChildItem cmdlet and after that it copies the ".txt" to a location I want. The hard part for me is to extract specific .txt files string from the array. So basically I need help matching specific files names in the array. Here is an example of the array I'm getting back with the following cmdlet:
$arrayObject = (Get-ChildItem -recurse | Where-Object { $_.Name -eq "*.txt"}).Name
The arrayobject variable is something like this:
$arrayobject = "test.2.5.0.txt", "test.1.0.0.txt", "test.1.0.1.txt",
"test.0.1.0.txt", "test.0.1.1.txt", "test.txt"
I want to match my array so it returns the following:
test.2.5.0.txt, test.1.0.0.txt, test.1.0.1.txt
Can someone help me with Regex to match the above file names from the $arrayObject?
As you already add the -Recurse parameter to Get-ChildItem, you can also use the -Include parameter like this:
$findThese = "test.2.5.0.txt", "test.1.0.0.txt", "test.1.0.1.txt"
$filesFound = (Get-ChildItem -Path 'YOUR ROOTPATH HERE' -Recurse -File -Include $findThese).Name
P.S. without the -Recurse parameter you need to add \* to the end of the rootfolder path to be able to use -Include
Maybe something like:
$FileList = Get-ChildItem -path C:\TEMP -Include *.txt -Recurse
$TxtFiles = 'test1.txt', 'test3.txt', 'test9.txt'
Foreach ($txt in $TxtFiles) {
if ($FileList.name -contains $txt) {Write-Host File: $Txt is present}
}
A general rule: Filter as left as possible! Less objects to be processed, less resources to be used, faster to be processed!
Hope it helps!
Please try to clarify what the regex should match.
I created a regex which matches out of the given filenames only the files you wanted to retrieve:
"*.[1-9].[0-9].[0-9].txt"
You can tryout the small check I wrote.
ForEach($file in $arrayobject){
if($file -LIKE "*.[1-9].[0-9].[0-9].txt"){
Write-Host $file
}}
I think the "-LIKE" operator would be better to check if a string matches a regex.
Let me know if this helps.
Sorry for the late reply. Just got back in the office today. My question has been misinterpreted but that's my fault. I wasn't clear what I really want to do.
What I want to do is search through a directory and retrieve/extract in my case the (major)version of a filename. So in my case file "test.2.5.0.txt" would be version 2.5.0. After that I will get the MajorVersion and that's 2. Then in an If statement I would check if it's greater or equal to 1 and then copy it to a specific destination. To add some context to it. It's nupkg files and not txt. But I figured it out. This is code:
$sourceShare = "\\server1name\Share\txtfilesFolder"
destinationShare = "\\server2name\Share\txtfilesFolder"
Get-ChildItem -Path $sourceShare `
-Recurse `
-Include "*.txt" `
-Exclude #("*.nuspec", "*.sha512") `
| Foreach-Object {
$fileName = [System.IO.Path]::GetFileName($_)
[Int]$majorVersion = (([regex]::Match($fileName,"(\d+(.\d+){1,})" )).Value).Split(".")[0]
if ($majorVersion -ge 1)
{
Copy-Item -Path $_.FullName `
-Destination $destinationShare `
-Force
}
}
If you have anymore advice. Let me know. I would be great to extract the major version without using the .Split method
Grtz
I'm attempting to write a PowerShell script to move files from one directory to another based on a few conditions. For example:
An example of a file name: testingcenter123456-testtype-222-412014.pdf.
The script should look for "testingcenter123456" before the first dash ("-") and then refer to a hash table for a matching key. All the files follow the format shown above.
Once its finds that key, it should use that key's corresponding value (example: "c:\temp\destination\customer7890") as the destination file path and copy the file there.
I looked around StackOverflow and found a few Q&As that seemed to answer parts of similar questions but the fact that I'm very new to this has led to the script I pieced together not working at all.
Here's what I have so far:
$hashTable = ConvertFrom-StringData ([IO.File]::ReadAllText("c:\temp\filepaths.txt"))
$directory = "c:\temp\source"
Get-ChildItem $directory |
where {!($_.PsIsContainer)} |
Foreach-Object {
Foreach ($key in $hashTable.GetEnumerator()){
if ($_.Name.Substring(0,$_.Name.IndexOf("-")) -eq $key.Name){
Copy-Item -Path $_.fullname -Destination $key.Value
}
}
}
If anyone can help me get un-stuck and hopefully learn a little something about PowerShell in the process, I'd appreciate it.
Honestly, I'm not seeing why this shouldn't work. It would be helpful if you told us which line was generating an error.
Foreach ($key in $hashTable.GetEnumerator()) {
if ($_.Name.Substring(0,$_.Name.IndexOf("-")) -eq $key.Name) {
Copy-Item -Path $_.fullname -Destination $key.Value
}
}
That said, you're missing the point of using hashtable by looping through its entries, manually matching on key. With a hashtable, you don't need to loop e.g.
$hashTable = ConvertFrom-StringData ([IO.File]::ReadAllText("c:\temp\filepaths.txt"))
Get-ChildItem c:\temp\source |
Where {!($_.PsIsContainer)} |
Foreach-Object {
$key = $_.Name.Substring(0,$_.Name.IndexOf("-"))
$val = $hashtable.$key
if ($val) {
$_ | Copy-Item -Dest $val -WhatIf
}
else {
Write-Warning "No entry for $key"
}
}
I am still pretty new to scripting and "programming" at all. if you miss any information here let me know.
This is my working zip function:
$folder = "C:\zipthis\"
$destinationFilePath = "C:\_archive\zipped"
function create-7zip{
param([string] $folder,
[String] $destinationFilePath)
write-host $folder $destinationFilePath
[string]$pathToZipExe = "C:\Program Files (x86)\7-Zip\7zG.exe";
[Array]$arguments = "a", "-tzip", "$destinationFilePath", "$folder";
& $pathToZipExe $arguments;
}
Get-ChildItem $folder | ? { $_.PSIsContainer} | % {
write-host $_.BaseName $_.Name;
$dest= [System.String]::Concat($destPath,$_.Name,".zip");
(create-7zip $_.FullName $dest)
}
create-7zip $folder $destinationFilePath
now I want him to zip special folders which I already sorted out :
get-childitem "C:\zipme\" | where-Object {$_.name -eq "www" -or $_.name -eq "sql" -or $_.name -eq "services"}
This small function finds the 3 folders I need called www, sql and services. But I didn't manage to insert this into my zip function, so that exactly this folders are zipped and put into C:\_archive\zipped
Because a string is used instead of an array, he tried always to look for a folder called wwwsqlservice which is not there. I tried to put an array using #(www,sql,services) but i had no success, so whats the right way, if there is one?
It should compatible with powershell 2.0, no ps3.0 cmdlets or functions please.
thanks in advance!
Here's a really simple example of what you want to do, removed from the context of your function. It assumes that your destination folders already exist (You can just use Test-Path and New-Item to create them if they don't), and that you're using 7z.exe.
$directories = #("www","sql","services")
$archiveType = "-tzip"
foreach($dir in $directories)
{
# Use $dir to update the destination each loop to prevent overwrites!
$sourceFilePath = "mySourcePath\$dir"
$destinationFilePath = "myTargetPath\$dir"
cmd /c "$pathToZipExe a $archiveType $destinationFilePath $sourceFilePath"
}
Overall it looks like you got pretty close to a solution, with some minor changes needed to support the foreach loop. If you're confident that create-7zip works fine for a single folder, you can substitute that for the cmd /c line above. Here's a listing of some handy example usages for 7zip on the command line.
I have part of a deploy PowerShell 2.0 script that copy a potential robots.dev.txt to robots.txt, if it doesn't exist don't do anything.
My original code was:
$RobotFilesToOverWrite= Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt"
foreach($file in $RobotFilesToOverWrite)
{
$origin=$file
$destination=$file -replace ".$Environment.","."
#Copy-Item $origin $destination
}
But, in a difference with C#, even if $RobotFilesToOverWrite is null, code is entering in the foreach.
So I had to surround everything with:
if($RobotFilesToOverWrite)
{
...
}
This is the final code:
$RobotFilesToOverWrite= Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt"
if($RobotFilesToOverWrite)
{
foreach($file in $RobotFilesToOverWrite)
{
$origin=$file
$destination=$file -replace ".$Environment.","."
#Copy-Item $origin $destination
}
}
I was wondering if there is a better way to achieve that?
EDIT: This problem seems to be fixed in PowerShell 3.0
# one way is using #(), it ensures an array always, i.e. empty instead of null
$RobotFilesToOverWrite = #(Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt")
foreach($file in $RobotFilesToOverWrite)
{
...
}
# another way (if possible) is not to use an intermediate variable
foreach($file in Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt")
{
...
}
quote from http://blogs.msdn.com/b/powershell/archive/2012/06/14/new-v3-language-features.aspx
ForEach statement does not iterate over $null
In PowerShell V2.0, people were often surprised by:
PS> foreach ($i in $null) { 'got here' }
got here
This situation often comes up when a cmdlet doesn’t return any objects. In PowerShell V3.0, you don’t need to add an if statement to avoid iterating over $null. We take care of that for you.
I want to zip some selected folders into an archive only with their relative paths.
Some of them have the same folder names (all release-folders, all debug-folders).
The filtering works fine and I have called set-location prior to get-children command.
What is the easiest way to do this job?
Do I really have to implement something like this
foreach ($o in $children)
{ $relPath = $o.FullName.Substring(subPath.Length);
$relPath = $relPath.Substring(0, relPath.LastIndexOf(#"\"));
zip.AddFolder($o.Name, $relPath);
}
Does someone can provide me an example?
Thx
When I need to handle relative paths, I do something like this:
$basePath = "C:\MyBasePath"
$newBasePAth = "C:\NewBasePath"
$files = Get-ChildItem $basePath
$newFileNames = foreach ($f in $files) {
$f.Fullname.Replace($basePath, $newBasePath)
}
Make sure the pattern of slashes you use is the same in $basePath and $newBasePath (use .Trim() to be sure)
Hope this helps
I finally implemented this and it works with the relative paths:
function ZipUp-Files ( $mychildren )
{
foreach ($o in $mychildren)
{
$e= $zipfile.AddDirectory($o.FullName,$o.fullname.substring($pwd.path.length));
}
}
$children =get-childitem -recurse -force | where-object {$_.psiscontainer} | where {$_.name -match $teststring}
[System.Reflection.Assembly]::LoadFrom("C:\dotNetZip\zip-v1.9\Release\Ionic.Zip.dll");
$zipfile = new-object Ionic.Zip.ZipFile($ziptarget);
$zipfile.UseZip64WhenSaving= [Ionic.Zip.Zip64Option]::Always
$zipfile.MaxOutPutSegmentSize=734003200
ZipUp-Files $children
$zipfile.Save("c:\temp\arc_rel.zip")
$zipfile.Dispose()