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