Solved

Long File Name

Posted on 2013-02-07
5
449 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 56

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 56

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 56

Expert Comment

by:Bill Prew
ID: 38868285
Welcome.

~bp
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

615 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