Here is a collection of useful DOS BATCH and VBSCRIPT scripts for processing, moving, sorting of files.
The code for all scripts can be downloaded in a single ZIP file called: Roy-FileProcScripts.zip
The ZIP archive also contains a nice intro for RoySAC.com, which I recommend to check out :).
ImageFilesSort2Dirs.bat
IMAGES Cleanup/Sorting Batch Script
- looks for IMAGE files in current directory (Extensions JPG, PNG, GIF, BMP, LBM, TGA, PCX, SVG, TIF, IFF)
- looks for "-" in file name (assumed to be the separator between Artist Name and Image Title)
- parses string before first "-", trims trailing spaces
- checks if directory with that name exists
4.1 If not, it creates it (artist name) - moves all image files of the artist into the artist dir
1: @echo off
2: :: -------------------------------------------------------
3: :: IMAGES Cleanup/Sorting Batch Script
4: :: -------------------------------------------------------
5: :: 1. looks for IMAGE files in current directory
6: :: (Extensions JPG, PNG, GIF, BMP, LBM, TGA, PCX, SVG, TIF, IFF)
7: :: 2. looks for "-" in file name (assumed to be
8: :: the separator between Artist Name and Image Title)
9: :: 3. parses string before first "-", trims trailing spaces
10: :: 4. checks if directory with that name exists
11: :: 4.1 If not, it creates it (artist name)
12: :: 5. moves all image files of the artist into the artist dir
13: ::
14: :: Batch Script by Carsten Cumbrowski aka Roy/SAC
15: :: visit http://www.roysac.com
16:
17:
18: setlocal enabledelayedexpansion
19: CLS
20: SET DC=DIR /A-D /ON /B
21: SET EXT=JPG PNG GIF BMP LBM TGA PCX SVG TIF IFF
22:
23: for %%f in (%EXT%) DO SET "DS=!DS!^^&%DC% *.%%f 2^^>NUL"
24: SET "DS=%DS:~2%"
25:
26: ::only list files with IMAGE extension, exclude directories
27: for /f "tokens=*" %%A in ('%DS%') do (
28: if NOT "%%A"=="File Not Found" (
29: call :PERFACTION "%%~A%"
30: )
31: )
32: echo.
33: echo Done!
34: echo.
35: pause
36: goto :EOF
37:
38:
39: :PERFACTION
40: ::look for dash in file name, parse str before dash
41: for /F "tokens=1 delims=-" %%B in ("%~1") do (
42: ::trim trailing spaces from parsed string
43: set str=%%~B%
44: for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
45: )
46: call :PROCFILE "%str%"
47: goto :EOF
48:
49:
50: :PROCFILE
51: ::check if directory exists and move file(s)
52: set f2="%~1"
53: if {%~x1}=={} (
54: echo %f2%
55: if NOT exist %f2% MD %f2%
56: FOR %%f in (%EXT%) DO MOVE "%~1*-*.%%f" %f2% 2>NUL
57: )
58: goto :EOF
59:
60:
MoveDirsToAlphaSub.bat
MOVE Subfolders and their content (including their subdirectories) to Destination Folder by First Letter of the Subfolder Name. I created this batch script to be able to sort content that I had in sub directories sorted by group names to an additional sub directory level by letter. I only needed the script to move the files in the sub directory itself, not worrying about additional sub directories that the folder might contains, but I extended the script to move those over as well. Here is an illustration of what the script does, using the example of mod tracker files in sub directories by group name.
Source:
Ackerlight
Ackerlight1.mod
Backlash
Backlash1.mod
The script moves everything to a target that will look like the following:
A
Ackerlight
Ackerlight1.mod
B
Backlash
Backlash1.mod
USAGE of the script:
MOVEDIRSTOALPHASUB.BAT [SOURCEDIR] [DESTDIR]
[SOURCEDIR] - Optional - Source Directory
[DESTDIR] - Optional - Destination Directory
1: @ECHO OFF
2: CLS
3: REM ============================================================
4: REM MOVE Subfolders and their content (including their subdirectories)
5: REM to Destination Folder by First Letter of the Subfolder Name
6: REM
7: REM Source:
8: REM axxxxxx DIR
9: REM xxxxxxx DIR
10: REM yyyyyyy FILE
11: REM yyyyyyy FILE
12: REM
13: REM Destination:
14: REM A DIR
15: REM axxxxxx DIR
16: REM xxxxxxx DIR
17: REM yyyyyyy FILE
18: REM yyyyyyy FILE
19: REM
20: REM USAGE:
21: REM MOVEDIRSTOALPHASUB.BAT [SOURCEDIR] [DESTDIR]
22: REM
23: REM [SOURCEDIR] - Optional - Source Directory
24: REM [DESTDIR] - Optional - Destination Directory
25: REM
26: REM Written by Roy/SAC in 2009
27: REM
28: REM ============================================================
29:
30: REM Backup original directory location
31: SET ORG=%CD%
32:
33: REM If no source directory is specified
34: REM use current directory location as source
35: IF "%~1"=="" (
36: SET SROOT=%CD%
37: ) ELSE (
38: SET SROOT=%~1
39: )
40: REM If no destination directory is specified
41: REM use current directory as destination
42: IF "%~2"=="" (
43: SET DROOT=%CD%
44: ) ELSE (
45: SET DROOT=%~2
46: )
47:
48:
49: REM Goto Source Directory
50: CD /D "%SROOT%"
51:
52: REM Get all Sub-Folders in Source Directory
53: for /f "tokens=*" %%A in ('DIR /ON /B /AD *.* 2^>NUL') DO CALL :ACTION "%%~A"
54:
55: REM Return to Original Directory
56: CD /D "%ORG%"
57: ECHO ===============================================
58: ECHO DONE! Window Closes Automatically in 10 Seconds
59: ECHO ===============================================
60: GOTO :ENDOFAPP
61:
62: REM ===========================================================
63: REM ACTION - Folder Processing
64: REM ===========================================================
65: :ACTION
66: REM Extract Folder Name
67: SET STR1="%~nx1"
68:
69: REM Extract First Letter of Folder Name
70: SET "STR=%STR1:~1,1%"
71:
72: REM Convert Letter to Upper Case
73: for %%X in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
74: "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
75: "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do (
76: call set "STR=%%STR:%%~X%%"
77: )
78:
79: REM Jump to destination directory
80: CD /D "%DROOT%"
81:
82: REM If letter directory does not exist, create it
83: IF NOT exist "%STR%" MD "%STR%"
84:
85: REM Jump into letter directory
86: CD /D "%DROOT%\%STR%"
87:
88: REM If target directory does not exist, create it
89: IF NOT exist "%~nx1" MD "%~nx1"
90:
91: REM copy all files and subfolders from source to target directory
92: XCOPY /S /E "%SROOT%\%~nx1\*.*" "%DROOT%\%STR%\%~nx1" 2>NUL
93:
94: REM Jump to source directory
95: CD /D "%SROOT%\%~nx1"
96:
97: REM Delete all files in source directory
98: DEL /S /Q *.* 2>NUL
99:
100: REM Jump to root source directory
101: CD /D "%SROOT%"
102:
103: REM Remove source directory and its sub directories
104: RMDIR /S /Q "%~nx1" 2>NUL
105: GOTO :EOF
106:
107:
108: REM ===========================================================
109: REM End of App - Script closes automatically after 10 seconds
110: REM ===========================================================
111: :ENDOFAPP
112:
113: FOR /l %%a in (10,-1,1) do (
114: TITLE %title% -- closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL
115: )
116: TITLE Press any key to close the application&ECHO.
MoveToAlphabetSubFolders.bat
This script simply moves files in a folder to sub folders with the first letter as name. For example, if you have a folder with the following files:
Ackerlight1.mod
Backlash1.mod
The script will move them to subfolders like this:
A
Ackerlight1.mod
B
Backlash1.mod
Note: this version of the script overwrites files with the same name in the destination folder. Check out the script below, which does the same as this script, but does not overwrite files in the destination directory.
1: @ECHO OFF
2: CLS
3: IF NOT {%1}=={} (
4: CD /d "%~1"
5: )
6: ECHO Processing Folder: %CD%
7:
8: FOR %%f IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z REST) DO
CALL :MoveFiles "%%f"
9: GOTO :EOF
10:
11: :MoveFiles
12: ECHO Processing: %1 ...
13: IF %1=="REST" Goto :MoveRest
14: IF NOT EXIST %1 MD %1
15: REM FOR %%f IN (%1*) DO MOVE "%%f" %1>NUL
16: move %1* %1\ >NUL 2>&1
17: GOTO :EOF
18:
19: :MoveRest
20: IF NOT EXIST 0-9 MD 0-9
21: MOVE * 0-9>NUL
22: GOTO :EOF
MoveToAlphabetSubFoldersWithoutOverwrite.bat
This script does the same as the one above with the difference that this script does not overwrite files with the same name that already exist in the destination directory.
1: @ECHO OFF
2: CLS
3: IF NOT {%1}=={} (
4: CD /d "%~1"
5: )
6: ECHO Processing Folder: %CD%
7: ECHO ------------------------------------------------------------------------------------------
8: FOR %%f IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z REST) DO
CALL :MoveFiles "%%f"
9: ECHO ------------------------------------------------------------------------------------------
10: ECHO Done!
11: ECHO.
12: PAUSE
13: GOTO :EOF
14:
15: :MoveFiles
16: ECHO Processing: %1 ...
17: IF %1=="REST" Goto :MoveRest
18: IF NOT EXIST %1 MD %1
19: REM FOR %%f IN (%1*) DO MOVE "%%f" %1\ >NUL 2>&1
20: Echo N|move /-Y %1* %1\ >NUL 2>&1
21: RmDir "%1">NUL 2>&1
22: GOTO :EOF
23:
24: :MoveFiles2
25: Echo N|move /-Y %1* 0-9\ >NUL 2>&1
26: GOTO :EOF
27:
28: :MoveRest
29: IF NOT EXIST 0-9 MD 0-9
30: FOR %%a IN (0 1 2 3 4 5 6 7 8 9 # $ % ~ ! ^ & ( ) - _ + = [ ] { } ' ; , `) DO
CALL :MoveFiles2 "%%a"
31: RmDir "0-9">NUL 2>&1
32: GOTO :EOF
MoveToSubFoldersByExt.bat
This script requires also the .VBS script “Extensions.vbs” (see below).
It sorts files away into sub directories with the extension name. I used it to sort away tracker modules by file format. For example:
Ackerlight1.mod
Backlash1.xm
Would be moved into folders like this:
MOD
Ackerlight1.mod
XM
Backlash1.xm
1: @ECHO OFF
2: CLS
3: IF NOT {%1}=={} (
4: CD /d "%~1"
5: )
6: Set WorkP=%CD%
7: ECHO Processing Folder: %WorkP%!
8: Call :GetScrPath "extensions.vbs"
9: Echo [%CmdL%]
10: FOR /F %%f IN ('%CmdL%') DO CALL :MoveFiles "%%f"
11: Call :MoveRest
12: GOTO :EOF
13:
14: :MoveFiles
15: IF NOT EXIST "%~1" MD "%~1"
16: FOR %%f IN (*.%~1) DO MOVE "%%f" "%~1\">NUL 2>&1
17: GOTO :EOF
18:
19: :MoveRest
20: IF NOT EXIST "NoExtension" MD "NoExtension"
21: MOVE "*." "NoExtension\">NUL 2>&1
22: RmDir "NoExtension">NUL 2>&1
23: GOTO :EOF
24:
25:
26: :GetScrPath
27: Set CmdL=cscript.exe /nologo "%~$PATH:1" "%WorkP%"
28: GOTO :EOF
extensions.vbs
This is a supporting script for the batch script “MoveToSubFoldersByExt.bat”. It returns a list of all file extensions for the files in the provided directory location.
1: Dim objCmdlineArguments: Set objCmdlineArguments = Wscript.Arguments
2: Dim oFso: Set oFso = CreateObject("Scripting.FileSystemObject")
3: Dim oFile, oFolder, sPath
4: Dim sExt, a, aExt
5: Dim sExtensions: sExtensions=""
6:
7:
8: If objCmdlineArguments.Unnamed.Count > 0 then
9: sPath = objCmdlineArguments.Unnamed(0)
10: If Not oFso.FolderExists(sPath) Then
11: sPath = oFso.GetAbsolutePathName(".")
12: End If
13: Else
14: sPath = oFso.GetAbsolutePathName(".")
15: End If
16:
17: Set oFolder = oFSO.GetFolder(sPath)
18:
19: 'Process Files in Folder
20: For each oFile in oFolder.Files
21: sExt = LCase(oFso.GetExtensionName(oFile.Name))
22: If sExt <> "" And InStr(1,sExtensions,sExt & "|",1) = 0 Then
23: sExtensions = sExtensions & sExt & "|"
24: End If
25: Next
26:
27: If sExtensions <> "" Then
28: sExtensions = Left(sExtensions,Len(sExtensions)-1)
29: If InStr(1,sExtensions,"|",1) > 0 Then
30: aExt = Split(sExtensions,"|")
31: sExtensions=""
32: QSort aExt, LBound(aExt), UBound(aExt)
33: For a = 0 To UBound(aExt)
34: If aExt(a) <> "" Then
35: sExtensions = Join(aExt,vbcrlf)
36: End If
37: Next
38: End If
39: WScript.Echo sExtensions
40: End If
41:
42: Set oFolder = Nothing
43: Set ofso = Nothing
44:
45:
46: Sub QSort(aData, iaDataMin, iaDataMax)
47: Dim Temp
48: Dim Buffer
49: Dim iaDataFirst
50: Dim iaDataLast
51: Dim iaDataMid
52:
53: iaDataFirst = iaDataMin ' Start current low and high at actual low/high
54: iaDataLast = iaDataMax
55:
56: If iaDataMax <= iaDataMin Then Exit Sub ' Error!
57: iaDataMid = (iaDataMin + iaDataMax) \ 2 ' Find the approx midpoint of the array
58:
59: Temp = aData(iaDataMid) ' Pick as a starting point (we are making
60: ' an assumption that the data *might* be
61: ' in semi-sorted order already!
62:
63: Do While (iaDataFirst <= iaDataLast)
64: 'Comparison here
65: Do While aData(iaDataFirst) < Temp
66: iaDataFirst = iaDataFirst + 1
67: If iaDataFirst = iaDataMax Then Exit Do
68: Loop
69:
70: 'Comparison here
71: Do While Temp < aData(iaDataLast)
72: iaDataLast = iaDataLast - 1
73: If iaDataLast = iaDataMin Then Exit Do
74: Loop
75:
76: If (iaDataFirst <= iaDataLast) Then ' if low is <= high then swap
77: Buffer = aData(iaDataFirst)
78: aData(iaDataFirst) = aData(iaDataLast)
79: aData(iaDataLast) = Buffer
80: iaDataFirst = iaDataFirst + 1
81: iaDataLast = iaDataLast - 1
82: End If
83: Loop
84:
85: If iaDataMin < iaDataLast Then ' Recurse if necessary
86: QSort aData, iaDataMin, iaDataLast
87: End If
88:
89: If iaDataFirst < iaDataMax Then ' Recurse if necessary
90: QSort aData, iaDataFirst, iaDataMax
91: End If
92:
93: End Sub 'QSort
MP3FileSort2Dirs.bat
I published this batch script already on my blog. It sorts MP3 files away to sub directories by artist name, assuming that the MP3 files start with the artist name followed by a “-“ character (then followed by the song title for example).
1: @echo off
2: :: -------------------------------------------------------
3: :: MP3 Cleanup/Sorting Batch Script
4: :: -------------------------------------------------------
5: :: 1. looks for MP3 files in current directory
6: :: 2. looks for "-" in file name (assumed to be
7: :: the separator between Artist Name and Song Title)
8: :: 3. parses string before first "-", trims trailing spaces
9: :: 4. checks if directory with that name exists
10: :: 4.1 If not, it creates it (artist name)
11: :: 5. moves file into the directory
12: ::
13: :: Batch Script by Carsten Cumbrowski aka Roy/SAC
14: :: visit http://www.roysac.com
15:
16: setlocal enabledelayedexpansion
17: cls
18: ::only list files with extension MP3, exclude directories
19: for /f "tokens=*" %%A in ('DIR /A-D /ON /B "*.MP3"^|SORT /REVERSE') do (
20: call :PERFACTION "%%~A%"
21: )
22: echo.
23: echo Done!
24: echo.
25: pause
26: goto :EOF
27:
28:
29: :PERFACTION
30: ::look for dash in file name, parse str before dash
31: for /F "tokens=1 delims=-" %%B in ("%~1") do (
32: ::trim trailing spaces from parsed string
33: set str=%%~B%
34: for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
35: )
36: call :PROCFILE "%str%"
37: goto :EOF
38:
39:
40: :PROCFILE
41: ::check of directory exists and move file
42: set f2="%~1"
43: if {%~x1}=={} (
44: echo %f2%
45: if NOT exist %f2% MD %f2%
46: move "%~1*.MP3" %f2%
47: )
48: goto :EOF
49:
50:
MoveFilesAutoRenDup.vbs
This script copies all files from a source directory, including its subfolders to a destination directory without any subfolders. Since the subfolders of the source might contain files with the same name (such as file_id.diz files), this script automatically renames those files, appending [2] to the first duplicate, [3] to the next and so on.
1: Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
2: Dim arguments: Set arguments = Wscript.arguments
3: Const ForAppending = 8, ForReading = 1, ForWriting = 2
4:
5: Dim sTo
6: Dim sFrom
7:
8: if arguments.unnamed.count = 0 then
9: sFrom = "N*A"
10: Do While sFrom <> "" and FSO.FolderExists(sFrom) = false
11: sFrom = InputBox("Move All Files and Auto Rename Dupes", _
12: "Enter Source Directory: ", fso.GetAbsolutePathName("."))
13: Loop
14: if sFrom = "" then
15: WScript.Quit(1)
16: End if
17: else
18: sFrom = arguments.unnamed.Item(0)
19: end if
20: if not FSO.FolderExists(sFROM) then
21: WScript.Quit(2)
22: end if
23:
24: if arguments.unnamed.count <= 1 then
25: sTo="N*A"
26: Do While sTo <> "" and FSO.FolderExists(sTo) = false
27: sTo = InputBox("Move All Files and Auto Rename Dupes", _
28: "Source Directory: " & vbcrlf & sFrom & vbcrlf & _
29: "Enter Destination Directory: ", FSO.GetParentFolderName(sFrom))
30: Loop
31: if sTo = "" then
32: WScript.Quit(1)
33: end if
34: else
35: sTo = arguments.unnamed.Item(1)
36: end if
37: if not FSO.FolderExists(sTo) then
38: WScript.Quit(2)
39: end if
40:
41:
42:
43:
44: Dim iFilesCount: iFilesCount = 1
45: Dim sSrc, sDest, DC, sExt, sBase, oFOld
46:
47: Set oFold = FSO.GetFolder(sFrom)
48:
49: if lcase(sTo) <> lcase(sFrom) then
50: ShowFiles oFold.Path
51: end if
52:
53: ShowSubFolders oFold
54:
55: Wscript.echo iFilesCount & " files moved"
56:
57:
58: '========================================================
59:
60: Sub ShowSubFolders(Folder)
61: Dim SubFolder
62:
63: For Each Subfolder in Folder.SubFolders
64: ShowFiles Subfolder.Path
65: ShowSubFolders Subfolder
66: Next
67: End Sub
68:
69: '----------------------------------------
70: Sub ShowFiles(sPath)
71: Dim oFile, oFolder
72:
73: iFolderCount = iFolderCount + 1
74: Set oFolder = FSO.GetFolder(sPath)
75: iFilesCount = iFilesCount + oFolder.Files.count
76:
77: For each oFile in oFolder.Files
78: sDest = FSO.BuildPath(sTo, oFile.Name)
79: sExt = FSO.GetExtensionName(sDest)
80: sBase = left(oFile.Name,len(oFile.name)-len(sExt)-1)
81: DC = 1
82: Do While FSO.FileExists(sDest) = true
83: ' Wscript.sleep 500
84: DC = DC + 1
85: sDest = FSO.BuildPath(sTo, sBase & "[" & DC & "]." & sExt)
86: Loop
87: sSrc = FSO.GetAbsolutePathName(FSO.BuildPath(sPath,oFile.Name))
88:
89: FSO.MoveFile sSrc, sDest
90:
91: Next
92: End Sub
93:
94:
CleanUp.bat
I published this script also already. It simply checks all subdirectories of the provided path and removes all those that are empty (have no files).
1: REM CleanUp.bat
2: @echo off
3: set Folder="%~1"
4: if %Folder%=="" @echo Syntax CleanUp Folder&goto :EOF
5: if not exist %Folder% @echo Syntax CleanUp Folder - %Folder% not found.&goto :EOF
6: CD /D %Folder%
7: Echo Processing Folder: %Folder%
8: setlocal
9: :: REMOVE EMPTY SUBFOLDERS
10: for /f "tokens=*" %%A in ('dir /ad /s /b *.* ^|Sort /Reverse') do (
11: RmDir "%%A" 2>NUL
12: )
13: CD ..
14: :: REMOVE FOLDER, IF EMPTY
15: RmDir %Folder% 2>NUL
16: endlocal
All scripts are freeware, but you are using them at your own risk. I take no responsibility for any damages or losses that result directly or indirectly from the use of those scripts.
Again, you can download the source code of all scripts in this post here: Roy-FileProcScripts.zip
Cheers!
Carsten aka Roy/SAC