batch changing png image to same as filename
-
Hi guys,
Not sure this is possible - but trying to save myself alot of time… Basically I have 5000 files, and inside each file is a .png link containing a number (1 - 5000) like “1.png” or “2.png” all the way to “5000.png”
but i need to change these to start at “5001.png” to “10000.png”
so 1.png becomes 5001.png
2.png becomes 5002.png
3.png becomes 5003.png and so on…if it helps the filenames are already 5001, 5002, 5003…
please note I am not a developer so can not create a script etc
any help would be much appreciated
thanks
Sorry forgot to point out the files are .json files - so the current filenames are 5001.json, 5002.json etc… But I only want the number from the filename not the complete filename
-
The Notepad++ search-and-replace algorithm doesn’t have a “counter” in the replacement, nor does it have a math engine, nor does it have access to the metadata like filename and extension. We have a FAQ: Can I Do a Mathematical Replacement which talks about the counting/math portion.
If all the text you wanted to replace was in a single file, then you could use the MultiReplace or Columns++ plugin, as described in that FAQ; but I don’t have enough experience with those plugins to say whether or not you could use their counter technology across multiple files (that is, I don’t know if those plugins have the equivalent of Notepad++'s Find in Files feature) – though their authors (@Thomas-Knoefel and @Coises) both are regulars of the Forum, and will likely chime in when they are able.
If those don’t work, one of the Scripting Plugins would, but you’d have to know how to code in order to make it work. But if you search the forum for something about “replacing text with the filename”, you will probably find an example of how to replace a piece of text (or at least add text) inside the file with the filename, and it’s not that hard to figure out how to just remove the extension (if nothing else, after running the script, you could just use Find in Files to search for
.json.png
and replace with.png
) -
@PeterJones thanks for the info peter… Scripting is like another language, will have a look at it and try and understand… as if i could figure it out I would just need to use a script to find the 1.png and “add” 5000 to the 1…
I make it sound so simple
-
IMO this is a job best suited for a “pure” script (I would probably use Python), not a script that uses Notepad++. In general I think this is true of most tasks that involve manipulating numerous files.
If you went down the route of a pure script, this forum stops being an appropriate place to ask related questions. -
@isntworkdull, scripts allow you do to one thing, and then another thing. Notepad++'s regular expression search/replace only allows you do do one one thing. If you want to then do another thing, like you can in a script, you do search/replace with another regular expression. Unfortunately, Notepad++ also does not offer a way to more automatically perform a search/replace though this can be done with Notepad++'s keyboard macro thing which essentially works like a script.
That said, you can add 5000 to your file names with a simple series of search/replace operations. Essentially, you will be running the “script” by hand by following a set of step-by-step directions.
Let’s say we have the file names, one per line:
5001.png 5002.png 5003.png ... 5010.png 5011.png 5012.png ... 4998.png 4999.png 5000.png
You can do this with eight separate search/replaces
Search Replace Comment ^5(\d{3}\.png)$
10\1
Deal with 5000.png to 5999.png by changing the leading 5
to a10
^(\d{1}\.png)$
500\1
Deal with 1.png to 9.png by inserting 500
in front of the 1-digit number to make500#.png
^(\d{2}\.png)$
50\1
Deal with 10.png to 99.png by inserting 50
in front of the 2-digit number to make50##.png
^(\d{3}\.png)$
5\1
Deal with 100.png to 999.png by inserting 5
in front of the 3-digit number to make5###.png
^1(\d{3}\.png)$
6\1
Deal with 1000.png to 1999.png by changing the leading 1
to a6
^2(\d{3}\.png)$
7\1
Deal with 2000.png to 2999.png by changing the leading 2
to a7
^3(\d{3}\.png)$
8\1
Deal with 3000.png to 3999.png by changing the leading 3
to a8
^4(\d{3}\.png)$
9\1
Deal with 4000.png to 4999.png by changing the leading 4
to a9
The first one with
Search:^5(\d{3}\.png)$
Replace:10\1
is the exception to the list because you included an exception to your file naming. You wanted to add 5000 to all numbers from 1 to 999 meaning we would have 5001 to 5999. The exception is that you also wanted to add 5000 to I guess 5999 meaning we would have 10000 to 10999. As we don’t want to add 5000 twice I did the search/replace that deals with the already existing 5000.png to 5999.png file names first.Some search/replace systems such as
sed
offer a feature known astransform
where the set of single steps to deal with the four-digit values could be done in a single search/replace. Notepad++'s regular expression system does not offer a transform operator meaning we needed five separate steps in our manual script. -
@isntworkdull said in batch changing png image to same as filename:
Not sure this is possible - but trying to save myself alot of time… Basically I have 5000 files, and inside each file is a .png link containing a number (1 - 5000) like “1.png” or “2.png” all the way to “5000.png”
This is possible using just 2 basic tools, a BATch command to add the filename to the json file and NPP regex using the Find in Files function.
So to begin:
- in a CMD window, go to the folder containing the json files. If more than 1 folder this step might need to be performed multiple times to get all the json files. The actual DOS command is
for %y in (*.json) do (@echo. && @echo %y) >> %y
. In testing it was found to add a space, then CRLF, then the filename (with .json) and a final CRLF. - In NPP, using the Find in Files function we have:
Find What:(?s)\d+(\.png.+)\x20\R(\d+)\.json\R
Replace With:${2}${1}
The search mode is obviously regular expression and use the filters set to *.json, set directory and possibly tick “in all sub folders” if required to parse all the json files being changed.
One assumption I’ve made is that there is only 1 link in each file that needs to be changed. As a suggestion take a few json files and copy elsewhere and run these steps over them and check the result before doing it on the real files, just in case you spot a major issue that you may not have been aware of, or forgot to convey in your post.
Good Luck
Terry - in a CMD window, go to the folder containing the json files. If more than 1 folder this step might need to be performed multiple times to get all the json files. The actual DOS command is
-
Make a backup copy of your files first!
Open Search | Find in Files… from the main menu.
Enter:
Find what:
\b(?:(\d)|(\d\d)|(\d\d\d)|(?:(1)|(2)|(3)|(4)|(5))(\d\d\d))\.png
Replace with:(?1(500$1))(?2(50$2))(?3(5$3))(?4(6$9))(?5(7$9))(?6(8$9))(?7(9$9))(?8(10$9))
Filters:*.json
Directory: browse to find where your files are stored
In all sub-folders: check or not, as appropriate
Search Mode : Regular expressionthen use Replace in Files.
-
@Coises
I like it, only issue is you forgot to return the.png
in the replacement field. That is unless you meant for the Find What to use a lookahead?Just goes to show that although regex can’t count it can be made to do some wonderous things nobody would have believed if they didn’t see it for themselves.
Terry