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


  1. Posts : 53
    win7 64bits
       #1

    .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

    @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


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    anarchoi said:
    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 by Pyprohly; 04 Oct 2015 at 21:33.
      My Computer


  3. Posts : 53
    win7 64bits
    Thread Starter
       #3

    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


  4. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #4

    Fixed.
      My Computer


  5. Posts : 53
    win7 64bits
    Thread Starter
       #5

    Thank you ! :)
      My Computer


 

  Related Discussions
Our Sites
Site Links
About 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 18:21.
Find Us