I want a batch to create folders automatically based on zip files.
The zip files have different names and I want to create folders automatically with the same name as those files and move them all there.
Example:
I have these files:
[Jungle] part 1.zip
[Jungle] part 2.zip
[Jungle] part 3.zip
[People] Of babies.zip
[People] Of people.zip
[Animals] From the sea.zip
[Animals] Furry.zip
All the files that contain "[Jungle]" create a folder called "Jungle" and they are moved there.
All files that contain "[People]" create a folder called "People" and are moved there.
Based on pattern of [folder] etc.zip
.
@echo off
setlocal
for %%A in ("[*]*.zip") do call :movefile "%%~A"
goto :eof
:movefile
setlocal
for /f "delims=]" %%A in ("%~1") do (set "folder=%%~A") & goto :next
:next
set "folder=%folder:~1%"
if not exist "%folder%" md "%folder%"
move "%~1" "%folder%"
goto :eof
The for
loop searches for files matching pattern of [*]*.zip
and calls the :movefile
label on each file found. The filename
is passed as an argument to the first argument of %1
.
%~1
strips outer quotes so it can be placed in new outer quotes.
The for
loop in the :movefile label delimits the "%~1"
string by the
character ]
. The variable folder
will be set the 1st token.
goto :next
ensures only one loop is done.
%folder:~1%
is the value of %folder%
without the 1st character of [
.
The folder is created if not exist and then the file is moved into the folder.