Page 1 of 1
Check the length of filename
Posted: 07 Jun 2009 20:15
by valerie
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.
Posted: 10 Jun 2009 11:51
by avery_larry
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
Re: Check the length of filename
Posted: 05 Nov 2017 07:31
by Carson Clay
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.
Re: Check the length of filename
Posted: 09 Nov 2017 02:13
by penpen
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