Delete files which got above 7 character names in files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Delete files which got above 7 character names in files

#1 Post by mohdfraz » 21 Aug 2012 05:01

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

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

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

#2 Post by foxidrive » 21 Aug 2012 05:28

does that include the minus sign?

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

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

#3 Post by mohdfraz » 21 Aug 2012 05:32

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.

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

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

#4 Post by foxidrive » 21 Aug 2012 05:34

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

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

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

#5 Post by foxidrive » 21 Aug 2012 05:38

The above post was edited.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

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

#6 Post by Squashman » 21 Aug 2012 05:55

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
)

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

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

#7 Post by mohdfraz » 21 Aug 2012 06:02

FoxiDrive and Squashman,,,,,,

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

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

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

#8 Post by foxidrive » 21 Aug 2012 06:28

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!

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#9 Post by !k » 21 Aug 2012 07:23

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.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

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

#10 Post by Squashman » 21 Aug 2012 07:26

!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

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

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

#11 Post by mohdfraz » 21 Aug 2012 07:46

!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.

Post Reply