Solved .bat batch move file to folder based on filename + remove string

huiol09

New member
Local time
9:16 AM
Messages
53
I have a folder with thousands of filenames like this. I need to put them inside folders and remove the useless parts from the filenames. I need to do this before adding the files inside my XBMC library.

[ www.AnnoyingSpam.com ] Some.File.Name.With.A.Very.Long.String.avi
[ www.AnnoyingSpam.com ] Another.File.With.A.Very.Long.String.mpg
[ www.AnnoyingSpam.com ] Again.And.Again.mp4

- First, I want to strip the AnnoyingSpam.com tags in the files
- Then, create a folder based on the file name without the tag and without the extension
- Finally, move the file to the new folder
- Repeat for the rest of the files in the root directory of the batch file


So far all I got is a script that will create folder and move files. But it adds the tag "Folder" to each folder and it doesn't remove the AnnoyingSpam.com tag

@echo off

for /f "usebackq delims=?" %%a in (`dir /a-d /b`) do (
if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
)

goto :EOF

:func
set file=%~1
set dir=%file% Folder
md "%dir%" Folder 2>nul
move "%file%" "%dir%"
goto :EOF

So my question is how can i remove these strings from the folder names that are being created in the above script:

"[ www.AnnoyingSpam.com ]"

" Folder"
 

My Computer

OS
win7
So my question is how can i remove these strings from the folder names that are being created in the above script:

And my answer to that is, use the below script ;)


th-382773.bat
Code:
@echo off
setlocal EnableDelayedExpansion

set RemoveStrings="[ www.AnnoyingSpam.com ]", 
REM 'RemoveStrings' variable notes:
REM 	Surround strings to remove with double quotes. 
REM 	Separate each separate string to remove with a comma.

goto :main

:trim_leading_spaces VariableName
setlocal EnableDelayedExpansion
	set "_=!%~1!"
	set _=%_:"=%
	:trim_leading_spaces__while
	if "%_:~0,1%"==" " ( set "_=%_:~1%"& goto :trim_leading_spaces__while)
endlocal & set "%~1=%_%"
goto :eof

:trim_trailing_spaces VariableName
setlocal EnableDelayedExpansion
	set "_=!%~1!"
	set _=%_:"=%
	:trim_trailing_spaces__while
	if "%_:~-1%"==" " ( set "_=%_:~0,-1%"& goto :trim_trailing_spaces__while)
endlocal & set "%~1=%_%"
goto :eof

:main
for /f "delims=" %%I in (' dir /b /a:-d ^| findstr /vixc:"%~nx0" ') do (
	set file="%%~I"
	for %%J in (%REMOVESTRINGS%) do set "file=!FILE:%%~J=!"
	call :trim_leading_spaces  file
	call :trim_trailing_spaces file
	for %%J in ("!FILE!") do set "NewDirName=%%~nJ"
	md "!NEWDIRNAME!"
	ren "%%~I" "!FILE!"
	move "!FILE!" "!NEWDIRNAME!"
)
 
Last edited:

My Computer

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Thank you so much !!! It's working, almost perfectly !
I tested with a 100+ files, it worked with almost all of them except the filenames with spaces.

For example
"[ www.AnnoyingSpam.com ] Some.Title.With A.Space"
Will become just
"A.Space"

Is there a way to fix this ?

Thanks again :)
 

My Computer

OS
win7
Fixed.
 

My Computer

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Back
Top