Command Line - Remove in folder with regex windows - regex

Objective
How can I remove all files within a folder (recursively) with regex on File Name?
From Windows 10, I can use PowerShell or git console with unix commands.
Required points
NOT Matches /.+?-dbg.*?\.js/
For a file to be matched it's required to match a "brother file" with the previous point pattern.
Example Test Folder contains
ActionMode.js
ActionMode-dbg.js
Component.js
Component-dbg.js
DualContribution.controller.js
DualContribution-dbg.controller.js
resources.js
manifest-json
Files that would be deleted
ActionMode.js
Component.js
DualContribution.controller.js
If you need more information please ask me. Thanks.

I don't know whether this really helps you, but here is a quick solution.
This Autoit code prints all files to console that you want to delete.
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
$allFiles = _FileListToArrayRec(#ScriptDir, '*.js', $FLTAR_FILES, $FLTAR_RECUR , $FLTAR_SORT, $FLTAR_FULLPATH )
If #error Then ConsoleWrite(#error & #crlf)
_ArrayDisplay($allFiles, "Sorted tree")
For $i = 0 to UBound($allFiles) -1
If StringInStr($allFiles[$i], '-dbg') <> 0 Then
If StringReplace($allFiles[$i], '-dbg', '') == $allFiles[$i-1] Then ConsoleWrite('Delete file: ' & $allFiles[$i-1] & #CRLF)
EndIf
Next

Related

Rename multiple file with special characters in Fedora

I have several hundred files in the below format:
'[animalpics.com][977]bluejays.png'
'[animalpics.com][9]lions.jpg'
'[animalpics.com][99]colts.jpg'
I would like to rename all the files to just the description and file type:
bluejays.png
lions.jpg
colts.jpg
I have tried rename but it doesn't seem to like the command I'm using:
rename 's/[animalpics.com][*]//g' *.*
How can I edit my command to rename these files? I also have install mmv but it's not as intuitive as I thought.
rename 's/^.*\]//' *
Demo:
$ls -1
'[animalpics.com][977]bluejays.png'
'[animalpics.com][99]colts.jpg'
'[animalpics.com][9]lions.jpg'
file.txt
$rename 's/^.*\]//' *
$ls -1
bluejays.png
colts.jpg
file.txt
lions.jpg
$

Change a constant variable without rebuilding C++

I've developed a c++ project with visual studio 2015. The output of my project is a single executable that must have a unique ID for every client and this ID must be accessible inside the code. A simple approach is to just define a constant variable inside the code and change its value for every client and build it many times but I have a Linux server and I'm not sure I can build it simply because I've used many Winapi libraries. I was thinking that maybe there is another way to change or add some constant value to the output like manipulating the executable.
For example:
#include <string>
#include <iostream>
#include <Windows.h>
const std::string ID = "some unique ID";
int main() {
std::cout << "Your ID: " << ID << std::endl;
getchar();
return(0);
}
It seems that there are only two approaches. One is just building the project inside a Linux environment which is a better method but must be used some tools like Mono XBuild link here.
Another option which may be simpler is just open the binary file and manipulate the specific string. As #aloMalbarez comment Here is a simple script based on this.
Suppose this example: (I used 50 ms as a fixed length for my ID)
#include <string>
#include <iostream>
#include <Windows.h>
#define ID "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
using namespace std;
int main() {
cout << "Your ID: " << ID << "\nlen:" << strlen(ID) << endl;
getchar();
return(0);
}
After generating the executable use the following script to create output.
I'm not a Linux guy so you can help me improve this.
./build.sh input.exe output.exe "myfixedID"
#!/bin/bash
# build.sh input_file output_file <ID>
input_file=$1
output_file=$2
ID=$3
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "wrong parameters"
echo "build.sh input_file output_file <ID>"
exit 1
fi
# use fixed string (ID) in the source file
# this creates 50 of "m"s
search_value=$(printf 'm%.0s' {1..50})
extension=".back"
temp_file="$input_file$extension"
tmpstring_file="./tmp"
null_termin='\0'
echo "copying the original file..."
yes | cp -rf $input_file $temp_file
address=$(strings -t d $temp_file | grep $search_value | grep -o '[0-9]*')
echo "Address:"
echo $address
if ! [[ $address =~ ^[0-9]+$ ]]; then
echo "cannot find valid ID in executable"
echo "removing temps"
rm $temp_file
exit 1
fi
# make the tempstring file
printf "$ID$null_termin" > $tmpstring_file
dd if=$tmpstring_file of=$temp_file obs=1 seek=$address conv=notrunc
echo "make new file"
yes | cp -rf $temp_file $output_file
echo "removing temps"
rm $temp_file $tmpstring_file
echo "Done!"
In init function of your program. Generate a unique id based of SHA-1
hash of current time, IP address, username (same more). you can do whatever you want to do in that program afterward (i.e save in database). Will that work ?
A Constant is not a Variable, they are opposite types. A Constant is an element that is assigned a particular value that does not change, thus the word constant, unchanging.
A Variable on the other hand is an element that is stored in memory as a changeable value, as your program runs, the VARIABLE can change it's current value.
In Visual Studio, you can create a configuration file that passes set values to your program, these values are variable, and can be changed programmatically and manually. But, as you stated you do not want a separate file to look up information from.
If you want to track users by their Unique ID, then you must have a database somewhere that can record new users and issue a unique ID, or, you can create a unique ID based on the date and time that the account is created, if you are creating a unique executable for each one, the date/time information is included in the file creation information, so you would simply use that, since every file is created at a unique date/time, that would always indicate the ID. You could keep the same name for each file, or incorporate the date/time into the filename, like myPro20180522183231.exe which would be year 2018 month 05 day 22 hour 18 minutes 32 seconds 31 and this could be confirmed through the date/time information of the file creation data.

Can't enable phar writing

I am actually using wamp 2.5 with PHP 5.5.12 and when I try to create a phar file it returns me the following message :
Uncaught exception 'UnexpectedValueException' with message 'creating archive "..." disabled by the php.ini setting phar.readonly'
even if I turn to off the phar.readonly option in php.ini.
So how can I enable the creation of phar files ?
I had this same problem and pieced together from info on this thread, here's what I did in over-simplified explanation:
in my PHP code that's generating this error, I added echo phpinfo(); (which displays a large table with all sort of PHP info) and in the first few rows verify the path of the php.ini file to make sure you're editing the correct php.ini.
locate on the phpinfo() table where it says phar.readonly and note that it is On.
open the php.ini file from step 1 and search for phar.readonly. Mine is on line 995 and reads ;phar.readonly = On
Change this line to phar.readonly = Off. Be sure that there is no semi-colon at the beginning of the line.
Restart your server
Confirm that you're phar project is now working as expected, and/or search on the phpinfo()table again to see that the phar.readonly setting has changed.
phar.readonly can only be disabled in php.ini due to security reasons.
If you want to check that it's is really not done using other method than php.ini then in terminal type this:-
$ php -r "ini_set('phar.readonly',0);print(ini_get('phar.readonly'));"
If it will give you 1 means phar.readonly is On.
More on phar.configuration
Need to disable in php.ini file
Type which php
Gives a different output depending on machine e.g.
/c/Apps/php/php-7.2.11/php
Then open the path given not the php file.
E.g. /c/Apps/php/php-7.2.11
Edit the php.ini file
could do
vi C:\Apps\php\php-7.2.11\php.ini
code C:\Apps\php\php-7.2.11\php.ini
[Phar]
; http://php.net/phar.readonly
phar.readonly = Off
; http://php.net/phar.require-hash
phar.require_hash = Off
Save
Using php-cli and a hashbang, we can set it on the fly without messing with the ini file.
testphar.php
#!/usr/bin/php -d phar.readonly=0
<?php
print(ini_get('phar.readonly')); // Must return 0
// make sure it doesn't exist
#unlink('brandnewphar.phar');
try {
$p = new Phar(dirname(__FILE__) . '/brandnewphar.phar', 0, 'brandnewphar.phar');
} catch (Exception $e) {
echo 'Could not create phar:', $e;
}
echo 'The new phar has ' . $p->count() . " entries\n";
$p->startBuffering();
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file2.txt']->compress(Phar::GZ);
$p['file3.txt'] = 'babyface';
$p['file3.txt']->setMetadata(42);
$p->setStub('<?php
function __autoload($class)
{
include "phar://myphar.phar/" . str_replace("_", "/", $class) . ".php";
}
Phar::mapPhar("myphar.phar");
include "phar://myphar.phar/startup.php";
__HALT_COMPILER();');
$p->stopBuffering();
// Test
$m = file_get_contents("phar://brandnewphar.phar/file2.txt");
$m = explode("\n",$m);
var_dump($m);
/* Output:
* there
**/
✓ Must be set executable:
chmod +x testphar.php
✓ Must be called like this:
./testphar.php
// OUTPUT there
⚠️ Must not be called like this:
php testphar.php
// Exception, phar is read only...
⚠️ Won't work called from a CGI web server
php -S localhost:8785 testphar.php
// Exception, phar is read only...
For anyone who has changed the php.ini file, but just doesn't see any changes. Try to use the CLI version of the file. For me, it was in /etc/php/7.4/cli/php.ini
Quick Solution!
Check:
cat /etc/php/7.4/apache2/php.ini | grep phar.readonly
Fix:
sed -i 's/;phar.readonly = On/;phar.readonly = Off/g' /etc/php/7.4/apache2/php.ini

How to make files optional include in MainSection of NSIS installer

MainSection of .nsi file contains the files name which are bundled along with the installer.
I need to make a file which should not get bundled when install type equals normal and that file should get bundled when type equals costume.
Section "MainSection" SEC01
- SetOutPath "$INSTDIR"
- SetOverwrite ifnewer
* if (installtype==custom)
* File "IncludeThisFile"
SectionEnd
How to achieve above in nsis.help is much appreciated!!
You usually just put optional things in another section but you can also do what you want:
!include LogicLib.nsh
!include FileFunc.nsh
var IsSpecialMode
Function .onInit
StrCpy $IsSpecialMode 0
${GetParameters} $0
ClearErrors
${GetOptions} $0 "/includespecial" $1
${IfNotThen} ${Errors} ${|} StrCpy $IsSpecialMode 1 ${|}
FunctionEnd
Page InstFiles
Section
SetOutPath "$instdir"
${If} $IsSpecialMode <> 0
File "${__FILE__}"
${EndIf}
SectionEnd
..and then run MySetup.exe /includespecial

Dynamically determine all solutions, projects and configurations on a build/trigger event and call them all with MSBuild from command line/script

We're still a little early in setting up our build automation and just using a bat file for now for Win32 C++ solutions. We have about 4 solutions and each has a couple vcproj files.
Each time a new solution or configuration is added I have to update the bat file to reflect the new solution or configuration to call with MSBuild.
I thought perhaps it would be easier to write a tool that parses all the sln files in a given path (and its subtree) and then parse all project files it references for configurations and then call all the builds that way.
Is there an easy way to do this?
Really this is 2 questions:
How can I tell MSBuild just to build all solutions inside a subtree? (I can do a search on them - that is a simple tool I think to write)
How can I tell MSBuild to build all configurations of a solution/vcproj?
We're using MSBuild, bat files, vc2008, c++
Being a PowerShell fan the following reformatted one-liner might be of help:
Get-ChildItem -Recurse -Include *.sln |
ForEach-Object {
$Solution = $_.FullName
Get-Content $_ |
ForEach-Object {
if($_ -match '\{[^\}]+[^=]+= ([^\{\s]*)$') {
$matches[1]
}
} | Sort-Object -Unique |
ForEach-Object {
$config = ([string]$_).Split([char]'|')
& "$env:windir\Microsoft.NET\Framework\v3.5\msbuild.exe" $Solution /p:Configuration="$($config[0])" /p:Platform="$($config[1])"
}
}
This script can be saved as a ps1 file or pasted as a function in your profile. To explain what it does:
find all .sln files in and below the current directory
parse the sln extracting the configuration and platform values
for each unique configuration platform combination call msbuild with the given solution
--edit: Forgot Split-String is part of PSCX, it's better to use [string]
I hope this helps. If you still would like to use MsBuild, it does support a recursive form of the file glob operator via the ".\**\*.sln" syntax see here for details. MsBuild also provides the MsBuild task which can be used to build a set of solutions. I do not know how you can get all 'known' configurations from a solution easily in msbuild. Therefore I choose the PowerShell route.
The Microsoft build framework can be accessed via APIs. But i am not sure if one can do it only via a batch file. IMHO the easiest way is to automate the build by writing an addin (using VB.NET perhaps) to the VS IDE. This addin can be invoked from a batch file. We have automated VC++ builds like this using the addin approach. In our case there was no other way because we had to automate the moc process of Qt also. I think it will be more flexible also.
The build system (atleast in VS 2005) that i worked on was quite buggy. Refer to this link, it will help you in understanding the pitfalls and also provides code-snippets on how to traverse the solution files.
Once the addin is installed, a small exe can be created which invokes the build via addin. This exe can inturn be called from a batch file.
Sample VS Automator exe that calls addin methods
''' Summary: Console App to automate MSVS build with special regard to Addin
Imports System
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports System.Reflection
Imports System.Windows.Forms
Imports System.IO
'''<summary>Module class that automates launching MSVS in silent mode. It disables all user actions. A hidden window is used for MSVS automation. This should work for VS 2008 as well.</summary>
'''<remarks>An STA Thread is used for this console app. Refer http://msmvps.com/blogs/carlosq/archive/2007/10/11/frustrations-with-command-line-add-ins-for-visual-studio.aspx for details </remarks>
Module VSAutomaton
Private Enum VisualStudioSolVersion
Unknown = 0
VSNET2002 = 1
VSNET2003 = 2
VS2005 = 3
VS2008 = 4
End Enum
<STAThread()> _
Sub Main(ByVal args() As String)
Const ADDIN_PROGID As String = "AddIn-Name.Connect"
Const ADDIN_METHOD As String = "SyncSolutionBatch" ' name of your method in addin code
Dim dte As EnvDTE.DTE = Nothing
Dim dteType As Type
Dim commandLineAddIn As AddIn-Name.Connect = Nothing
Dim solutionFullFileName As String
Dim solutionFolder As String
Dim solutionName As String
Dim logFullFileName As String
Dim buildLogFile As String = Nothing
Dim buildConfig As String = Nothing
Dim connectObject As Object = Nothing
Dim connectObjectType As Type
Dim version As VisualStudioSolVersion
Dim progID As String
Dim executableName As String
Dim addIn As EnvDTE.AddIn
Dim msgFilter As MessageFilter.MessageFilter = Nothing
Try
msgFilter = New MessageFilter.MessageFilter
If args.Length = 0 Then
executableName = IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly.Location)
ReportError("Usage: " & executableName & " solution_file_name.sln")
Else
solutionFullFileName = args(0) ' project solution file
If Not IO.File.Exists(solutionFullFileName) Then
ReportError("Solution file '" & solutionFullFileName & "' does not exist.")
Else
solutionFolder = IO.Path.GetDirectoryName(solutionFullFileName)
solutionName = IO.Path.GetFileNameWithoutExtension(solutionFullFileName)
logFullFileName = IO.Path.Combine(solutionFolder, solutionName & ".log")
If IO.File.Exists(logFullFileName) Then
IO.File.Delete(logFullFileName)
End If
version = GetSolutionVersion(solutionFullFileName)
If version = VisualStudioSolVersion.Unknown Then
ReportError("The format version of the solution file is not supported.")
Else
progID = GetVisualStudioProgID(version)
dteType = System.Type.GetTypeFromProgID(progID)
If dteType Is Nothing Then
ReportError("Could not find the ActiveX Server for ProgID '" & progID & "'. Likely the proper version of Visual Studio is not installed.")
Else
dte = DirectCast(System.Activator.CreateInstance(dteType), EnvDTE.DTE)
dte.SuppressUI = True
dte.UserControl = False
addIn = GetAddInByProgID(dte, ADDIN_PROGID)
If addIn Is Nothing Then
ReportError("The Add-in " & ADDIN_PROGID & " was not found in Visual Studio.")
Else
addIn.Connected = True
connectObject = addIn.Object
connectObjectType = connectObject.GetType
' So a copy of the same DLL is necessary in the same dir as this app. exe
connectObjectType.InvokeMember(ADDIN_METHOD, Reflection.BindingFlags.InvokeMethod Or Reflection.BindingFlags.Static Or Reflection.BindingFlags.Public, Nothing, connectObject, New String() {solutionFullFileName})
End If
End If
End If
End If
End If
Catch ex As Exception
ReportError(ex.ToString)
Finally
If Not (dte Is Nothing) Then
Try
dte.Quit()
Catch ex As Exception
End Try
End If
If Not (msgFilter Is Nothing) Then
' this is a tricky aspect. We do not want leaks but .NET can sometimes be extra smart
msgFilter.Dispose() 'If the GC decides to re-collect the garbage from this app, then a crash may result
' but this is the drawback of indeterministic destruction semantics
End If
End Try
End Sub
Private Sub ReportError(ByVal msg As String)
#If DEBUG Then
MsgBox(msg)
#End If
Console.WriteLine(msg)
End Sub
Private Function GetAddInByProgID(ByVal dte As EnvDTE.DTE, ByVal addinProgID As String) As EnvDTE.AddIn
Dim addinResult As EnvDTE.AddIn = Nothing
Dim addin As EnvDTE.AddIn
For Each addin In dte.AddIns
If addin.ProgID = addinProgID Then
addinResult = addin
Exit For
End If
Next
Return addinResult
End Function
Private Function GetSolutionVersion(ByVal solutionFullFileName As String) As VisualStudioSolVersion
Dim version As VisualStudioSolVersion = VisualStudioSolVersion.Unknown
Dim solutionStreamReader As IO.StreamReader = Nothing
Dim firstLine As String = Nothing
Dim format As String
Try
solutionStreamReader = New IO.StreamReader(solutionFullFileName)
firstLine = solutionStreamReader.ReadLine()
format = firstLine.Substring(firstLine.LastIndexOf(" ")).Trim
Select Case format
Case "7.00"
version = VisualStudioSolVersion.VSNET2002
Case "8.00"
version = VisualStudioSolVersion.VSNET2003
Case "9.00"
version = VisualStudioSolVersion.VS2005
Case "10.00"
version = VisualStudioSolVersion.VS2008
End Select
Finally
If Not (solutionStreamReader Is Nothing) Then
solutionStreamReader.Close()
End If
End Try
Return version
End Function
Private Function GetVisualStudioProgID(ByVal version As VisualStudioSolVersion) As String
Dim progID As String = ""
Select Case version
Case VisualStudioSolVersion.VSNET2002
progID = "VisualStudio.DTE.7"
Case VisualStudioSolVersion.VSNET2003
progID = "VisualStudio.DTE.7.1"
Case VisualStudioSolVersion.VS2005
progID = "VisualStudio.DTE.8.0"
Case VisualStudioSolVersion.VS2008
progID = "VisualStudio.DTE.9.0"
End Select
Return progID
End Function
End Module
Sample batch file to invike the VS automator exe:
#echo off
:: --Usage: $>BatchFileName.bat "<project_name(.sln)>" {Release | Debug} [ Make ]
:: Please remember the "double-quotes".
REM -- check for blank input
if x%1%x == xx goto InputError
if x%2%x == xx goto InputError
echo Automating MSVS-Build for %1% ...
echo .
set arg1=%1%
REM -- remove quotes
for /f "useback tokens=*" %%a in ('%arg1%') do set match=%%~a
set slnFile=%match:~-4%
if %slnFile% == .sln goto lbl_FileOK
:lbl_FileOK
REM build configuration and output file
set SOLFILE=%1%
set BUILDCONFIG=%2%
set CLEANWSOBJS=%3%
REM -- Read necessary registry entries
REM --- Read MSVS installation dir
regedit /e A$B$C$.bxt "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Setup\VS"
find "VS7CommonDir" <A$B$C$.bxt>A$B$C$.bat
goto :1st
:1st
for /F "tokens=1* delims==" %%A in ('TYPE A$B$C$.bat ^| find "VS7CommonDir"') do set vscomdir=%%B
set vscomdir=%vscomdir:"=%
REM -- Initialize the MSVS environment
set VSENV="%vscomdir%Tools\vsvars32.bat"
call %VSENV% > nul
REM -- remove quotes
for /f "useback tokens=*" %%a in ('%SOLFILE%') do set str=%%~a
set LastFolder=%str%
REM -- Extract the project name
if "%LastFolder:~-1%"=="\" set LastFolder=%LastFolder:~0,-1%
for %%a in ("%LastFolder%") do set LastFolder=%%~nxa
set flname=%LastFolder:.shared=.sln%
set tmpfile=%solPath%\%flname%
REM --- Check if the target '.sln' already exists, if yes delete
if EXIST %NEWSOLFILE% DEL /Q %NEWSOLFILE%
REM -- use the addin functionality
VSAutomator.exe %SOLFILE%
REM --- create log file as projectname_buildconfig.log
set tmplog=%NEWSOLFILE:.sln=%
set OUTLOGFILE=%tmplog%_%BUILDCONFIG%.log
REM -- Now build the newly ready .sln file
echo .
echo Building Solution file - %NEWSOLFILE%, Output log file - %OUTLOGFILE%
echo .
if x%CLEANWSOBJS%x == xMakex goto JustBuild1
devenv.com /useenv %NEWSOLFILE% /CLEAN %BUILDCONFIG% /OUT %OUTLOGFILE% > nul
:JustBuild1
devenv.com /useenv %NEWSOLFILE% /BUILD %BUILDCONFIG% /OUT %OUTLOGFILE% > nul
Please remember that the code presented above may not be perfect as i have referred it from my POC that i did when i had a similar automation problem.