Create a folder based on part of the name of the file and move the related files in it

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Diegao
Posts: 1
Joined: 05 Sep 2017 06:24

Create a folder based on part of the name of the file and move the related files in it

#1 Post by Diegao » 05 Sep 2017 06:31

Hi,

I've found this script but is not doing what i want.

So far this script is running only in the folder where it is. I want to point the folder that I want to process...don't know if it's clear?

Code: Select all

@echo off
SUBST C:\

set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
   set "filename=%%a"
   setlocal EnableDelayedExpansion
   for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
      set "basename=!filename!"
      md "!basename!"
   )
   move "!filename!.%%b" "!basename!"
   for /F "delims=" %%c in ("!basename!") do (
      endlocal
      set "basename=%%c
   )
)


Thank you
Last edited by aGerman on 05 Sep 2017 09:38, edited 1 time in total.
Reason: added code tags

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Create a folder based on part of the name of the file and move the related files in it

#2 Post by Squashman » 05 Sep 2017 12:17

Maybe you want to use one of these two commands.
CD
PUSHD

Post Reply