Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to count the underscore in a filename variable ?  (Read 5761 times)

0 Members and 1 Guest are viewing this topic.

Hackoo

    Topic Starter


    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
How to count the underscore in a filename variable ?
« on: August 28, 2017, 09:50:57 AM »
Hi  ;)
I have this script in order to count the underscore in a file name variable!
but, I have always the count is equal to zero?
What wrong with this script?
Code: [Select]
@echo off
set /a "cnt=0"
set /a "pos=0"
setlocal enabledelayedexpansion
@for /f "delims=" %%a in ('Dir /b "%userprofile%\Desktop"') do (
set "filename=%%a"
Call:Stringloop "!filename!"
)

pause & exit
::**************************************************
:Stringloop <filename>
set "filename=%~1"
if "!filename:~%pos%,1!"=="_" set /a cnt+=1
set /a pos+=1
if not "!filename:~%pos%,1!"=="" goto Stringloop
echo !filename! : !cnt!
exit /b
::*************************************************
Thank you !

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: How to count the underscore in a filename variable ?
« Reply #1 on: August 28, 2017, 10:18:31 AM »
Cant test this from work where I am now, but say you place X in for underscore _ and have filename as Xmen.doc does it then show counter equal to 1 and a file name as Xfiles.TXT as a value of 3 since it gets two more X's from that file too? This tests the core of what you have so you know the only issue is "_"

Wondering if an escape character is needed for the underscore to be detected as such if thats the case. Or other method is testing for specific ASCII value for each character in filenames vs character match. https://helloacm.com/the-chr-function-implementation-in-windows-pure-batch-script/

"^_"   this might be the fix if its just requiring an escape character.  ;)

http://www.robvanderwoude.com/escapechars.php

btw the ascii decimal identifier for underscore is 95 so such as if you press and hold alt and then type 95 on keypad and then release alt you will get an underscore in notepad etc. You can test through chr and test that way for 95 vs  for "_" and that might fix this. Escape character if thats the fix is the easiest fix though.

This might be a fixed version... unable to test on work computer though  ;D
Code: [Select]
@echo off
set /a "cnt=0"
set /a "pos=0"
setlocal enabledelayedexpansion
@for /f "delims=" %%a in ('Dir /b "%userprofile%\Desktop"') do (
set "filename=%%a"
Call:Stringloop "!filename!"
)

pause & exit
::**************************************************
:Stringloop <filename>
set "filename=%~1"
if "!filename:~%pos%,1!"=="^_" set /a cnt+=1
set /a pos+=1
if not "!filename:~%pos%,1!"=="" goto Stringloop
echo !filename! : !cnt!
exit /b
::*************************************************

Salmon Trout

  • Guest
Re: How to count the underscore in a filename variable ?
« Reply #2 on: August 28, 2017, 01:14:19 PM »
2 points, one red, one green (see code)

(1) You need to set pos=0 for each file name. Your script sets it once and it just increases forever. No need to do set /a when creating a variable, only when you do arithmetic on it.

(2) To get a variable to work in a substring expression you need to use CALL SET (to make a child process expand the variable - and you need to double up the percent signs as shown)

@echo off
set cnt=0
setlocal enabledelayedexpansion
@for /f "delims=" %%a in ('Dir /b "%userprofile%\Desktop"') do (
   
   REM (1)
   set pos=0
   

   set "filename=%%a"
   Call:Stringloop "!filename!"
)
pause & exit
::**************************************************
:Stringloop <filename>
set "filename=%~1"

REM (2)
CALL Set "char=%%filename:~%pos%,1%%"


if "!char!"=="_" set /a cnt+=1
if "!char!"=="" echo !filename! : !cnt! & exit /b
set /a pos+=1
goto Stringloop
::*************************************************


Output...


HP : 0
iPlayer Recordings : 0
LockScreen Images by Winaero Tweaker : 0
Tor Browser : 0
Tor Browser2 : 0
Burn to the Brim.lnk : 0
Core Temp Gadget & Addons.url : 0
Core Temp.lnk : 0
desktop.ini : 0
Folder Size Explorer.lnk : 0
Folder Size.lnk : 0
FreeCommander XE.lnk : 0
Frhed.lnk : 0
Google Chrome Canary.lnk : 0
grub-choose-default.lnk : 0
HandBrake.lnk : 0
Intel© Power Gadget 3.0.lnk : 0
Internet Explorer.lnk : 0
MTPuTTY.lnk : 0
Namexif.lnk : 0
New_Text_Document_01.txt : 3
New_Text_Document_02.txt : 6
New_Text_Document_03.txt : 9
New_Text_Document_04.txt : 12
New_Text_Document_05.txt : 15
New_Text_Document_06.txt : 18
New_Text_Document_07.txt : 21
New_Text_Document_08.txt : 24
Nmap - Zenmap GUI.lnk : 24
NTLite.lnk : 24
Opera browser.lnk : 24
OSFMount.lnk : 24
ParNRar.lnk : 24
Perspective_Image_Correction.lnk : 26
Process Hacker 2.lnk : 26
QExplore 'My Computer'.lnk : 26
RapidBATCH 5.1 Personal Edition.lnk : 26
RMCDesigner.lnk : 26
RMPrepUSB.lnk : 26
SABnzbd.lnk : 26
Start Tor Browser.lnk : 26
Subtitle Edit.lnk : 26
Transmission Remote GUI.lnk : 26
VcXsrv.lnk : 26
Windows 10 Upgrade Assistant.lnk : 26
Windows 7 USB DVD Download Tool.lnk : 26
XLaunch.lnk : 26
Zipware.lnk : 26
Press any key to continue . . .


Hackoo

    Topic Starter


    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: How to count the underscore in a filename variable ?
« Reply #3 on: August 28, 2017, 06:54:48 PM »
Thank you, both for your reply!
@Salmon Trout
Your script works but, I want to get for every file the count of its underscore!
for example in your output you get something like that on this file : New_Text_Document_08.txt : 24
or we expect like this : New_Text_Document_08.txt : 3  and so on ......
Edit
I got it  ;D
The mistake: The variables cnt and pos are never reset to 0
so to correct this we should do like this :
Code: [Select]
@echo off
Rem The srting to count in the filename is the underscrore "_"
Rem we can of course set another one ;)
set "MyString=_"
setlocal enabledelayedexpansion
@for /f "delims=" %%a in ('Dir /b "%userprofile%\Desktop"') do (
   set "filename=%%a"
   set /a "cnt=0"
   set /a "pos=0"
   Call:Counting_String "!filename!" "%Mystring%"
)
pause & exit
::*********************************************************
:Counting_String <filename> <MyString>
set "filename=%~1"
set "string=%~2"
if /I "!filename:~%pos%,1!"=="%Mystring%" set /a cnt+=1
set /a pos+=1
if not "!filename:~%pos%,1!"=="" goto Counting_String
echo !filename!: [!cnt!] "%Mystring%"
exit /b
::*********************************************************
« Last Edit: August 28, 2017, 07:49:44 PM by Hackoo »

Hackoo

    Topic Starter


    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: How to count the underscore in a filename variable ?
« Reply #4 on: August 29, 2017, 02:47:42 AM »
Thank you, both for your reply!
@Salmon Trout
Your script works but, I want to get for every file the count of its underscore!
for example in your output you got something like that on this file : New_Text_Document_08.txt : 24
or we expect like this : New_Text_Document_08.txt : 3  and so on ......
Edit
I got it  ;D
The mistake: The variables cnt and pos are never reset to 0
so to correct this we should do like this :
Code: [Select]
@echo off
Rem The srting to count in the filename is the underscrore "_"
Rem we can of course set another one ;)
set "MyString=_"
setlocal enabledelayedexpansion
@for /f "delims=" %%a in ('Dir /b "%userprofile%\Desktop"') do (
   set "filename=%%a"
   set /a "cnt=0"
   set /a "pos=0"
   Call:Counting_String "!filename!" "%Mystring%"
)
pause & exit
::*********************************************************
:Counting_String <filename> <MyString>
set "filename=%~1"
set "string=%~2"
if /I "!filename:~%pos%,1!"=="%Mystring%" set /a cnt+=1
set /a pos+=1
if not "!filename:~%pos%,1!"=="" goto Counting_String
echo !filename!: [!cnt!] "%Mystring%"
exit /b
::*********************************************************

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: How to count the underscore in a filename variable ?
« Reply #5 on: August 31, 2017, 11:44:57 PM »
You completely missed the fancy way aGerman showed you how to accomplish your task.  His code has no GOTO commands so it literally counts it all in one pass.
http://www.dostips.com/forum/viewtopic.php?f=3&t=8071#p53604

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: How to count the underscore in a filename variable ?
« Reply #6 on: September 01, 2017, 05:10:59 AM »
Quote
aGerman showed you how to accomplish your task

 :-\

Knows its praise, but ( a German ) just has a strange tone to its label. Not sure if its because I'm half German myself etc and had issues with people years ago in elementary school jabbing at my ancestry etc when sharing family tree info etc. Knows its praise and not a bad label etc. Thinking stating Salmon Trout would have been sufficient. Maybe Salmon Trout and Squashman are at some joking around level that makes this less odd in reading.   ;D But concerned that passer bys might find that not so much a praise and maybe offensive.  ::) Just my thoughts. While not jewish, family members ( my great grandfathers brothers and sisters ) died trying to escape the war, 2 of which when pressured to join the nazi army left the world on their own terms vs joining as a nicer way of explaining a bad situation as to how they died. I am not offended by squashman... all is cool but just my concerns about passerbys who might not understand a friendship joking jingle with that label.  :-\

Salmon Trout

  • Guest
Re: How to count the underscore in a filename variable ?
« Reply #7 on: September 01, 2017, 05:51:38 AM »
The aGerman thing is a mystery to me. I think maybe that is the username of someone on the dostips forum, that is linked to in squash man's reply.

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: How to count the underscore in a filename variable ?
« Reply #8 on: September 01, 2017, 05:59:22 AM »
OHHHHHHHHHHHHHHHHHHHHHHHHHHHHhhhhhhhhhh hhhhhhhhhhh  :-[

I was thinking it was cause you were German and you ended up helping him. Just went to dostips and sure enough there is a username of aGerman... well now passer bys wont be confused and accidentally offended with what may be looked at as a label vs a username.  ::) I saw it as a typo missed space and ( a German ) basically. Laughing at myself in not seeing the connection of aGerman with a username and going in the wrong direction.

Salmon Trout

  • Guest
Re: How to count the underscore in a filename variable ?
« Reply #9 on: September 01, 2017, 08:57:51 AM »
I'm not German, I'm English!

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: How to count the underscore in a filename variable ?
« Reply #10 on: September 01, 2017, 09:50:04 AM »
 ;D

All these years I thought you were German when referring to europe and seeing this.

Quote
Völlig losgelöst   
von der Erde   
schwebt das Raumschiff   
völlig schwerelos.


Salmon Trout

  • Guest
Re: How to count the underscore in a filename variable ?
« Reply #11 on: September 01, 2017, 10:25:50 AM »
Oh that! It's some words of a song, "Major Tom (Coming Home)" by Peter Schilling (who is German!) It was the theme tune of a German TV series called "Deutschland 83", a spy drama set in 1983. I really liked it. The song was a big European hit in that year. So was the TV series in 2015. I just thought it was cool to put some of the words in my signature. The series premiered on 17 June 2015 on the SundanceTV channel in the United States, becoming the first German-language series to air on a US network.

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: How to count the underscore in a filename variable ?
« Reply #12 on: September 01, 2017, 01:15:11 PM »
COOL and now it all makes sense  ;D

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: How to count the underscore in a filename variable ?
« Reply #13 on: September 01, 2017, 01:20:51 PM »
Glad we got that all cleared up.  For many years I thought my ancestors were from Germany but it turned out they were actually from Austria but they spoke German.