Hi everybody.
I need to move several files to a new folder with same prefix name (4 Initial Characters). For example:
INPUT
9087310.pdf
9087411.txt
9087512.dwg
9088613.pdf
9088714.txt
9088815.dwg
9089416.pdf
9089517.txt
9089618.dwg
OUTPUT
folder 9087 (9087310.pdf 9087411.txt 9087512.dwg)
folder 9088 (9088613.pdf 9088714.txt 9088815.dwg
folder 9089 (9089416.pdf 9089517.txt 9089618.dwg)
I want to do this with more than 200 files:
Thank you in advance.
Batch create folder and move files with same prefix
Moderator: DosItHelp
Re: Batch create folder and move files with same prefix
This might help you (untested - writing on my mobile phone):
penpen
Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
for %%a in (*.*) do (
set "file=%%~na"
setlocal enableDelayedExpansion
set "prefix=!file:~0,4!"
if not exist "!prefix!" md "!prefix!"
for %%b in ("!prefix!") do (
set "attributes=%%~ab"
if not "!attributes!" == "!attributes:d=!" (
echo(Error: Needed Directory name is blocked by a file.
) else (
move "!file!" "!prefix!"
)
)
endlocal
)
goto :eof
penpen