remove some character string from multiple filenames - replace

my OS is Win 10. i have a folder which contains files like: '1.JPG.JPG.jpg' or '99.JPG.jpg' or '335.JPG.JPG.jpg' -almost 470 files-
notice that the file names are look like value of ID column to reference and some files have 2 times '.JPG' but some have 1 time. also there are some files contains 'jpg' instead of 'JPG' (both without quotes) in between file name.
i want to rename all files with number value in start of file name and then add .jpg to all files like 1.jpg or 99.jpg or 335.jpg etc. this is sure all files are jpeg, there is no .png or .bmp etc.
please help how i can do this?
EDIT: can i used to get digit part of file name and hard code .jpg and replace all file names at once using script, if yes, please guide how it can be done?

its done using following Powershell commands searched from internet:
step 1) ls | Rename-Item -NewName {$_.name -replace ".JPG.JPG",""}
step 2) ls | Rename-Item -NewName { [io.path]::ChangeExtension($_.name, "jpg") }
hope peoples like me (the novice) will get benefit.
Link: https://www.windowscentral.com/how-rename-multiple-files-bulk-windows-10#:~:text=You%20can%20press%20and%20hold,file%20to%20select%20a%20group.

Related

Scanning folder to find all files with minimum matching words (UNIX)

Having some trouble figuring out the command line to the following issue and hoping u guys can help!
Basically, I have a folder which contains a ~1000 PDF's. I need to search through every pdf and return the file names of PDF's that match certain words X amount of times.
For example, I have 10 PDF's which all contain the word "Fragile". I would like to return a list of all files that contain "Fragile" a minimum of 3 times throughout the PDF.
I am currently using pdfgrep and giving it a regex to look for, but it will return all the files that match at least once. I have seen a few recommendations out there piping the command with "awk", but i'm not sure what this really does...
Don't know much about pdfgrep, but if the output is like on https://pdfgrep.org/ it should be fairly easy to get the number of the lines in the output, doing something like:
for f in *.pdf; do if [ $(pdfgrep -nHm 10 "Fragile" "$f" | wc -l) -gt 2 ]; then echo $f; fi; done

Removing white spaces within a file name after a specific text using sed

I have few files, having files name in the below form:
export_<directory_name>_system.indexes .json
Here the directory keeps changing,so it could be anything
However _system.indexes is constant
Notice the space between 'indexes and '.json'.
I want to get rid of the space in between them, so the expected file name is as below
export_<directory_name>_system.indexes.json
I have tried the below:
echo export_direcotoryName_system.indexes .json | sed 's/\s\+//'
All though it does remove the space..but it also removes space from other files which i do not want.
How can restrict the removal only to files having system.indexes in their filenames
With prename:
prename -n 's/system.indexes .json/system.indexes.json/' *.json
if everythink looks fine, remove -n.

How to search files in windows file explorer with specified extension name?

We can search files in windows 7 or higher version using the following tool:
(I don't have image uploading privilage. I mean the top-right area in windows file explorer.)
When I search for MATLAB files using "*.m", it not only returns *.m files, but also returns *.mp3, *.mp4 files. Is there any way to show *.m files exclusively?
Thanks!
I assume you used the quotation marks here to show the text you typed, because ironically the exact way how it should work is to put the search in quotation marks...
so
*.m
finds .mp3 as well as .m but
"*.m"
should only find the .m files. Alternatively you could also write
ext:".m"
which would guarantee that only extensions are searched. (Although I am not sure if this is ever necessary here, because while windows can have a dot in the filename and also can have files without extensions I am not sure if it is possible to have both at the same time.)
using the following
"*.m"
will solve your problem.You can find more information on regex to be used in msdn in the following link .Advanced query syntax
Above that, you can also take advantage of the wildcard character *.
For example, if you want to search for a file with a name ending with 024 or starting with 024 then you can put in the search box like *024.* or 024*.* respectively.
Here the * after . represents files with any extensions, if you want particular then mention extension line 024.png.
Explorer don't have a function of finding with RegEx.
You need to use Power-Shell instead of Win Explorer;
for example: where '(?i)Out' is a regex
Get-ChildItem -Path e:\temp -Recurse -File | Where-Object { $_.Name -match '(?i)Out' }
alternatively you can just simply search for your extension like this:
.extension
eg:
typing .exe will give you all the files with .exe extensions in a folder.
PS: Typing .xml OR .vmcx will give you both type of files. It is useful if you seek to make an archive of different kinds of files stored in different folders or locations.
You can get close to proper regex support from the mostly awesome Cygwin, and as a bonus you get most every linux tool running natively on linux. But it still doesnn't know that .* means "zero or more of anything", ^ means the start of a line (and $ the end), so some things are still weird.
And a startlingly large bunch of weird corner cases that only deranged perl programmers notice fail the test.
So many other things it gets wrong, but it's more workable than anything in any windows OS, plus you get perl, grep, diff, wget, curl, etc. -- the whole GNU lib for free.
If you want a full on bash shell with proper respect for regex, install the super neet-o Bash for Windows 10
Either will do what you want. And they're a billion times faster than that stupid search bar that takes off at 100 mph then crawls to 1 pixel per 10 minutes near the end.

regex help for replacing directories under certain conditions

I need to write a regex to update some registry files for work for an upcoming migration
I have to dynamically update all strings in files where:
has a directory path that starts with C:
directory does not have "xyz" or "abc" in the path
if the string in the file contains a file [EX *.ext], the only thing that is updated is the directory path and not the filename & extension so those are left alone
does not replace but instead keeps any appending information in the string [EX: " " " or ")"] that are used to close out the string parameters in the files
I have got it doing 1 and 2, but not 3 and 4. I'm using PowerShell 4.0, recursively checking files in a directory and producing a new file with the changes for each file found in each subdirectory.
The conversion should look like:
FROM: setting1="\"C:\\\\this\\\\is\\\\\adirectory\""
TO: setting1="\"O:\\\\\New_Directory_Path\""
FROM: othersetting="\"C:\\\\this\\\\abc\\\\directory""
TO: UNCHANGED
FROM: thisfile="\"(C:\\\\this\\\\directory\\\\somefile.some_ext)""
TO: thisfile="\"(O:\\\\New_Directory_Path\\\\somefile.some_ext)""
I'm at my wits end on this one, this is what I have so far, in terms of regex:
gc $file | % {$_ -replace "C:(?!.*abc.*)(?!.*xyz.*)(?!.*\..*).*","O:\\\\New_Directory_Path\"} | out-file "$filePath\$newFileName"
Hoping someone on here can lend a hand. Also, this is my first time posting on here, sorry for not putting my code in tags.
try this pattern
Edited:
C:(?!.*(abc|xyz)).*?(?=[^\\]+\.[^\\]+|$)
Demo

how to remove lines from file that don't match regex?

I have a big file that looks like this:
7f0c41d6-f9c6-47aa-a034-d40bc629c973.csv
159890
159891
24faaed6-62ee-4175-8430-5d73b09911c8.csv
159907
5bad221f-25ef-44fa-9086-fd152e697928.csv
642e4ac3-3d46-4b4c-b5c8-aa2fa54d0b04.csv
d0e145a5-ceb8-4d4b-ae47-11e0c9a6548d.csv
159929
ba678cbd-af57-493b-a69e-e7504b4bc328.csv
7750840f-9bf9-4a68-9f25-a2ba0968d481.csv
159955
159959
And I'm only interesting in *.csv files, can someone point me how to remove files that do not end with .csv.
Thank you.
grep "\.csv$" file
will pull out only those lines ending in .csv
Then if you want to put them in a different file;
grep "\.csv$" file > newfile
sed is your friend:
sed -i.bak '/\.csv$/!d' file
-i.bak : in-place edit. creates backup file with .bak extension
([0-9a-zA-Z-]*.csv$)
This is the regex code that only select the filename ending with .csv extensions.
Hope this will help you.
If you are familiar with the vim text editor (vim or vi is typically installed on many linux boxes), use the following vim Ex mode command to remove lines that don't match a particular pattern:
:v/<pattern>/d
For example, if I wanted to delete all lines that didn't contain "column" I would run:
:v/"column"/d
Hope this helps.
If it is the case that you do not want to have to save the names of files in another file just to remove unwanted files, then this may also be an added solution for your needs (understanding that this is an old question).
This single line for loop using the grep "\.csv" file solution recursively so you don't need to manage multiple files names being saved here or there.
for f in *; do if [ ! "$(echo ${f} | grep -Eo '.csv')" == ".csv" ]; then rm "${f}"; fi; done
As a visual aid to show you that it works as intended (for removing all files except csv files) here is a quick and dirty screenshot showing the results using your sample output.
And here is a slightly shorter version of the single line command:
for f in *; do if [ ! "$(echo ${f} | grep -o '.csv')" ]; then rm "${f}"; fi; done
And here is it's sample output using your sample's csv file names and some randomly generated text files.
The purpose for using such a loop with a conditional is to guarantee you only rid yourself of the files you want gone (the non-csv files) and only in the current working directory without parsing the ls command.
Hopefully this helps you and anyone else that is looking for a similar solution.