batch-filecmdbatch-rename

How do I create folder from file name and move files into folder and change foldername


I'm trying to create an automation in a batchfile to create folders from filenames and move the files with a different filename to this new created folders. I've got a bunch of Excel files and the first few tokens are the same in these files. So for the created folders i need to remove the first 12 tokens and the extensions. Only managed to adjust the filenames, but the folders need the same name without the extensions .xlsx.

I saw this code and tried to adjust it.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceDir=C:\Bureaublad\1"
set "DestDir=C:\Bureaublad\1"

for /F "eol=| delims=" %%A in ('dir /B /A-D-H "%SourceDir%\Kamekllofis*.*" 2^>nul') do (
    for /F "eol= tokens=1 delims=." %%B in ("%%~nA") do (
        md "%DestDir%\%%B" 2>nul
        set "FileName=%%A"
        call move /Y "%SourceDir%\%%A" "%DestDir%\%%B\%%FileName:~12%%"
    )
)

Tried this md "%DestDir%\%%B:~12%%" 2>nul

I just need the first characters from the filename removed and the extension .xlsx


Solution

  • You know how to create the correct file names. Why don't you use the same method to create the correct foldernames? By the way - you don't need the second for loop when you use modifiers (see for /?)

    ...
    for /F "eol=| delims=" %%A in ('dir /B /A-D-H "*.pdf" 2^>nul') do (
        set "FName=%%~nA"
        call md "%DestDir%\%%FName:~12%%" 2>nul
        call move /Y "%SourceDir%\%%A" "%DestDir%\%%FName:~12%%\%%FName:~12%%%%~xA"
    )