Create folder from first 8 char & move files based on same criteria
Posted: 05 Oct 2020 14:20
I am not an expert in any way but have looked at other posts and modified the following. Would love if someone could confirm. I have one directory with several thousand .jpg files. The first 8 characters of the file names are the shot date YYYYMMDD_image##.jpg - I need to create a directory for every date, and move all the files from that date into that directory, whether 1 file or a 1000 files. Thank you.
From this:
To this:
From this:
Code: Select all
D:\iphone pics dump\
20150401_imagefile001.jpg
20150401_imagefile002.jpg
20150501_imagefile001.jpg
20160115_imagefile099.jpg
(. . . X 26K)
Code: Select all
D:\20150401\
20150401_imagefile001.jpg
20150401_Imagefile002.jpg
D:\20150501\
20150501_imagefile001.jpg
D:\20160115\
20160115_imagefile099.jpg
Code: Select all
@echo off
setlocal enabledelayedexpansion
for %%A in (*.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname:~0,8!."
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause