Using the strLen function with my script. Please Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
EmbraceLuke
Posts: 4
Joined: 20 May 2016 05:57

Using the strLen function with my script. Please Help

#1 Post by EmbraceLuke » 20 May 2016 06:08

Hello everyone, this is my first post. I hope you can help me.

I am currently trying to write this Batch file here for work.

I'm fairly new to .bat files but I have a basic understanding of php and c++.

I need to write a batch file that will do the following:
1. Display all files including full filepath in the current folder.
2. Display length of the displayed files including the filepath for each individual file.
3. Maybe show how many files there are in total.

I've written this so far and it does Job #1 and #3 but I can't get it do display the string lenght.

Code: Select all

@echo off
for /r . %%g in (*.*) do (
      echo %%g
      )      
set cnt=0
for %%A in (*) do set /a cnt+=1
set /A cnt=cnt-1
echo Dateien im Verzeichnis = %cnt%
PAUSE


Then I came across your Website and found the string operations. "Great" I thought and then tried to implement it into my script.
For testing purposes I've written following script that WORKS:

Code: Select all

@echo off
set "var1=Hallo1"
echo %var1%
call :strLen var1 var2
echo %var2%
Pause

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b



I then tried combining it with my original script but it failed. Do you have any idea why ? How can I get it to run?

Code: Select all

@echo off

for /r . %%g in (*.*) do (
      call :strLen g var1
      echo Pfad ist %var1% Zeichen lang
      echo %%g
      
      
      )      
set cnt=0
for %%A in (*) do set /a cnt+=1
set /A cnt=cnt-1
echo Dateien im Verzeichnis = %cnt%
PAUSE

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b



I would be more then thankful if you could give me a hint on what's wrong and maybe point me to the right direction if not maybe even posting a solution. If you can think of any easier script / solution please also let me know.

Yours Luke

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Using the strLen function with my script. Please Help

#2 Post by Matt Williamson » 20 May 2016 07:26

Try this:

Code: Select all

@echo off
setlocal enabledelayedExpansion
for /r . %%g in (*.*) do (
      call :strLen %%g var1
      echo Pfad ist !var1! Zeichen lang
      echo %%g
)     
set cnt=0
for %%A in (*) do set /a cnt+=1
set /A cnt=cnt-1
echo Dateien im Verzeichnis = %cnt%
PAUSE
exit /b

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using the strLen function with my script. Please Help

#3 Post by foxidrive » 20 May 2016 08:20

EmbraceLuke wrote:I need to write a batch file that will do the following:
1. Display all files including full filepath in the current folder.
2. Display length of the displayed files including the filepath for each individual file.
3. Maybe show how many files there are in total.


I love the smell of batch files in the morning...

Code: Select all

@echo off
for %%a in (*) do echo "%%~fa"
for %%a in (*) do echo "%%~za %%~fa"
dir /b /a-d |find /c /v ""
pause & goto :EOF

EmbraceLuke
Posts: 4
Joined: 20 May 2016 05:57

Re: Using the strLen function with my script. Please Help

#4 Post by EmbraceLuke » 20 May 2016 08:29

Thank you for your reply.

Sadly this doesn't seem to return any value in the variable var1 where it's supposed to show the number.

Matt Williamson wrote:Try this:

Code: Select all

@echo off
setlocal enabledelayedExpansion
for /r . %%g in (*.*) do (
      call :strLen %%g var1
      echo Pfad ist !var1! Zeichen lang
      echo %%g
)     
set cnt=0
for %%A in (*) do set /a cnt+=1
set /A cnt=cnt-1
echo Dateien im Verzeichnis = %cnt%
PAUSE
exit /b

EmbraceLuke
Posts: 4
Joined: 20 May 2016 05:57

Re: Using the strLen function with my script. Please Help

#5 Post by EmbraceLuke » 20 May 2016 08:34

Thank you for your reply as well..

I tried it and came to the conclusion that it displays the length of the string INSIDE the file. It's my goal to count the number of signs used in the filepath of the file.

C:\Users\fastl\Desktop\BatchFileTest\filepathcount.bat


I would like to know how many signs are used in this filepath that I was already able to get on my screen. take a look at my image:

What do you think? Is there a way to do that?


Image

foxidrive wrote:
EmbraceLuke wrote:I need to write a batch file that will do the following:
1. Display all files including full filepath in the current folder.
2. Display length of the displayed files including the filepath for each individual file.
3. Maybe show how many files there are in total.


I love the smell of batch files in the morning...

Code: Select all

@echo off
for %%a in (*) do echo "%%~fa"
for %%a in (*) do echo "%%~za %%~fa"
dir /b |find /c /v ""
pause & goto :EOF

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

Re: Using the strLen function with my script. Please Help

#6 Post by aGerman » 20 May 2016 12:57

Seems you missed the fact that you have to pass the variable name where your string was saved rather than the string itself. (It's similar to passing a reference in C++ or a pointer in C.) That means you have to assign an environment variable first.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set cnt=0
for %%g in (*.*) do (
  set "fullname=%%~fg"
  call :strLen fullname var1
 
  setlocal EnableDelayedExpansion
  echo Pfad ist !var1! Zeichen lang
  echo !fullname!
  echo(
  endlocal
 
  set /a cnt+=1
)     
echo Dateien im Verzeichnis = %cnt%
PAUSE
exit /b

Don't forget to append the :strLen function ...

Regards
aGerman

EmbraceLuke
Posts: 4
Joined: 20 May 2016 05:57

Re: Using the strLen function with my script. Please Help

#7 Post by EmbraceLuke » 22 May 2016 23:41

I want to thank you, aGerman, for your quick reply and helping me figuring out what exactly was wrong with my script. Needless to say, your script works and my boss was very happy we don't have to ask IT to download us some sketchy freeware-stuff. Thanks for that!
I am really not the best witch batch files but I'll take a deeper look into passinh variables in order to be able figuring out this stuff by myself in the future.

Have a great day and thanks again!

For any rookie who had the same problem as me I am going to post your piece of code together with the function in one piece to avoid all unnessecary confusion and help people out!

Code: Select all

@echo off &setlocal DisableDelayedExpansion

set cnt=0
for %%g in (*.*) do (
  set "fullname=%%~fg"
  call :strLen fullname var1
 
  setlocal EnableDelayedExpansion
  echo Pfad ist !var1! Zeichen lang
  echo !fullname!
  echo(
  endlocal
 
  set /a cnt+=1
)     
echo Dateien im Verzeichnis = %cnt%
PAUSE
exit /b

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b


aGerman wrote:Seems you missed the fact that you have to pass the variable name where your string was saved rather than the string itself. (It's similar to passing a reference in C++ or a pointer in C.) That means you have to assign an environment variable first.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set cnt=0
for %%g in (*.*) do (
  set "fullname=%%~fg"
  call :strLen fullname var1
 
  setlocal EnableDelayedExpansion
  echo Pfad ist !var1! Zeichen lang
  echo !fullname!
  echo(
  endlocal
 
  set /a cnt+=1
)     
echo Dateien im Verzeichnis = %cnt%
PAUSE
exit /b

Don't forget to append the :strLen function ...

Regards
aGerman

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Using the strLen function with my script. Please Help

#8 Post by Compo » 23 May 2016 08:00

The following should give you the file list complete with their full path string lengths and the total number of files.

Code: Select all

@Echo Off
SetLocal
Set "count=0"
For /F "Tokens=*" %%A In ('Dir/b/a-d') Do (For /F "Delims=:" %%B In (
   '(Echo("%%~fA"^&Echo(^)^|FindStr/O .') Do Set/A length=%%B-5
   Call Echo(%%~fA   %%length%%
   Set/A count+=1)
Echo( %count% File(s)
Timeout -1 1>Nul

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Using the strLen function with my script. Please Help

#9 Post by Thor » 23 May 2016 11:15

This use the "strlen.exe" utility from Aacini
viewtopic.php?f=3&t=3428&p=17101&hilit=strlen.hex#p17101

Code: Select all

@echo off
set Total_Dirs=0
for /f %%a in ('dir /b /ad') do set /a Total_Dirs+=1
setlocal enabledelayedexpansion
set Files=0
for /f %%a in ('dir /b') do (
   set "line=%%~fa"
   strlen line
   echo !line! !errorlevel!
   set /a Files+=1
)
set /a Total_Files=!Files!-!Total_Dirs!
set Total_Files
Last edited by Thor on 23 May 2016 13:19, edited 1 time in total.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Using the strLen function with my script. Please Help

#10 Post by thefeduke » 23 May 2016 11:41

Although EmbraceLuke has adopted aGerman's solution, I am pointing out the differences in the filecounts from some of the other suggestions:
Compo wrote:The following should give you the file list complete with their full path string lengths and the total number of files.
This yields a count one greater than the number shown by DIR. I note that Matt Williamson's suggestion adjusted the count down by one before displaying it. That seems to apply here as well.
foxidrive wrote:

Code: Select all

dir /b |find /c /v "" 
One must make allowances for this count, as it also contains the number of immediate subdirectories visible at that level.

John A.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Using the strLen function with my script. Please Help

#11 Post by Compo » 23 May 2016 11:58

thefeduke wrote:
Compo wrote:The following should give you the file list complete with their full path string lengths and the total number of files.
This yields a count one greater than the number shown by DIR.
You cannot compare a standard Dir command with one excluding directories and including all files, (hidden, system, etc.)
Does it show a count different to that shown in DIR/A-D?
my guess is that you have a desktop.ini or similar hidden away and spoiling your count.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Using the strLen function with my script. Please Help

#12 Post by thefeduke » 23 May 2016 12:54

Compo wrote:my guess is that you have a desktop.ini or similar hidden away and spoiling your count.
Wonderful. DIR/AH said it all for me. There was a hidden database for my syncing program. Forest for the trees. I missed the detail line difference that was there to see. Nonetheless, there are differences and thank you for clarifying one of them. It is always a good thing to understand more.
You don't always get what you want. Sometimes you get what you ask for.
In the contest between the programmer's desires and the rules of the system, the programmer must make the accommodation.

John A.

Post Reply