Looking for a way to delete files in a directory depending on their extension's case [closed] - powershell

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I would love your help with a tool that would greatly simplify my life. As of now I have no scripting knowledge. I am not sure which of powershell or a .bat is the best suited to my need :
I have a folder with thousands of little files with various extension names. Sometimes I need to delete all the files that have their extension written in uppercase, sometimes the ones in lowercase.
I need a script that would ask me in input if I want to delete Upper or Lowercase files and then would go deleting all the files in the folder with an extension in Upper or Lowercase (based on the input).
I have no idea where to begin. Could you please share some ideas ?
Thanks for your time

One way would be to use the -match regular expression operator. Since PowerShell operators always defaults to case-insensitive string comparison, we'll need to qualify it with a c, so -cmatch. To match only upper-case letters, I usually use the regex class \p{Lu} (Lu stands for "Letters, upper"):
$allUppercase = Get-ChildItem -Path C:\path\to\folder\ |Where-Object {$_.Extension -cmatch '^.\p{Lu}+$'}
and now you can delete them with Remove-Item:
$allUppercase |Remove-Item -Force
if you want extensions with lowercase only, test for \p{Ll}:
$allLowercase = Get-ChildItem -Path C:\path\to\folder\ |Where-Object {$_.Extension -cmatch '^.\p{Ll}+$'}

Related

How to reset my PATH after breaking it accidentally? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I think I run something incorrectly trying to add a directory to PATH in fish. Perhaps it was this:
set -g PATH my_foobar_directory "$PATH"
From fish tutorial I now understand that I shouldn't have added the double-quotes.
Better yet, should've used fish_add_path my_foobar_directory.
Lesson learned; however, the change has persisted somewhere, and nothing I try seems to recover the previous state. I also cannot find the previous PATH value — the console logs with it were washed away by copious fish: Unknown command: python etc, from fish_prompt bells & whistles.
Falling back to bash gives me bogus PATH as well — even after set -e PATH.
What do? How do I start over?
So for myself, I solved it like this.
In the process tree, I found a sufficiently long-running process. In my case, cinnamon-session worked — though any not-so-distant fish parent would do.
The idea being that in that process's environment, the previous PATH value could still be intact. It was.
Then basically — let's say the pid was 661 — print environment of pid 661 in fish format:
/bin/tr \0 ' ' < /proc/661/environ
# copy output
Then just pick that output, and feed it into the "universal" variant (fish-specific) of the PATH variable, taking care to erase all other variants:
set -e PATH
set -eg PATH
set -Ux PATH <paste>

Change directory to usb drive without using variables [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a problem to cd into my usb drive (F:) using PowerShell and I can't use variables becouse I am starting PowerShell with Start-Process. I tried using
cd (Get-Volume -FileSystemLabel NameOfUsb).DriveLetter:
and it almost works but "F" has a space after it and ":" is in new line so it spits out error.
Is there any way to do it? It don't have to be using that method
You really should include the full Start-Process line you are trying to run. However, this should do what you want.
cd "$((Get-Volume -FileSystemLabel NameOfUsb).DriveLetter):\"

Can you restore a word document from 97-2003 only using its Compound File streams? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I am trying to recover a word document that was using 97-2003 version. I do not remember why I did this, but I saved off the Compound File streams into a folder, and it is definitely something I would like to have back as the original document. Is there any solution for this?
Thanks!
Sure, you should theoretically be able to restore it.
You should be able to create a root compound document using
StgCreateDocfile() // or StgCreateStorageEx()
You will need the IStorage* pointer from this if it succeeds.
If you have a folder in your folder, you are going to have to create sub storages for each folder--and do it recursively. The API is IStorage::CreateStorage() ... look it up.
If you have a file in a folder, then a stream needs to be created in the storage that is equivalent to the folder. To create a stream, use IStorage::CreateStream()...look up arguments.
Looking at your screen shot, it has some streams named something like [1]CompObj or [5]SummaryDocumentInformation. For the [n] part, that is probably the equivalent of _T('\00n') where n is 1 or 5 or whatever--it's probably a control character. I've seen that in compound files. If you want to investigate, create a Word 97-2003 document and save as a .doc file and examine the structure.
So, something like [1]CompObj is really _T("\001CompObj")
The stuff above about [n] in your file names is an educated/experienced guess.

How do I extract the first x lines from a large CSV file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
How do I extract the first 100,000 lines from a large CSV file (1GB+) using only Powershell on a Windows machine?
Not sure why the below answer was marked as not helpful when it answers the question.
-TotalCount<Int64>
Gets the specified number of lines from the beginning of a file or other item. The default is -1 (all lines).
You can use the "TotalCount" parameter name or its aliases, "First" or "Head".
The performance of the command can be improved by
Get-Content -TotalCount 100000 -ReadCount 0 filename.csv
Get-Content -TotalCount 100000 filename.csv

Form fields validation: which characters to include / exclude? [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 2 years ago.
Improve this question
A google search gave me the methods to validate form fields, but I can already construct them. My question is, which are the characters that are safe to include and which are to exclude in a form field? Specifically, username and password.
A brief explanation would be nice too.
Thanks.
You need to exclude all characters you will never have inside you datas.
Do you think it would be any sense to have special characters if your usernames/passwords must only contains alphanumeric characters ?
Look at some REGEX for JAVA or for PHP.
There is a regexp reference table which could be usefull too.
If you give us more information about the langage you are using, we could maybe help us more.
Have a good day!
[UPDATE]
There is the security reference which is very good and the OWASP website which is a real reference for any web security related topics, look at the OWASP Cheat Sheets.
#**Cross-Site Scripting Vulnerabilities?**
#for any programming language, the chars you should reject or handle properly are:
> < ( ) [ ] ' " ; : / |
#for PHP, tools to handle with care:
strip_tags(), utf8_decode(), htmlspecialchars(), strtr()
#do Positive/Negative filtering
#check Encoding
#**SQL Injection ?**
#etc...
[/UPDATE]
If you properly sanitize your input and output, there's nothing you need to be afraid of.
Note: I'm assuming you're using PHP as your server side language.
First, use PDO (or MySQLi) with prepared statements, to eliminate the risk of SQL Injection.
Second, anything that will be displayed on your site should be sanitized against XSS attacks (so that users don't register a username of <script>doSomeEvilStuff()</script>).
That's it basically, if you're really paranoid, you should be using a whitelist (to only allow certain characters) and not a blacklist (to only disallow certain characters), since someone will always find a way to bypass a blacklist, but no one can bypass a whitelist.
For usernames, I don't see the need for anything more than /[a-zA-Z0-9_.\s!$%^&*\-+=]/ You may think otherwise. In any case, don't allow /[`<>(){}[]]/