Solved

Long File Name

Posted on 2013-02-07
5
445 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 52

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 52

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 52

Expert Comment

by:Bill Prew
ID: 38868285
Welcome.

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - Passing arguments 8 58
Running Visio Macro from VBS File 3 56
How to batch remove spreadsheet password 19 146
Carriage Return in Batch File 5 36
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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now