How to search filenames in a log file in Powershell - powershell

I have made a script that builds the .vbp files in the company that I work. I want to build only the last changed files. By using timestamps I will build more unecessary files as some people test the files. So I thought that the best solution is to store the latest commits from svn to a log file (which I already do) and search in this file for the .vbp files and then build them. How can I search for the *.vbp wildcard and build these files only?
Below I have the part of code that stores the logs from the client trunk in $outputFileClientCustom2 and builds $factory, but I want factory to be take from the XML that I have the last commits stored.
$clientTrunk | % {
svn log -l 1 -v --xml $_ 2>&1 |
ft -AutoSize -Wrap |
Out-File $outputFileClientCustom2
}
& Get-ChildItem $factory -Include *.vbp -recurse | foreach ($_) {
Write-Host 'Building ------->' $_.FullName;
& $vb6 /out $outputFileClient $_.FullName /make | Out-Null
}

Related

powershell and windows explorer (properties) have different count results

I'm recursively counting total number of objects (files, folders, etc) to check folders vs their Amazon S3 backups.
When I use windows explorer on a folder (right click --> properties), I get a smaller number of total objects than what the following powershell code generates. Why?
Amazon S3 matches the count from Windows Explorer 100% of the time. Why is powershell giving a higher total number, and what is the likely difference (system files, hidden files, etc)? The total number of objects in these folders is routinely 77,000+.
folder_name; Get-ChildItem -Recurse | Measure-Object | %{$_.count}
I was unable to replicate.
When in file explorer, right-click the folder in question -> properties
Under the General tab, there's a section called Contains.
This lists both the Files and Folders as separate numbers.
In my example I have 19,267 Files, 1,163 Folders which is a total of 20,430 objects
When I run
Get-ChildItem -Path C:\folder -Recurse | measure | % Count
it returns 20430
When I run
Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $false} | measure | % count
it returns 19267
When I run
Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $true} | measure | % count
it returns 1163
Are you certain that you're counting both the files and folders when manually viewing the properties?
The discrepancy comes from Windows Explorer counting files, and separately, folders. Powershell (version 2.0 Build 6.1) is counting everything together. It seems -File and -Directory don't work in PowerShell V2.0.
I really want to be able to get a list as a .cvs or .txt output of just the number of files (recursively) from a large number of folders. Going through windows explorer is one by one, and I don't get this as an output that I can copy/paste.
To count the number of files and folders in separate variables, you can do
# create two variables for the count
[int64]$totalFolders, [int64]$totalFiles = 0
# loop over the folders in the path
Get-ChildItem -Path 'ThePath' -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
if ($_.PSIsContainer) { $totalFolders++ } else { $totalFiles++ }
}
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"
The -Force switch makes sure also hidden and system files are counted.
A probably faster alternative is to use robocopy:
$roboCount = robocopy 'ThePath' 'NoDestination' /L /E /BYTES
$totalFolders = #($roboCount -match 'New Dir').Count - 1 # the rootfolder is also counted
$totalFiles = #($roboCount -match 'New File').Count
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"

Creating Zip files using PowerShell

I have these below files at a location C:\Desktop\Mobile.
Apple_iphone6.dat
Apple_iphone7.dat
Samsung_edge7.dat
Samsung_galaxy.dat
Sony_experia.dat
Sony_M2.dat
I need to create a script that writes the similar files into a single zip. So files Apple_iphone6.dat and Apple_iphone7.dat must be into single zip.
So the final zip files created would be:
Apple_Files_Timestamp.zip
Samsung_Files_Timestamp.zip
Sony_Files_Timestamp.zip
I tried this
Get-ChildItem C:\Desktop\Mobile -Recurse -File -Include *.dat | Where-Object { $_.LastWriteTime -lt $date } | Compress-Archive -DestinationPath C:\Desktop\Mobile
But it gives me error 'Compress-Archive' is not recognized as the name of a cmdlet.
How can I get this code work?
You have two problems, I will try to summarize both of them.
1. Compress files
In order to use Compress-Archive command you need to have PowerShell 5 as already commented by #LotPings. You can:
run your script on Windows 10 machine, or Server 2016 which are coming with v5
download and install PoSh 5, see details on MSDN
If you cannot do either of those, you can
install some module from PowerShell gallery that provides similar functionality via 7-zip tool. Search resultes are here. Download and check those modules before use!
use .NET 4.5 class, check answer here on Stack Overflow
2. Group files
Once you group files, you can easily pipe them to compressing command, similar as you already tried. Proper grouping would be achieved with something like this:
$Files = Get-ChildItem 'C:\Desktop\Mobile'
$Groups = $Files | ForEach-Object {($_.Name).split('_')[0]} | Select-Object -Unique
foreach ($Group in $Groups) {
$Files | where Name -Match "^$Group" | Compress-Archive "C:\Desktop\Mobile\$Group.7z"
}
Pre Powershell v5 you can use this. No additional downloads needed.
$FullName = "Path\FileName"
$Name = CompressedFileName
$ZipFile = "Path\ZipFileName"
$Zip = [System.IO.Compression.ZipFile]::Open($ZipFile,'Update')
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Zip,$FullName,$Name,"optimal")
$Zip.Dispose()
With Powershell 2.0 you can't use Compress-Archive, you need download the original terminal executables to zip and unzip files from here.
You can use:
zip <path> <zip_name> -i <pattern_files>
In your example:
zip "C:\Desktop\Mobile" Apple_Files_Timestamp.zip -i Apple*.dat
zip "C:\Desktop\Mobile" Samsung_Files_Timestamp.zip -i Samsung*.dat
zip "C:\Desktop\Mobile" Sony_Files_Timestamp.zip -i Sony*.dat
If you need use adittional zip options, visit zip manual.
The following script does the grouping,
the zipping command depends on your chosen zipper.
$TimeStamp = Get-Date -Format "yyyyMMddhhmmss"
Get-ChildItem *.dat|
Group-Object {($_.Name).split('_')[0]}|
ForEach-Object {
$Make = $_.Name
Foreach($File in $_.Group){
"{0,20} --> {1}_Files_{2}.zip" -f $File.Name,$Make,$TimeStamp
}
}
Sample output:
> .\SO_44030884.ps1
Samsung_edge7.dat --> Samsung_Files_20170517081753.zip
Samsung_galaxy.dat --> Samsung_Files_20170517081753.zip
Apple_iphone6.dat --> Apple_Files_20170517081753.zip
Apple_iphone7.dat --> Apple_Files_20170517081753.zip
Sony_M2.dat --> Sony_Files_20170517081753.zip
Sony_experia.dat --> Sony_Files_20170517081753.zip
This link might help Module to Synchronously Zip and Unzip using PowerShell 2.0

PowerShell for-each cleanup

I know PowerShell is up to v5, but as I am new to PowerShell, I've been looking through Stack Overflow to generate the script I have. I've found that I need a generic non-version specific way of accomplishing this process...
Here is the issue - Step 1 - I'm pulling application installation locations information from the registry and am using a temporary file to house the results.
dir "HKLM:\SOFTWARE\Wow6432Node\companyname" | Get-ItemProperty | Select installdir | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt"
This provides me a list of installation directories for the company's software that is installed on a particular machine. I then want to take these results, append *.config to each line, as well as taking these results and appending *.xml to each line, and output to a new text file.
The input for the process would be the contents of the initial results file, and the output file should have each line listed in the first results, added to the final results file, once appended with *.xml, and once appended with *.config.
The net effect I am looking for is the creation of a #file for a 7z command. I am attempting this by using the following -
(Get-Content "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt") -replace '\S+$','$&*.config' | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-config.txt" -Encoding utf8
(Get-Content "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt") -replace '\S+$','$&*.xml' | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-config.txt" -Append -Encoding utf8
However, I'm only getting one line that has *.xml and one line that has *.config appended -
After getting this far, I'm thinking that some for-each loop is needed, but I'm not getting anywhere with what I have tried adapting from here. I'm looking now for some way to combine the three lines into one function, if that is possible, and eliminate the temporary file step in the first command, by reading and outputting in the same step. This would also need to remove the "installdir" and "----------" lines from the output. Anyone have some ideas and maybe examples?
Taken your above command dir "HKLM:\SOFTWARE\Wow6432Node\companyname" | Get-ItemProperty | Select installdir | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt" you could put the result of your query into a variable $result:
$result = dir "HKLM:\SOFTWARE\Wow6432Node\microsoft" | Get-ItemProperty | Select installdir;
From there you can easily loop through the array, skipping empty ones and process the rest of it:
foreach($path in $result.installdir)
{
# skip empty paths
if([string]::IsNullOrWhiteSpace($path)) { continue; }
# now do your processing ...
$path;
}
Is this what you were asking for?

Move a files that have the same name but different extension. Powershell

I am a junior tech and have been tasked to write a short powershell script. The problem is that I have started to learn the PS 5 hours ago - once my boss told that I'm assigned to this task. I'm a bit worried it won't be completed for tomorrow so hope you guys can help me a bit. The task is:
I need to move the files to different folders depending on certain conditions, let me start from the he folder structure:
c:\LostFiles: This folder includes a long list of .mov, .jpg and .png files
c:\Media: This folder includes many subfolders withe media files and projects.
The job is to move files from c:\LostFiles to appropiate folders in c:\Media folder tree if
The name of the file from c:\LostFiles corresponds to a file name in one of the subfolders of the C:\media We must ignore the extension, for example:
C:\LostFiles has these files which we need to move (if possible) : imageFlower.png, videoMarch.mov, danceRock.bmp
C:\Media\Flowers\ has already this files: imageFlower.bmp, imageFlower.mov
imageFlower.png should be moved to this folder (C:\media\Flowers) because there is or there are files with exactly the same base name (extension must be ignored)
Only the files that have corresponding files (the same name) should be moved.
So far I have written this piece of code (I know it is not much but will be updating this code as I am working on it now (2145 GMT time). I know I missing some loops, hey yeah, I am missing a lot!
#This gets all the files from the folder
$orphans = gci -path C:\lostfiles\ -File | Select Basename
#This gets the list of files from all the folders
$Files = gci C:\media\ -Recurse -File | select Fullname
#So we can all the files and we check them 1 by 1
$orphans | ForEach-Object {
#variable that stores the name of the current file
$file = ($_.BaseName)
#path to copy the file, and then search for files with the same name but only take into the accont the base name
$path = $Files | where-object{$_ -eq $file}
#move the current file to the destination
move-item -path $_.fullname -destination $path -whatif
}
You could build a hashtable from the media files, then iterate through the lost files, looking to see if the lost file's name was in the hash. Something like:
# Create a hashtable with key = file basename and value = containing directory
$mediaFiles = #{}
Get-ChildItem -Recurse .\Media | ?{!$_.PsIsContainer} | Select-Object BaseName, DirectoryName |
ForEach-Object { $mediaFiles[$_.BaseName] = $_.DirectoryName }
# Look through lost files and if the lost file exists in the hash, then move it
Get-ChildItem -Recurse .\LostFiles | ?{!$_.PsIsContainer} |
ForEach-Object { if ($mediaFiles.ContainsKey($_.BaseName)) { Move-Item -whatif $_.FullName $mediaFiles[$_.BaseName] } }

Rename first 20 characters of every filename in a file

I am trying to write a script in powershell to remove the first 20 characters of every MP3 filename in a folder, I have created a file 'test.ps' and inserted the powershell code below into it,
gci *.mp3 | rename-item -newname { [string]($_.name).substring(20) }
When I run this file in powershell.exe nothing happens,
Can anyone help? Thanks.
This may get you started. (There are probably much more concise ways, but this works and is readable when you need to maintain it later. :-) )
I created a folder C:\TempFiles, and created the following files in that folder:
TestFile1.txt
TestFile2.txt
TestFile3.txt
TestFile4.txt
(I created them the old-fashioned way, I'm afraid. <g>. I used
for /l %i in (1,1,4) do echo "Testing" > TestFile%i.txt
from an actual command prompt.)
I then opened PowerShell ISE from the start menu, and ran this script. It creates an array ($files), containing only the names of the files, and processes each of them:
cd \TempFiles
$files = gci -name *.txt
foreach ($file in $files) {
$thename = $file.substring(4);
rename-item -path c:\TempFiles\$file -newname $thename
}
This left the folder containing:
File1.Txt
File2.Txt
File3.Txt
File4.Txt
File5.Txt
In order to run a script from the command line, you need to change some default Windows security settings. You can find out about them by using PowerShell ISE's help file (from the menu) and searching for about_scripts or by executing help about_scripts from the ISE prompt. See the sub-section How To Run A Script in the help file (it's much easier to read).
Your code actually works. Two things...
Rename the file to test.ps1.
Run it in the folder you have your MP3 files in. Since you didn't provided a path to Get-ChildItem it will run in the current directory.
I tested your line by making a bunch of mp3 files like this -
1..30 | % { new-item -itemtype file -Name (
$_.ToString().PadLeft(30, 'A') + ".mp3" )}
I would use a more "safer" way (you'll get an error if the file name is shorter than the length in question, you are also targeting the file extension as a part of the total characters). Check if the base name of each file is greater than 21 characters (if you remove the first 20 it can be still have a name with one character long). It can fail if the directory contains a file with same name after you removed the first 20, you can develop it further on your own):
gci *.mp3 | foreach{
if($_.BaseName.Length -ge 21)
{
$ext = $_.Extension
$BaseName = $_.BaseName.Substring(20)
Rename-Item $_ -NewName "$BaseName$ext"
}
}
// delete (replace with empty char) first 20 charters in all filename witch is started with "dbo."
// powershell
Get-ChildItem C:\my_dir\dbo -Recurse -Force -Filter dbo.* | Where-Object {!$_.PSIsContainer} | Rename-Item -NewName { ($_.name).Substring(20) }