create a folder based on filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gussy81
Posts: 3
Joined: 16 Jul 2019 08:56

create a folder based on filename

#1 Post by gussy81 » 16 Jul 2019 09:01

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


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: create a folder based on filename

#2 Post by aGerman » 16 Jul 2019 09:47

Just use !folname:~0,4!.

Steffen

gussy81
Posts: 3
Joined: 16 Jul 2019 08:56

Re: create a folder based on filename

#3 Post by gussy81 » 17 Jul 2019 01:19

super steffen thank you!!

Post Reply