Reputation: 1095
I try to get (echo) the files in a directory (with subdirectorys) where the filename is longer than 16 characters. therefore I play around with this script:
@ECHO OFF
setlocal enabledelayedexpansion
for /R %%f in (*) do (
set /p val=<%%f
REM echo "fullname: %%f"
REM echo "path: %%~pf"
REM echo "name: %%~nxf"
REM set test=%%~nxf
REM echo %test%
if "%%~nxf:~16%" == "" echo "Less than 16 characters."
if not "%%~nxf:~16%" == "" echo "Bigger than 16 characters."
)
pause
GOTO :EOF
But I get on every file "Bigger than 16 characters."! I also try to set the var %test%
to %%~nxf
(the filename) but get only "Echo is off" because the var %test%
is not set?
How to set the var %test%
with the filename and how to get "Less than 16 characters." if the filename has only 6 characters?
Upvotes: 0
Views: 259
Reputation:
You are not using the delayedexpansion
you enabled earlier, also you cannot use substitution or positioning on a metavariable, you need to assign it to a variable first:
@echo off & setlocal enabledelayedexpansion
for %%i in (*) do (
set "line=%%~nxi"
if "!line:~16!"=="" (
echo !line! is less than 16 characters.
) else echo !line! is 16 or more characters.
)
see the help cmd
by running:
set /?
Scroll down to Delayed environment
section.
Upvotes: 1
Reputation: 56180
Your real problem is I try to get (echo) the files in a directory (with subdirectories) where the filename is longer than 16 characters.
.
dir /s /b ????????????????*.*
won't work, because the wildcard ?
means zero or one character
.
Gladly where
handles wildcards differently: ?
is exactly one character
and *
is any number of characters (including zero)
.
So the following line should do exactly what you want: display all files whose name plus extension (according to your code attempt %~nxf
) is 16 characters or more:
where /r . ????????????????*
To get all files whose name without extension is 16 chars or more:
where /r . ????????????????*.*
(might get false positives, as file.with.several.extensions.txt
or file-without-extension
- where false
depends on your expectations)
Upvotes: 0