Solved

Long File Name

Posted on 2013-02-07
5
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

751 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