Match all files that are inside a folder and ignore one folder inside - match

I have the following directory structure:
root
├─ files
│ ├─ folder1
│ │ ├─ file1.js
│ | └─ file2.js
│ ├─ folder2
│ │ └─ file3.js
│ ├─ file4.js
| └─ file5.js
└─ config.js
How can I match every file inside of file (and subdirectories) except the files that are in folder1, in this case file3.js, file4.js and file5.js?
I know I could exclude folder1 with the following: files/!(folder1)/*.js, but this only matches file3.js.

Try **/files/{*.js,!(folder1)*/*.js}. You can test using globster.xyz

There is probably a more elegant way to do this as I am not too familiar with glob, but I think this will get what you are asking for.
import glob
exclude_pattern = ['folder1']
file_list = glob.glob('./files/**/*', recursive=True)
for pattern in exclude_pattern:
exclude_patternmatch = list(filter(lambda x: pattern in x, file_list))
for item in exclude_patternmatch:
file_list.remove(item)
print(file_list)
output:
['./files/file6.js', './files/file5.js', './files/folder2/file3.js', './files/folder2/file4.js']

Related

Can't get diff output from NestedStack in AWS CDK

I have an issue when I'm working with NestedStack in AWS CDK, the problem is when I'm using the cdk diff command its returns the diff output return to me.
But when I have a diff inside my NestedStack it's just a reference and i really don't know what it will change inside my NestedStack.
[~] AWS::CloudFormation::Stack IAMPolicyStack.NestedStack/IAMPolicyStack.NestedStackResource IAMPolicyStackNestedStackIAMPolicyStackNestedStackResource4B98A1D2
├─ [~] NestedTemplate
│ └─ [~] .Resources:
│ └─ [~] .CDKMetadata:
│ └─ [~] .Properties:
│ └─ [~] .Analytics:
│ ├─ [-] v2:deflate64:H4sIAAAAAAAA/zPSMzLWM1BMLC/WTU7J1s3JTNKr9kstLklNCS5JTM7WcU7LC0otzi8tSk4FsZ3z81IySzLz82p1AipLMvLz9I31DA30TBSzijMzdYtK80oyc1P1giA0AJZoScZcAAAA
│ └─ [+] v2:deflate64:H4sIAAAAAAAA/zPSMzLWM1BMLC/WTU7J1s3JTNKr9kstLklNCS5JTM7WcU7LC0otzi8tSk4FsZ3z81IySzLz82p1AipLMvLz9I31LPUMjRSzijMzdYtK80oyc1P1giA0ALbtmvJcAAAA
Hope someone out there has hit the same issue as me and has a solution about how to get the diff output from NestedStack.
Updated 30/05/2022
The featuer look like its not ready yet, but its coming in the feature based my ticket here - https://github.com/aws/aws-cdk/issues/20392
This commit adds a workaround where you can run
cdk deploy --no-execute
and then view the nested stack changes in the CloudFormation change set that gets created.
Edit: Looks like this has not been merged yet as of 1.163.1, here is the open issue.

Powershell navigating to unknown directory

I have stumbled the unfortunate situation, having to be in a directory in which another directory is located:
C:\Test\[Folder with unknown name]\theFileINeed.txt
The structure mentioned above originates from a Zip-file from an external source. So i can not change the structure.
My goal is to navigate to the Directory with the unknown name, so it is my working directroy and I can execute further commands there. (Like Get-Childitem e.g.)
Is there a simple way to e.g. use the cd command to move into that directory?
I have fiddled around a bit with Resolve-Path but couldn't find a helpful solution.
Thanks in advance.
Consider this structure:
C:\TMP\TEST
├───unknowndir1
│ │ nonuniquefile.txt
│ │ uniquefile.txt
│ │
│ ├───nonuniquesubdir
│ └───uniquesubdir
└───unknowndir2
│ nonuniquefile.txt
│
└───nonuniquesubdir
You could do cd .\test\*\uniquesubdir but you can't cd .\test\*\nonuniquesubdir as you'll gen an error (...) path (...) resolved to multiple containers. The same error is even with cd .\test\*\uniquesubdir\.. as if it didn't even check for existence of uniquesubdir.
So if you want to enter unknown directory based of a file it contains, you'd have to do something like this: cd (Get-Item .\test\*\uniquefile.txt).DirectoryName.
It will fail if you use nonuniquefile.txt as it again resolves to two different directories. You could enter the first of these directories with cd (Get-Item .\test\*\nonuniquefile.txt).DirectoryName[0] if you don't care which of them you use.

Clickhouse: split output on select

Performing a select on Clickhouse, on a MergeTree table that is loaded from a KafkaEngine table via a Materialized View, a simple select shows output split in groups in the clickhouse-client:
:) select * from customersVisitors;
SELECT * FROM customersVisitors
┌────────day─┬─────────createdAt───┬──────────────────_id─┬───────────mSId─┬───────xId──┬─yId─┐
│ 2018-08-17 │ 2018-08-17 11:42:04 │ 8761310857292948227 │ DV-1811114459 │ 846817 │ 0 │
│ 2018-08-17 │ 2018-08-17 11:42:04 │ 11444873433837702032 │ DV-2164132903 │ 780066 │ 0 │
└────────────┴─────────────────────┴──────────────────────┴────────────────┴────────────┴─────┘
┌────────day─┬─────────createdAt───┬──────────────────_id─┬───────────────────mSId──┬────────xId─┬─yId─┐
│ 2018-08-17 │ 2018-08-17 10:25:11 │ 14403835623731794748 │ DV-07680633204819271839 │ 307597 │ 0 │
└────────────┴─────────────────────┴──────────────────────┴─────────────────────────┴────────────┴─────┘
3 rows in set. Elapsed: 0.013 sec.
Engine is ENGINE = MergeTree(day, (mSId, xId, day), 8192)
Why does the output appear splitted in two groups?
If I'm not mistaken, the output is split when the data came from different blocks, also often it leads to being processed in different threads. If you want to get rid of it, wrap your query in outer select
select * from (...)
MergeTree Engine is designed for faster WRITE and READ operations.
Fater writes are achieved by inserting data in parts and then the data is merged offline into a single part for faster reads.
you can see the data partition the following directory :
ls /var/lib/clickhouse/data/database_name/table_name
If you run the following query, you will find this that the data is now available in a single group and also a new partition is available at the above location :
optimize table MY_TABLE_NAME
Optimize table forces merging of partition, but in usual cases, you can just leave it on Click house .

Prevent stemming of words starting with # in PostgreSQL full text search

Basically, I want to be able to get an exact match (hashtag included) for queries like this:
=#SELECT to_tsvector('english', '#adoption');
to_tsvector
-------------
'adopt':1
Instead, I want for words starting with #, to see:
=#SELECT to_tsvector('english', '#adoption');
to_tsvector
-------------
'#adoption':1
Is this possible with psql full text search?
Before you search or index, you could replace each # character with some other character that you don't use in your texts, but which changes the parser's interpretation:
test=> SELECT alias, lexemes FROM ts_debug('english', '#adoption');
┌───────────┬─────────┐
│ alias │ lexemes │
├───────────┼─────────┤
│ blank │ │
│ asciiword │ {adopt} │
└───────────┴─────────┘
(2 rows)
test=> SELECT alias, lexemes FROM ts_debug('english', '/adoption');
┌───────┬─────────────┐
│ alias │ lexemes │
├───────┼─────────────┤
│ file │ {/adoption} │
└───────┴─────────────┘
(1 row)

How to duplicate folder then rename folder and files inside it in Batch or Powershell to multiple folders [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Let's say I have this folder structure:
02548 //let's call this MASTER FOLDER, folder name are using site_id
|- 1. Master File
|- 02548_MSFI.pdf
|- 2. src
|- 02548_main.cpp
|- 3. Backup
|- alpha.svn
I also have site_id.txt file contains the name of side_id:
02548
03584
05482
07992
05861
What I want to do is to duplicate the MASTER FOLDER to new folders. So, the final result will be something like this:
|-02548 // MASTER FOLDER
| |- 1. Master File
| |- 02548_MSFI.pdf
| |- 2. src
| |- 02548_main.cpp
| |- 3. Backup
| |- alpha.svn
|-03584 //the folder name are taken from the list inside the site_id.txt
| |- 1. Master File
| |- 03584_MSFI.pdf //please notice the prefix of this file's name
| |- 2. src
| |- 03584_main.cpp //please notice the prefix of this file's name
| |- 3. Backup
| |- alpha.svn
|-05482 //the folder name are taken from the list inside the site_id.txt
| |- 1. Master File
| |- 05482_MSFI.pdf //please notice the prefix of this file's name
| |- 2. src
| |- 05482_main.cpp //please notice the prefix of this file's name
| |- 3. Backup
| |- alpha.svn
and so on until all the site_id from site_id.txt are here.
In the real world, site_id.txt will contains more than 1000 of list. So, doing this manually will be very painful.
How do I do this using batch script or powershell?
Don't know how you would do it, but this is how I would do it if I had a job and that task was assigned to me:
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q44093158.txt"
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
XCOPY /s /e "%sourcedir%\t w o\*" "%destdir%\%%a\" >nul
FOR /f "delims=" %%p IN ('dir /s /b "%destdir%\%%a\*#*"') DO (
SET "filename=%%~nxp"
CALL set "filename=%%filename:#=%%a%%"
CALL REN "%%p" "%%filename%%"
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q44093158.txt containing your data for my testing.
Within the source directory, (I actually tested with \t w o appended to ensure it worked with directorynames containing spaces), build the structure including the master files with the branch-number replaced by #
The code simply reads filename1 which contains the branch names one to a line, duplicates the directory structure at the destination, then looks for filenames containing #. With those files, it substitutes the branch name for the # and renames the file.
Done!
This PowerShell script and the file site_id.txt should be placed in the parent folder of 02548
## C:\Test\Copy-Template.ps1
$template = '02548'
foreach ($site in (Get-Content site_id.txt)) {
Get-ChildItem $template -recurse | Foreach-Object {
$NewName = $_.FullName -replace $template,$site
if ($_.PSIsContainer){
if (!(Test-path $NewName)){md $NewName|Out-Null}
} else {
copy $_.FullName $NewName -EA 0
}
}
}
Sample output:
> tree /F
C:.
│ Copy-Template.ps1
│ site_id.txt
│
└───02548
├───1. Master File
│ 02548_MSFI.pdf
│
├───2. src
│ 02548_main.cpp
│
└───3. Backup
alpha.svn
> .\Copy-Template.ps1
> tree /f
C:.
│ Copy-Template.ps1
│ site_id.txt
│
├───02548
│ ├───1. Master File
│ │ 02548_MSFI.pdf
│ │
│ ├───2. src
│ │ 02548_main.cpp
│ │
│ └───3. Backup
│ alpha.svn
│
├───03584
│ ├───1. Master File
│ │ 03584_MSFI.pdf
│ │
│ ├───2. src
│ │ 03584_main.cpp
│ │
│ └───3. Backup
│ alpha.svn
│
├───05482
...
├───05861
...
└───07992
├───1. Master File
│ 07992_MSFI.pdf
│
├───2. src
│ 07992_main.cpp
│
└───3. Backup
alpha.svn
#echo off
setlocal EnableDelayedExpansion
rem Call the subroutine, read data from site_id file
call :DuplicateTree < site_id.txt
goto :EOF
:DuplicateTree
rem Read MasterFolder from first line
set /P "masterFolder="
:nextFolder
rem Read and process next folders until EOF
set /P "nextFolder="
if errorlevel 1 exit /B
rem Duplicate folders first
md "%nextFolder%"
for /R "%masterFolder%" /D %%a in (*) do (
set "folder=%%a"
set "folder=!folder:*%cd%\=!"
md "!folder:%masterFolder%=%nextFolder%!"
)
rem Duplicate files
for /R "%masterFolder%" %%a in (*) do (
set "file=%%a"
set "file=!file:*%cd%\=!"
copy "%%a" "!file:%masterFolder%=%nextFolder%!"
)
goto nextFolder
Place this Batch file in the same folder that contain both site_id.txt file and 02548 Master Folder.