User User name Password  
   
Friday 8.11.2024 / 12:57
Search AfterDawn Forums:        In English   Suomeksi   På svenska
afterdawn.com > forums > software, operating systems and more > windows - software discussion > batch - enum folder with less than 6 chars
Show topics
 
Forums
Forums
BATCH - Enum folder with less than 6 chars
  Jump to:
 
Posted Message
gg67
Newbie
_
10. November 2011 @ 02:18 _ Link to this message    Send private message to this user   
Hello,

Regarding the old thread "finding Length of String in BATCH file" (/thread_view.cfm/514228), I just want to bring another way to do it :


@Echo Off
SetLocal EnableDelayedExpansion
	Set vRoot=%~dp0Sample\
	For /F "delims=*" %%p In (dir /b /s /ad /on "%vRoot%") Do (
		Set vName=%%~np
		If "!vName!"=="!vName:~0,6!" ( Echo "!vName!" ^<= 6 ) Else ( Echo "!vName!" ^> 6 )
	)
EndLocal
Pause

Save this code in a batch and create the following dir structure in the batch's folder :
\Sample
\Sample\1 Folder
\Sample\2 Fld
\Sample\1 Folder\1a Fld
\Sample\1 Folder\1b Fldr
\Sample\1 Folder\1c Fl
\Sample\2 Fld\2a Fld
\Sample\2 Fld\2b Fldr
\Sample\2 Fld\2c Fl



And here for the long commented version :
[code]@echo off

setlocal enabledelayedexpansion
if errorlevel 1 GoTo noEnvExpansionError


:: ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
set vRoot=%~dp0Sample\
::echo vRoot = "%vRoot%"

Set vPath=
Set vName=

For /F "delims=*" %%a In (dir /b /s /ad /on "%vRoot%") Do (

REM %%a contains the folder full path (we store it in vPath only to display the relative path below, as replacement cannot be done on %%a)
Set vPath=%%a

REM drops the root path for clarity (replaces it with \)
echo path !vPath:%vRoot%=\!

REM grabs the folder name
Set vName=%%~na
echo name !vName!

REM extracts 6 characters starting @ the 1st character (zero-based index)
echo start !vName:~0,6!

If "!vName!"=="!vName:~0,6!" (
echo ^<= 6
) else (
echo ^> 6
)

echo.
)


Pause
GoTo end


:: ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
:noEnvExpansionError
echo Unable to enable the Delayed environment variable expansion : script aborted.

:end
endlocal
[/code]
Greetings

This message has been edited since posting. Last time this message was edited on 10. November 2011 @ 02:21

afterdawn.com > forums > software, operating systems and more > windows - software discussion > batch - enum folder with less than 6 chars
 

Digital video: AfterDawn.com | AfterDawn Forums
Music: MP3Lizard.com
Gaming: Blasteroids.com | Blasteroids Forums | Compare game prices
Software: Software downloads
Blogs: User profile pages
RSS feeds: AfterDawn.com News | Software updates | AfterDawn Forums
International: AfterDawn in Finnish | AfterDawn in Swedish | AfterDawn in Norwegian | download.fi
Navigate: Search | Site map
About us: About AfterDawn Ltd | Advertise on our sites | Rules, Restrictions, Legal disclaimer & Privacy policy
Contact us: Send feedback | Contact our media sales team
 
  © 1999-2024 by AfterDawn Ltd.

  IDG TechNetwork