Check the length of filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
valerie
Posts: 1
Joined: 04 Jun 2009 05:34

Check the length of filename

#1 Post by valerie » 07 Jun 2009 20:15

How to create a batch file to check the length of filename as below:
if file exist in c:\test folder with wildcard cs*.txt and filename length is 15 then rename the file
eg.filename = CSIYYMMDD0001.txt, rename to CSI0001.txt(remove the YYMMDD)
Thank you in advance.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 10 Jun 2009 11:51

Code: Select all

@echo off

set "folder=c:\test"

for /f "usebackq tokens=* delims=" %%a in (`dir /b "%folder%\cs*.txt"`) do call :process "%%~a"
goto :eof

:process
set "filename=%~1"

set "cnt=0"
:loop
if not defined filename goto continue
set /a cnt+=1
set "filename=%filename:~1%"
goto loop
:continue
echo length=%cnt%
if not %cnt%==17 goto :eof
set "filename=%~1"
set "filename=%filename:~0,3%%filename:~9%"
ren "%folder%\%~1" "%filename%"
goto :eof

Carson Clay
Posts: 1
Joined: 05 Nov 2017 07:26

Re: Check the length of filename

#3 Post by Carson Clay » 05 Nov 2017 07:31

I have a drawing and I have a field calling up the path and file name. Is their a way to limit the size of the path displayed? I am using the Long Path Tool.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check the length of filename

#4 Post by penpen » 09 Nov 2017 02:13

Carson Clay wrote:I have a drawing and I have a field calling up the path and file name.
Actually it seems your actual description isn't related to this topic (in which case you should find a related one or create a new topic), or you have to describe your issue more detailed.
Please read the sticky "How to get help for a batch script - quickly!".
Carson Clay wrote:Is their a way to limit the size of the path displayed? I am using the Long Path Tool.
Of course:
Just don't use a tool if you explicitely want to avoid its result.
In your case you probably want to access the short path - just parse the output of "dir /X", or use the short path syntax of "for-variables" ("test.bat"):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for %f in (*) do echo "%~sf"
endlocal
penpen

Post Reply