JREN.BAT v2.8 - Rename files/folders using regular expressions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dostipsQuestion
Posts: 4
Joined: 13 May 2020 18:01

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#61 Post by dostipsQuestion » 15 May 2020 09:47

dbenham wrote:
14 May 2020 09:04
Question 1

I'm not exactly sure what you are trying to do.

If you want only the size of a single folder,
like "D:\portable", then specify the root path with /P and
specify the specific folder with /FM

Code: Select all

call jren "^" "size()+'  '" /d /j /list /p "d:\" /fm "portable"
Dave Benham
.
Hello Dave Benham,
Thank You for the code below:

Code: Select all

call jren "^" "size()+'  '" /d /j /list /p "d:\" /fm "portable"
Desired output is achieved:
3596879 portable
Thank You


Question:
How to use above code with variables?

Code: Select all

call jren "^" "size()+'  '" /d /j /list /p "%~d0" /fm "%~p0" > "FolderList1.txt"
%~d0 = d:
%~p0 = \portable\

The problem is the \

Said differently:
"portable" = Directory name with no \ \ = works in above code.
"\portable\" = %~p0 = does not work in above code.


Help.
Please clarify how above variables in batch can be done.

--

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#62 Post by dbenham » 15 May 2020 15:18

The /FM must be the name of the folder or file you want, and the /P must be the full path leading up to that file/folder.

Code: Select all

for %%F in ("%~dp0.") do call jren "^" "size()+'  '" /d /j /list /p "%%~dpF" /fm "%%~nxF" > "FolderList1.txt"
"%~dp0." expands to "D:\portable\.", which is logically equivalent to "D:\portable"

So in the FOR loop, "%%~dpF" yields "D:\" and "%%~nxF" yields "portable".

Note that a folder name could include an extension, which is the reason I use %%~nxF instead of %%~nF.

Similarly I use %%~dpF instead of %%~dF just in case your batch script is not in a folder that hangs of the root.

dostipsQuestion
Posts: 4
Joined: 13 May 2020 18:01

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#63 Post by dostipsQuestion » 16 May 2020 21:23

Hello Dave Benham,
Thank You for the code in above post.

Below are slight changes to your code.
These changes might add clarity for others:

Code: Select all

for %%F in ("%~dp0.") do call D:\Batches\JREN\JREN.BAT "^" "size('               ')+'  bytes in       '" /d /j /list /p "%%~dpF" /fm "%%~nxF" > "R:\FolderList1.txt"
Above code of JREN.bat version 2.8 gives desired output as:
______227595130 bytes in FireFox


The above _ is added to align numbers in this post.
Actually, JREN.bat automatically aligns by padding with blank spaces.


Regarding counting, I am unsure as to what else JREN.bat can do.
Maybe JREN.bat can also do more than byte counting as per above code.
So I will ask.
I rather use JREN.bat versus other commands.

What code would tell JREN.bat version 2.8 to:
1. count Files?
2. count Folders?

Looking for output:
______227595130 bytes in FireFox
____________387 Files
_____________86 Folders

If JREN.bat cannot count files and folders as shown above,
then counting can be made in batch with:
dir /a:-d /s /b | find /c ":\" > "R:\FolderList1.txt"
dir /a:d /s /b | find /c ":\" > "R:\FolderList1.txt"


And, what code would tell JREN.bat version 2.8 to:
3. List all Files?

Looking for output:
S:\FireFox\App
S:\FireFox\Data
S:\FireFox\help.html
S:\FireFox\FirefoxPortable.exe
Etc.

If JREN.bat cannot output above files then I can use a batch with:
dir /s /b /ON > "R:\FolderList1.txt"



And, what code would tell JREN.bat version 2.8 to:
4. Date & Time stamp? Ideal stamp:
Ended : Sat 16 May 2020 at 11:55:14
If JREN.bat cannot stamp as above then this is close:
"C:\WINDOWS\system32\ROBOCOPY.EXE" /L * \ \ | find /i "Ended" >> "R:\FolderList1.txt"


And, what code would tell JREN.bat version 2.8 to:
5. Give credit to you in TEXT files? Meaning:
JREN.BAT version 2.8 by Dave Benham
Put above text to a file.

Because this does work at windows command line:
D:\Batches\JREN\JREN.BAT /?VERSION
But above command did not work inside a batch.

and
your web site for JREN.BAT would be good for a text file.
http://_


6. Curious, what is maximum JREN.BAT byte count?
For sure JREN.BAT can count to 255 GB, example:
___255000111222 bytes


Byte count background:
https://superuser.com/questions/222249/ ... 267#222267
As designed, the maximum NTFS file size is
16 EB (16 × 1024^6 bytes) minus 1 KB (1024 bytes) or
18,446,744,073,709,550,592 bytes.

As implemented, the maximum NTFS file size is
16 TB (16 × 1024^4 bytes) minus 64 KB (64 × 1024 bytes) or
17,592,185,978,880 bytes.

Said differently, maximum NTFS file size is:
18446744073709550592 bytes by design _______ > 18 quintillion
______17592185978880 bytes by implementation > 17 trillion

Maximum volume size
Implementation:
256 terabytes minus 64 KB ( 232 clusters minus 1 cluster)
https://docs.microsoft.com/en-us/previo ... dfrom=MSDN



--

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#64 Post by dbenham » 18 May 2020 07:30

In general, the built in help (JREN /?) does a good job describing the features of JREN. It is up to you to figure out how to use those features. Plus there are examples throughout this thread. I hope you take the time to scan all the posts.
dostipsQuestion wrote:
16 May 2020 21:23
What code would tell JREN.bat version 2.8 to:
1. count Files?
2. count Folders?

Looking for output:
______227595130 bytes in FireFox
____________387 Files
_____________86 Folders
JREN does not have any feature to provide a summary at the end of all processing. That might be an interesting feature to add, though I'm not particularly inclined to invest in much more development with this project. It is significantly behind in its Unicode capabilities compared to JREPL.BAT - regular expression find/replace utility. And the date/time formatting and computation capabilities lag compared to what is available in jTimeStamp.bat. I probably want to address that before I add any new features, but there just doesn't seem to be that much interest in this utility. By far my most widely used utility is JREPL.BAT. You should check that out if you haven't seen it already.
dostipsQuestion wrote:
16 May 2020 21:23
And, what code would tell JREN.bat version 2.8 to:
3. List all Files?

Looking for output:
S:\FireFox\App
S:\FireFox\Data
S:\FireFox\help.html
S:\FireFox\FirefoxPortable.exe
Etc.
Not sure exactly what you are looking for. JREN can process (list) files or folders, but not both at the same time. The /S option allows recursion of subdirectories. So the following would list all files within the folder tree rooted at the current directory:

Code: Select all

jren "^" "path()" /list /s /j
JREN processes system and hidden files the same as any other. Use the many other options to specify the root folder, and filter paths and files as needed.
dostipsQuestion wrote:
16 May 2020 21:23
And, what code would tell JREN.bat version 2.8 to:
4. Date & Time stamp? Ideal stamp:
Ended : Sat 16 May 2020 at 11:55:14
You need to investigate the /TS option. The following JScript expression using the FileSystemObject will yield your desired format if your computer's date format can be parsed by JScript

Code: Select all

ts({ dt:'fsoCreated', fmt:'Ended : {wkd} {d} {month} {yyyy} at {hh}:{nn}:{ss}' })
If the date cannot be parsed, then you will need the much slower WMI variant:

Code: Select all

ts({ dt:'created', fmt:'Ended : {wkd} {d} {month} {yyyy} at {hh}:{nn}:{ss}' })
dostipsQuestion wrote:
16 May 2020 21:23
And, what code would tell JREN.bat version 2.8 to:
5. Give credit to you in TEXT files? Meaning:
JREN.BAT version 2.8 by Dave Benham
Put above text to a file.

Because this does work at windows command line:
D:\Batches\JREN\JREN.BAT /?VERSION
But above command did not work inside a batch.
JREN.BAT is a batch script, so you must use CALL JREN within your own script, else it won't return to your script.
dostipsQuestion wrote:
16 May 2020 21:23
your web site for JREN.BAT would be good for a text file.
http://_
I treat this DosTips thread as the primary source for JREN. https://www.dostips.com/forum/viewtopic.php?f=3&t=6081

The closest thing I have to a personal website is my StackOverflow profile: https://stackoverflow.com/users/1012053 ... ab=profile

And for music I have my YouTube channel - www.youtube.com/c/DaveBenhamMusic
and SoundCloud channel - www.soundcloud.com/dave-benham

dostipsQuestion wrote:
16 May 2020 21:23
6. Curious, what is maximum JREN.BAT byte count?
For sure JREN.BAT can count to 255 GB, example:
___255000111222 bytes
I don't know for sure. I'm relying on built in Microsoft routines to get the files sizes. My guess is that it cannot exceed the maximum JScript integer size of (2^53)-1 = 9,007,199,254,740,991. And then within that limit I imagine it is dependent on the underlying file system.


Dave Benham

dostipsQuestion
Posts: 4
Joined: 13 May 2020 18:01

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#65 Post by dostipsQuestion » 20 May 2020 07:32

.
With code below, JREN.BAT works great giving output:

____87111222333 bytes in Archive

Note the _ means padded blank spaces.


Question:
What is an easy way to use above
1 line output and get this
4 line output?

_______________ TB (Tera Bytes)
____87_________ GB (Giga Bytes)
____87111______ MB (Mega Bytes)
____87111222___ KB (Kilo Bytes)
____87111222333 bytes in Archive


Said differently, replace numbers with blank spaces.
Not doing math of divide by 1024.
Just truncate.
Just replace numbers with blank spaces.


Can above 5 line output be done inside JREN?

If so, please make a suggestion as to how and
point me to line number inside JREN.bat

If not, then what suggested Text Tool can manipulate the
1 line output?
____87111222333 bytes in Archive


Code for JREN.bat

Code: Select all

for %%F in ("%~dp0.") do call D:\Batches\JREN\JREN.BAT "^" "size('               ')+'  bytes in       '" /d /j /list /p "%%~dpF" /fm "%%~nxF" > "d:\FolderList1.txt"

--

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#66 Post by dbenham » 20 May 2020 08:55

I'm' going to leave that coding exercise to you. The entire JScript language is at your disposal. You can include newlines in your output with the correct JScript code.

medp7060
Posts: 9
Joined: 05 Oct 2020 20:41

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#67 Post by medp7060 » 05 Oct 2020 21:25

I may have found a small bug. This is reproducible:

Code: Select all

D:\>jren BOOTEX.loG BOOTEX.LOG /P "G:\"
"G:\BOOTEX.loG"  -->  "BOOTEX.LOG"

D:\>jren BOOTEX.LOG BOOTEX.loG /P "G:\"
"G:\BOOTEX.LOG"  -->  "BOOTEX.loG"

D:\>jren BOOTEX.loG BOOTEX.LOG /P "G:\"
"G:\BOOTEX.loG"  -->  "BOOTEX.LOG"        // but not renamed !!!!

D:\>jren BOOTEX.loG BOOTEX.LOG /P "G:\"
"G:\BOOTEX.loG"  -->  "BOOTEX.LOG"

D:\>
Drive G: is a USB. That is the third rename command was executed, but name was not changed. A fourth command has to be issued for the renaming.

Further test on Drive G: confirmed with a new CMD session. This only needs once to actually rename it:

Code: Select all

G:\>jren BOOTEX.LOG BOOTEX.loG
"G:\BOOTEX.LOG"  -->  "BOOTEX.loG"

G:\>
with a new CMD session, this needs to run twice to actually rename it:

Code: Select all

G:\>jren BOOTEX.loG BOOTEX.LOG
"G:\BOOTEX.loG"  -->  "BOOTEX.LOG"     // but not renamed !!!!

G:\>jren BOOTEX.loG BOOTEX.LOG
"G:\BOOTEX.loG"  -->  "BOOTEX.LOG"

G:\>

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#68 Post by dbenham » 06 Oct 2020 17:40

That is weird, I don't see how my code can do that. And working off a USB drive shouldn't make a difference.

Have you tried the same steps with a simple REN instead of JREN? Your example should work just as well with REN.

Please note that your first argument is a regular expression, meaning the dot is a wildcard that matches any character, not a dot literal. But that has nothing to do with your reported behavior.


Dave Benham

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#69 Post by aGerman » 07 Oct 2020 10:01

It's not a bug it's a feature :lol:
Also REN doesn't work for me, not even manual renaming in the explorer UI. Neither in one direction nor in the other one. The reason is likely that file names on Windows are not case-sensitive. So, I suspect the names are treated to be the same. Nothing wrong with JREN here :wink:

Possible workaround could be to rename in two steps where the first step actually updates the file name (in terms of my assumption). Something like that

Code: Select all

ren "G:\BOOTEX.loG" "BOOTEX.~LOG"
ren "G:\BOOTEX.~LOG" "BOOTEX.LOG"
Steffen

medp7060
Posts: 9
Joined: 05 Oct 2020 20:41

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#70 Post by medp7060 » 07 Oct 2020 15:23

Thanks, Dave.

As Steffen said, your script has the feature where other utilities all fail when Windows simply ignores case-sensitive renaming. I did a thorough research and yours came up as the only one which can make a case sensitive name change. Windows' own ren command, all other file explorers cannot do that. The MS says that old FAT, exFAT, etc are case conserving, but not case sensitive. Therefore, it is impossible just to change the case of a file name on these file systems. However, your script can. To me, it seems that your current v2.8 has a little thing that prevents the case sensitive renaming from going to a certain direction (e.g. from "BOOTEX.loG" --> "BOOTEX.LOG" in some cases like mine, not always), but the other direction renaming is always fine (e.g. "BOOTEX.LOG" --> "BOOTEX.loG"). Of course, it is possible that your script is fine, it could be because of other factors that resulted in the weird thing I observed.

Your codes are too complex for me to figure out what is the possible reason. I can only give you more details on my circulation, in case you wanted to have a look at it. Thank you so much for this useful utility.

OS: Windows 10 1903
Drive G: USB FAT32 format (with "Quick Removal" device enabled)
A text file: BOOTEX.LOG


I tested it again today. This issue is reproducible:

One command is enough for "BOOTEX.LOG" --> "BOOTEX.loG"

Code: Select all

Microsoft Windows [Version 10.0.18362.900]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\xyz>g:

G:\>jren BOOTEX.LOG BOOTEX.loG
"G:\BOOTEX.LOG"  -->  "BOOTEX.loG"

G:\>dir
 Volume in drive G is USB_DATA
 Volume Serial Number is E03D-4258

 Directory of G:\
 04/10/2020  10:54 pm            41,722 JREN.BAT
08/10/2020  09:59 am                34 BOOTEX.loG
               5 File(s)        473,480 bytes
              10 Dir(s)   9,542,025,216 bytes free

G:\>exit
Two commands are needed for "BOOTEX.loG" --> "BOOTEX.LOG"

Code: Select all

Microsoft Windows [Version 10.0.18362.900]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\xyz>g:

G:\>jren BOOTEX.loG BOOTEX.LOG
"G:\BOOTEX.loG"  -->  "BOOTEX.LOG"     // actually failed see below

G:\>dir
 Volume in drive G is USB_DATA
 Volume Serial Number is E03D-4258

 Directory of G:\
04/10/2020  10:54 pm            41,722 JREN.BAT
08/10/2020  09:59 am                34 BOOTEX.loG    // failed here
               5 File(s)        473,480 bytes
              10 Dir(s)   9,542,025,216 bytes free

G:\>jren BOOTEX.loG BOOTEX.LOG                // a second try
"G:\BOOTEX.loG"  -->  "BOOTEX.LOG"             // successful!!!

G:\>dir
 Volume in drive G is USB_DATA
 Volume Serial Number is E03D-4258

 Directory of G:\
04/10/2020  10:54 pm            41,722 JREN.BAT
08/10/2020  09:59 am                34 BOOTEX.LOG
               5 File(s)        473,480 bytes
              10 Dir(s)   9,542,025,216 bytes free

G:\>exit

medp7060
Posts: 9
Joined: 05 Oct 2020 20:41

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#71 Post by medp7060 » 07 Oct 2020 16:35

More tests have revealed that JREN.bat will fail sometimes at first try, but a second try will work to rename the cases:

Code: Select all

G:\>jren captain.mkv captain.MKV
"G:\captain.mkv"  -->  "captain.MKV"          //actually failed on the first try

G:\>jren captain.mkv captain.MKV
"G:\captain.mkv"  -->  "captain.MKV"         //succeeded on the 2nd try

G:\>jren captain.MKV captain.mkv
"G:\captain.MKV"  -->  "captain.mkv"         //actually failed on the first try

G:\>jren captain.MKV captain.mkv
"G:\captain.MKV"  -->  "captain.mkv"         //succeeded on the 2nd try

G:\>

medp7060
Posts: 9
Joined: 05 Oct 2020 20:41

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#72 Post by medp7060 » 07 Oct 2020 23:47

By the way, these always worked. So perhaps is there something to do with the the dot (.)?

Code: Select all

G:\t>jren BOOTEX.1.LOG BOOTEX.1.loG
"G:\t\BOOTEX.1.LOG"  -->  "BOOTEX.1.loG"          //succeeded 

G:\t>jren BOOTEX.1.loG BOOTEX.1.LOG
"G:\t\BOOTEX.1.loG"  -->  "BOOTEX.1.LOG"          //succeeded 

G:\t>jren BOOTEX.1.LOG BOOTEX.1.loG
"G:\t\BOOTEX.1.LOG"  -->  "BOOTEX.1.loG"          //succeeded 

G:\t>jren BOOTEX.1.loG BOOTEX.1.LOG
"G:\t\BOOTEX.1.loG"  -->  "BOOTEX.1.LOG"          //succeeded 

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#73 Post by aGerman » 08 Oct 2020 03:19

I had a brief look at the code. Seems that Dave updates the Name property of the FolderItem object.
https://docs.microsoft.com/en-us/window ... folderitem
If you have some experiences in JScript you may just write a piece of code which skips all the regex stuff. Even though my expectation is that this won't help much. If you're facing issues like the same thing fails once and succeeds at a second try, then I strongly suspect it's some kind of undefined behavior. You should never rely on it. Rather consider to work around it as I did …

Steffen

//EDIT:
Save this code with extension .bat and give it a go

Code: Select all

@if (0)==(0) echo off &set jsrename=cscript.exe //nologo //e:jscript "%~fs0"

>"foo.TXT" type nul                    &REM create a new file
pause                                  &REM just to give you the opportunity to observe what happened

%jsrename% "%cd%" "foo.TXT" "foo.txt"  &REM Syntax: %jsrename% "Path" "OldName" "NewName"
pause

%jsrename% "%cd%" "foo.txt" "foo.TXT"
pause

goto :eof @end new ActiveXObject('Shell.Application').NameSpace(WScript.Arguments(0)).ParseName(WScript.Arguments(1)).Name = WScript.Arguments(2);
If you want to modify it, always leave the first and the last line the same and update the code in the middle.

medp7060
Posts: 9
Joined: 05 Oct 2020 20:41

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#74 Post by medp7060 » 09 Oct 2020 03:12

Thanks, Steffen.

I tested your script. It worked as the ren command.

I also think the weird observations with JREN.BAT is due to some other undefined causes. ATM, I am using the workaround.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#75 Post by aGerman » 09 Oct 2020 10:26

For me it worked on the NTFS-formatted C: volume only. It failed on my exFAT-formatted external SSD and on a USB pen drive with FAT32 file system. However I'm uncertain if those are really representative results. Even though when I used the FileSystemObject object ...

Code: Select all

goto :eof @end new ActiveXObject('Scripting.FileSystemObject').MoveFile(WScript.Arguments(0) + '\\' + WScript.Arguments(1), WScript.Arguments(0) + '\\' + WScript.Arguments(2));
... it's still the same behavior which indicates that the file system might have something to do whith it.

Steffen

Post Reply