counter in variable location in filename

English support forum

Moderators: white, Hacker, petermad, Stefan2

Rekumbens
Junior Member
Junior Member
Posts: 6
Joined: 2020-11-08, 17:14 UTC

counter in variable location in filename

Post by *Rekumbens »

File names are given that include an inventory number, which can be anywhere before the extension.
Example:
tmp001 FERY Antal (HUN) - ex libris Palásthy; X2, op. 10, 1980, 40×60, ff, 61235, Son lucet....tif
tmp002 FERY Antal (HUN) - ex libris Arady Kálmán; X2, op. 11, 1981, 60×71, 1color, 61236.tif
The inventory number is 61235, 61236
These must be replaced with other numbers (another inventory number), e.g.:
62100, 62101
There are hundreds of these in a library.

There is regExp in the search/replace field, but the value [C] cannot be entered.
I also thought that when typing the file name instead of the numbers, I would put the text "xxxxx" and replace it with the inventory number afterwards.
But you cannot insert a [C] counter in search/replace.

There is no RegExp for the file name, so I can find it somehow...

Idea?
User avatar
Dalai
Power Member
Power Member
Posts: 9393
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: counter in variable location in filename

Post by *Dalai »

Placeholders like [N], [C], [P] and so on only work in the Rename mask field; this is by design. Search and replace supports literal strings and RegEx strings.

To replace multiple numbers you can enter all numbers separated by a pipe symbol, and do the same with the replacements. Something like this:
Search for:

Code: Select all

61235|61236
Replace with:

Code: Select all

62100|62101
Make sure to disable RegEx since the pipe has a special meaning in that case.

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
Rekumbens
Junior Member
Junior Member
Posts: 6
Joined: 2020-11-08, 17:14 UTC

Re: counter in variable location in filename

Post by *Rekumbens »

This is not good for me, because when changing 100 inventory numbers, you would have to type a lot! Also, I don't know exactly the wrong numbers that should be replaced!
There should be some automation.
We only know that the bad numbers are five digits (52400, 53321, 54222, etc.) and I would replace that with another sequence of numbers, e.g. 62000 to 62200
User avatar
white
Power Member
Power Member
Posts: 4623
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: counter in variable location in filename

Post by *white »

You can, for example, add the counter to the front and use find and replace to replace the wrong number with the new number. Or should the sequence order be preserved?
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: counter in variable location in filename

Post by *beb »

2Rekumbens
before and after:
Image: https://i.imgur.com/CTgYkMU.jpg
(items with 5 digits have been renumbered starting from 62000, the others left intact):

PowerShell_inventoryRecount.ps1

Code: Select all

$source     = 'tif'
$inventory  = 62000
$tif        = (Get-ChildItem -path $source -filter *.tif -recurse)|Sort-Object
$tif|Where-Object {$_.Name -match '\d{5}' -and $_.Name -notmatch '\d{6}'}|Rename-Item -NewName {$_.Name -replace "\d{5}",$script:inventory++}
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Rekumbens
Junior Member
Junior Member
Posts: 6
Joined: 2020-11-08, 17:14 UTC

Re: counter in variable location in filename

Post by *Rekumbens »

WHITE:
​The order in the file name is important, because they will be database elements, commas will break them up into elements...

BEB: Unfortunately, the received script did not run, even though I granted the necessary permissions. Tried another script, it didn't run either, so the error is on me...
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Set-ExecutionPolicy -ExecutionPolicy Bypass
error...
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: counter in variable location in filename

Post by *beb »

2Rekumbens
How do you launch it?
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: counter in variable location in filename

Post by *beb »

2Rekumbens
You can try the following Total Commander button to run the script from the current directory in the active panel.

Code: Select all

TOTALCMD#BAR#DATA
powershell -ExecutionPolicy Bypass -file PowerShell_inventoryRecount.ps1

WCMICONS.DLL
PowerShell_inventoryRecount


-1
Note: in the current edition, the script assumes the .tif files are placed in a "tif" folder next to the PowerShell_inventoryRecount.ps1, which is good for testing.
So, please:
- make the "tif" folder next to the PowerShell_inventoryRecount.ps1.
- copy some of your .tif files to the "tif" folder.
- run the script.
- see the results.

Understanding what the script does:
[1] (Get-ChildItem -path $source -filter *.tif -recurse)|Sort-Object
looks in the $source = 'tif' folder if there are any *.tif files there,
and all the *.tif files found are collected in an array and sorted to grant sequential order.
[2] Where-Object {$_.Name -match '\d{5}' -and $_.Name -notmatch '\d{6}'}
checks the collected data if there are any *.tif files there with a 5-digit part in names,
and all such *.tif files are piped for further processing.
[3] Rename-Item -NewName {$_.Name -replace "\d{5}",$script:inventory++
consecutively renames the said files under the given criteria, replacing a current 5-digit part in names with a counter number starting from 62000.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: counter in variable location in filename

Post by *Fla$her »

Rekumbens wrote: 2024-01-06, 19:40 UTC We only know that the bad numbers are five digits (52400, 53321, 54222, etc.) and I would replace that with another sequence of numbers, e.g. 62000 to 62200
You can try this .js:

Code: Select all

/****************************** JS ******************************
 Purpose: Increasing five-digit numbers in names by a given value
 Parameters: [path] [extensions or *] [addend]
 Example:    "%P" gif|jpg|tif 9600
****************************************************************/
var a = WSH.Arguments
var oItems = WSH.CreateObject('Shell.Application').Namespace(a(0)).items()
oItems.Filter(73920, '*.' + a(1).replace('|',';*.'))
for (var oFile = new Enumerator(oItems); !oFile.atEnd(); oFile.moveNext()) {
   num = oFile.item().name.match(/\b\d{5}\b/)
   if (num) oFile.item().name = String(oFile.item()).replace(num[0],eval(num[0]+'+'+a(2)))
}
oFile = null; oItems = null; CollectGarbage()
Save to ANSI, drag to the toolbar, and specify the suggested parameters in the second button field.
Last edited by Fla$her on 2024-01-08, 19:48 UTC, edited 1 time in total.
Overquoting is evil! 👎
sa16
Senior Member
Senior Member
Posts: 217
Joined: 2021-09-10, 07:15 UTC

Re: counter in variable location in filename

Post by *sa16 »

2Rekumbens
You can (1) duplicate the old five-digit number at the beginning of the file name, (2) sort the files by it, insert a new counter at the beginning, (3) replace the old number with a new one.

Ctrl+M
With RegEx
1)
Search for: (.*)(\d{5})(.*)
Replace with: $2$1$2$3
2)
F5 (next step)
Sort by Old name (if necessary)
Rename mask: file name [C62000][N6-]
Search for: <Clear>
Replace with: <Clear>
3)
F5 (next step)
Rename mask: file name [N]
Search for: (\d{5})(.*)(\d{5})(.*)
Replace with: $2$1$4
Rekumbens
Junior Member
Junior Member
Posts: 6
Joined: 2020-11-08, 17:14 UTC

Re: counter in variable location in filename

Post by *Rekumbens »

Thanks to everyone for the help, I learned from every idea!
By idea SA16, I managed to change the inventory numbers, it works great!

Fla$her: Error after starting *.js file, line 7 error 1, code: 800A0009

BEB: it didn't work, I didn't do something right, because it couldn't interpret the - file parameter...
Anyway, I did everything as you wrote: the .ps1 file is in the same directory as the test files.
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: counter in variable location in filename

Post by *beb »

2Rekumbens
Glad you have completed your task.
Figuring out how to run the PowerShell scripts is would be useful for the future, though.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: counter in variable location in filename

Post by *Fla$her »

Rekumbens wrote: 2024-01-07, 17:59 UTCFla$her: Error after starting *.js file, line 7 error 1, code: 800A0009
'error 1' — are you sure that's what it says there? Use Ctrl+C.
What parameters did you specify?
Overquoting is evil! 👎
sa16
Senior Member
Senior Member
Posts: 217
Joined: 2021-09-10, 07:15 UTC

Re: counter in variable location in filename

Post by *sa16 »

2Rekumbens
Item 3 can be moved to item 2:
2)
F5 (next step)
Sort by Old name (if necessary)
Rename mask: file name [C62000][N6-]
Search for: (\d{5})(.*)(\d{5})(.*)
Replace with: $2$1$4
Rekumbens
Junior Member
Junior Member
Posts: 6
Joined: 2020-11-08, 17:14 UTC

Re: counter in variable location in filename

Post by *Rekumbens »

I put the file in the TC menu bar
See Picture 1
Image: http://www.kepfeltoltes.eu/images/2024/01/08/946image1.jpg

Then I click and it appears: Picture 2
Image: http://www.kepfeltoltes.eu/images/2024/01/08/338image2.jpg

Error: Index is outside the range
Code:
Source: Microsoft Jscript Running Error
Post Reply