Solved

Long File Name

Posted on 2013-02-07
5
447 Views
Last Modified: 2013-02-08
Hello Experts,

I need to examine file names to determine if they are greater than 200 characters long, ONLY THE FILE NAME NOT INCLUDING THE PATH. If they are greater than 200 characters long I need to rename the file by deleting all spaces. I need to do this from a batch file.

Example:

some very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very  long long long long long long long long long long long long long long long long long long long long long long long long file name.txt

would become

someveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongfilename.txt

Thanks!
0
Comment
Question by:eddiepardon
  • 3
  • 2
5 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 38865366
This should do the trick, just edit the BaseDir to the location of your files.

Keep in mind this will not work with file names that exceed the normal Windows file length.

@echo off
setlocal EnableDelayedExpansion

set MaxLength=200
set BaseDir=C:\Temp

for %%A in ("%BaseDir%\*.txt") do (
  call :ChkLen "%%~A"
)
exit /b

REM Subroutine to remove spaces from a file name if longer than 200 characters
:ChkLen [filename]
  call :StrLen "%~n1" Len
  if %Len% GTR %MaxLength% (
    set Str=%~n1
    ren "%~1" "!Str: =!%~x1"
  )
  exit /b

REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & 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")
  set /a %~2=%_len%
  exit /b

Open in new window

~bp
0
 

Author Comment

by:eddiepardon
ID: 38865718
BP,

This looks great I just have one question...

I want the set BaseDir=C:\Temp to be what ever the current location that the bat file is running from. The location will be different each time this bat file runs so it is not practical to hard code it.

Thanks!
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38866617
This should work in that mode:

@echo off
setlocal EnableDelayedExpansion

set MaxLength=200

for %%A in ("*.txt") do (
  call :ChkLen "%%~A"
)
exit /b

REM Subroutine to remove spaces from a file name if longer than 200 characters
:ChkLen [filename]
  call :StrLen "%~n1" Len
  if %Len% GTR %MaxLength% (
    set Str=%~n1
    ren "%~1" "!Str: =!%~x1"
  )
  exit /b

REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & 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")
  set /a %~2=%_len%
  exit /b

Open in new window

~bp
0
 

Author Closing Comment

by:eddiepardon
ID: 38868260
BP,

Thanks for the help!
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 38868285
Welcome.

~bp
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question