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.
Check the length of filename
Moderator: DosItHelp
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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
-
- Posts: 1
- Joined: 05 Nov 2017 07:26
Re: Check the length of filename
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
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.Carson Clay wrote:I have a drawing and I have a field calling up the path and file name.
Please read the sticky "How to get help for a batch script - quickly!".
Of course:Carson Clay wrote:Is their a way to limit the size of the path displayed? I am using the Long Path Tool.
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