Delete files which got above 7 character names in files
Moderator: DosItHelp
Delete files which got above 7 character names in files
Hi,
I have files like this;
-30880.41142.647546296299.txt
122170.txt
880.41142.647546296299.txt
210880.txt
I need a batch code to delete those files which got more than 7 character in file name.
Thanks
I have files like this;
-30880.41142.647546296299.txt
122170.txt
880.41142.647546296299.txt
210880.txt
I need a batch code to delete those files which got more than 7 character in file name.
Thanks
Re: Delete files which got above 7 character names in files
does that include the minus sign?
Re: Delete files which got above 7 character names in files
foxidrive wrote:does that include the minus sign?
Yes, but I do not need the negative number files so u can delete those right away. In writing the code.
Re: Delete files which got above 7 character names in files
If it does include the minus then this should work - test it first. (Edited to delete negative numbers - untested)
The string length routine was posted earlier so it came in handy.
The string length routine was posted earlier so it came in handy.
Code: Select all
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do (
set "name=%%~na"
if "!name:~0,1!"=="-" (
del "%%a" 2>nul
) else (
call :strlen name len
if !len! GTR 7 del "%%a"
)
)
pause
goto :eof
: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
Re: Delete files which got above 7 character names in files
The above post was edited.
Re: Delete files which got above 7 character names in files
Code: Select all
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do (
set "name=%%~na"
if "!name:~0,1!"=="-" del "%%a" 2>nul
if NOT "!name:~7!"=="" del "%%a" 2>nul
)
Re: Delete files which got above 7 character names in files
FoxiDrive and Squashman,,,,,,
Many Thanks, both codes are working great.
Have a nice day
Many Thanks, both codes are working great.
Have a nice day
Re: Delete files which got above 7 character names in files
Squashman wrote:Code: Select all
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do (
set "name=%%~na"
if "!name:~0,1!"=="-" del "%%a" 2>nul
if NOT "!name:~7!"=="" del "%%a" 2>nul
)
Good thinking Batman!
Re: Delete files which got above 7 character names in files
Code: Select all
@echo off &setlocal enableextensions
for /f "delims=" %%f in ('dir *.txt /b/a-d') do (
echo %%~nf|findstr /r /c:"^-" /c:"........" >nul &&del /q "%%f"
)
Last edited by !k on 21 Aug 2012 07:27, edited 1 time in total.
Re: Delete files which got above 7 character names in files
!k wrote:Code: Select all
@echo off &setlocal enableextensions
for /f "delims=" %%f in ('dir *.txt /b/a-d') do (
echo %%~nf|findstr /rc:"^-" /rc:"........" >nul &&del /q "%%f"
)
We sure are skinning the cat today!
Re: Delete files which got above 7 character names in files
!k wrote:Code: Select all
@echo off &setlocal enableextensions
for /f "delims=" %%f in ('dir *.txt /b/a-d') do (
echo %%~nf|findstr /r /c:"^-" /c:"........" >nul &&del /q "%%f"
)
very nice, Brilliant K,,,,,,,,, Thank u very much.
u all guys are great, thank you all.