dkrider471
dkrider471

Reputation: 9

Batch file that moves all movies to a specific folder file needs renamed

So my problem is that i'm using a batch file to move all movies, images, and so on. I'm using it to organize a very big hard drive over 20GB. So everything was working fine till I got to a new hard drive. I'm getting a file error saying the File name is to long. So my idea was to check if the file name exceeded 250 characters and if it did then to rename the file to only 200 with leaving the file extension alone. I've attached the current code i'm using to make this possible. My Question is how to make this possible. I'm thinking of adding an if command right before the if not exist but am stumped on how to check for the characters in the file name? Any help would be greatly appreciated!

 

@echo off

setlocal EnableDelayedExpansion

set TESTFOLDER=L:\AllMovies
md "%TESTFOLDER%"

for /f "tokens=*" %%i in ('dir /s /ah /b /a-d L:\*.mov or L:\*.mp4') do (
  call :copyfile "%%~i"
) 

goto :eof

:copyfile
set fname=%~nx1
set counter=0

if not exist "%TESTFOLDER%\%dest%\!fname!" goto docopy

:recheck
if exist "%TESTFOLDER%\%~n1_!counter!%~x1" (
  set /a counter+=1
  goto recheck
)
set fname=%~n1_copy!counter!%~x1

:docopy
copy "%~1" "%TESTFOLDER%\%dest%\!fname!"

goto :eof

endlocal

Upvotes: 0

Views: 457

Answers (1)

Serenity
Serenity

Reputation: 86

You can always prepend \\?\ to a filename to ignore filename checks. Windows supports filenames up to 32K. However programs tend to only support 260 total characters for the path (of which 255 max for the name).

Write your name to a file then read the file size, it will be the filename size.

Upvotes: 0

Related Questions