Page 1 of 1

Delete files which got above 7 character names in files

Posted: 21 Aug 2012 05:01
by mohdfraz
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

Re: Delete files which got above 7 character names in files

Posted: 21 Aug 2012 05:28
by foxidrive
does that include the minus sign?

Re: Delete files which got above 7 character names in files

Posted: 21 Aug 2012 05:32
by mohdfraz
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

Posted: 21 Aug 2012 05:34
by foxidrive
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.

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

Posted: 21 Aug 2012 05:38
by foxidrive
The above post was edited.

Re: Delete files which got above 7 character names in files

Posted: 21 Aug 2012 05:55
by Squashman

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

Posted: 21 Aug 2012 06:02
by mohdfraz
FoxiDrive and Squashman,,,,,,

Many Thanks, both codes are working great.
Have a nice day

Re: Delete files which got above 7 character names in files

Posted: 21 Aug 2012 06:28
by foxidrive
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

Posted: 21 Aug 2012 07:23
by !k

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"
)

Re: Delete files which got above 7 character names in files

Posted: 21 Aug 2012 07:26
by Squashman
!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! :D

Re: Delete files which got above 7 character names in files

Posted: 21 Aug 2012 07:46
by mohdfraz
!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.