Page 1 of 1

create a folder based on filename

Posted: 16 Jul 2019 09:01
by gussy81
hi,

i have a script i found that helps me move 10 or so pdf files and create folders based on the files names. so for example i have a 1999-notes.pdf file and when i run script the folder '1999-Notes' is created and the pdf file is placed inside it. but i was hoping for a solution where the folder is named after the first 4 characters of the filename - in this case 1999


this is the working script i have currently:

Code: Select all


@echo off
setlocal enabledelayedexpansion
for %%A in (*.pdf) 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!"
   ) else (
      echo Folder !folname! exists
   )
   echo Moving file %%A to folder !folname!
   move "%%A" "!folname!"
   )
echo Finished
pause


Re: create a folder based on filename

Posted: 16 Jul 2019 09:47
by aGerman
Just use !folname:~0,4!.

Steffen

Re: create a folder based on filename

Posted: 17 Jul 2019 01:19
by gussy81
super steffen thank you!!