Solved

Long File Name

Posted on 2013-02-07
5
443 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 51

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 51

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 51

Expert Comment

by:Bill Prew
ID: 38868285
Welcome.

~bp
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

17 Experts available now in Live!

Get 1:1 Help Now