Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: .bat batch move file to folder based on filename + remove string

03 Oct 2015   #1
anarchoi

win7 64bits
 
 
.bat batch move file to folder based on filename + remove string

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

Quote:
@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 System SpecsSystem Spec
.
04 Oct 2015   #2
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by anarchoi View Post
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!"
)
My System SpecsSystem Spec
04 Oct 2015   #3
anarchoi

win7 64bits
 
 

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 System SpecsSystem Spec
.

04 Oct 2015   #4
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Fixed.
My System SpecsSystem Spec
04 Oct 2015   #5
anarchoi

win7 64bits
 
 

Thank you !
My System SpecsSystem Spec
Reply

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




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
batch&task to create new folder and move all desktop file to it
hi so work in an recruitment office and im trying to create a batch file that the computers can run on a schedule to create a folder with that days date on it and move all files from the desktop to that folder( also created on the desktop), i want the code to only move files and leave folders and...
General Discussion
Move files from original folder to existing folders - w batch file?
My files have a format of "ID_date_time.d7d Ex: 11111-5_2013_10_07_142149.d7d The ID is 1111-5 and was tested 10/7/2013 at 2:21:49 PM and the file extension is always .d7d The local files are automatically saved here by the program generating the data: C:\Local\Data I want them copied...
General Discussion
Batch file that replaces a filename in a txt file
I've got a task that I need to repeat 100 times in a CAD program. I've written a small script in the CAD program that takes a model and does a bunch of things to it, saves it, and closes it. (That part is taken care of, but I want to automate it) That script is written in a text file. But it's...
General Discussion
Batch file to move (many) duplicted folder contents - Help!!
Hope I can explain my difficulty. I have duplicated folders album name for all my music files. As there is quite a considerable number I wondered if it was possible to batch file (automate) the process. I can not delete "d" as this would delete all the tracks. What I need to do is move the...
General Discussion
Batch file , change filename with system date and time infor
I wrote a bat file, to change a file name, test.txt to testMonthDayYearTime.txt here is what i wrote: ren c:\example\test.txt test%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%%.txt But after running, the file name is test12182010 093012.txt, a space is ...
General Discussion
How to create a batch file that will move files based on the file type
How to create a batch file that will move files based on the file type? My Requirement : I need a batch file to copy all .doc files in all my HardDisk to a removable disk say (Z: drive) . Can anyone make it possible using a batch file?Please...Its urgent...
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 01:40.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App