Batch file to create folder and move file in the folder

create folders from file names batch
batch file to move files to another folder based on name
batch file to move folders from one folder to another
batch move file to subfolder
batch file to move files from one folder to another on network
script to move files from one folder to another
dos move directory and subdirectories
create folder using batch file

I am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.

The jpeg files per content is labled as followed:

6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120

The file name is different per content

a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.

They will all be extracted to a singe folder.

So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.

For example: Create folder named 6c82c0239f6eb839 and move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.

I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.

@echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
  set Name=%%a
  set Folder=!Name:~0,3!
  xcopy  /y "%%a" !Folder!\
)

Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?

Thank you

@echo off
setlocal

@rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (

    rem Get filename as token before the dash.
    for /f "delims=-" %%B in ("%%~A") do (

        rem Make dir if needed.
        if not exist "%%~B" md "%%~B"

        rem Check if isdir.
        2>nul pushd "%%~B" && popd

        if errorlevel 1 (
            >&2 echo Failed isdir "%%~B".
        ) else (

            rem Do the move operation.
            >nul move /y "%%~A" "%%~B"

            if errorlevel 1 (
                >&2 echo Failed move "%%~A" to "%%~B"
            )
        )
    )
)

exit /b %errorlevel%

The code is well remarked so if you want to understand the evaluated code by changing @echo off to @echo on. The use of %errorlevel% after the exit /b is not required though will let you know what the errorlevel is when @echo on is used.

The pushd tests for a directory (even if it is a symlink). errorlevel is checked to decide if to echo a error message or do the move.

As the for loop variables are used direct, use of enabledelayedexpansion is not needed.

Many commands support the argument of /? to get help about the command. i.e. move /?.

xcopy: move files instead of copy?, Based on your now completely changed question: If there was only one .png file in the working directory, then you could simply do it in one line  Ok, so I have a bunch of files in one folder than I need a batch file to create folders and move them to said folders. Example of files: something_Disk1.abc something_Disk2.abc something_Disk3.abc another_Disk1.abc another_Disk2.abc Most batch files I have found just create folders based off their entire name and throw them in there.

If you only try to copy the correct jpeg to the correct folder, you can do this:

@echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
  set Name=%%a
  REM I presume all the files have 16 characters before the dash
  set Folder=!Name:~0,16!
  IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy  /y "%%a" !Folder!\
)

I was not able to test.

Linux and Unix mv command tutorial with examples, Batch file to create a folder and move files to it. and on for around 3000 more. "new subfolder" will be the same folder name in all client folders. I would like to create a batch file that does the following (Place the batch file in the root directory of lets say 10 folders ) (Folder structure example : c:\Root\Folder1\ -- c:\Root\Folder10) Inside each folder is an undetermined amount of random files with file types such as .psd .png .obj . ma .mb User now executes the bat file

First of all, I would like to apologize for my manners regarding my initial post.

The answer by micheal_heath has resolved my issue.

Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.

Batch file to make folders with part of file name and then copy files

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

I just changed the the following line remove the hypen and numbers to create folders for the file name properly.

for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D

I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.

This is a Windows batch script that creates folders for each file in the current folder that the script is running. The folders are created with folder names that follow the filenames. After creating the folder, it will move the file into it's own folder. Batch Created Folder By Filename Part and Move. Batch create folders based on series of files and move to subdirectory. Batch script to create folders based on filenames. create folders based on a file name and move those files into that folder.

Batch Script - Moving Files - For moving files, Batch Script provides the MOVE above command will move the files new.txt and test.txt into the c:\example folder. file batch-file move. Share a link to this question. improve this question. edited Apr 26 '13 at 20:49. asked Apr 26 '13 at 20:03. 5 silver badges. 13 bronze badges. What's the current working directory? – Dan Pichelman Apr 26 '13 at 20:04. C:/test for this one. – user2077474 Apr 26 '13 at 20:07. the root directory. -- to different

copied successfully or not. If they did, delete the source. I can run this (move) as a separate batch before or after the "Prior to EFC" move. I will be doing a batch file on the "clients" folder to list a .txt of all QB files before i move and after I move because our employees need to go in and delete a ton of these QB files that aren't needed anymore.

How do I move a file from one directory to another in shell script? A move on the same drive should not require copying and deleting, but only changing the OS data showing the location of the file in the folder structure. So if all the moves are within the same drive, it's better to use the command move .

Comments
  • So why do you think the folder name is only three characters long?
  • Hi, Thanks for your response. Im guessing its the !Name:~0,3! area right? Sorry, im a total beginner and have no idea what im doing please bear with me.
  • Could someone change this I'm afraid that's not how this site works. We're not a custom programming service that writes code to meet your requirements for you. Make an effort to understand what the code is doing and try to change it yourself.
  • You're right, I will dig around on my end too
  • I absolutely do not get what you mean by the jpeg files per content is labled as followed and the file name is different per content; please explain. Also share your own efforts. Read the tour and learn How to Ask here; also consult this: minimal reproducible example
  • It's much easier to check the attribute of a meta variable if it contains the letter d for directory than using a subroutine echo %%~aB|findstr "d" >Nul 2>&1 && (>nul move /y "%%~A" "%%~B")
  • I am not understanding why you are doing a directory check. The question clearly states that the directory needs to be created.
  • @LotPings, easier is opinion. Running findstr within a loop is inefficient as it is an executable that may need to run with each loop cycle. I would rather use internal commands which have less overhead.
  • @Squashman, the directory check is done in case a filename of the same name exists to avoid overwrite by move later. Overwriting a file that is supposed to be a directory 120+ times, using move, is not a good experience.
  • @michael_heath, I follow your logic on that. I will offer an alternative option on the ISDIR. You could replace your CALL to your ISDIR function with dir /a-d "%%~B" >nul 2>&1. Then change the comparison to IF NOT ERRORLEVEL 1
  • Hello, thanks for your reply. CD <C:\Users\sfb11\OneDrive\Desktop\image> would this be the correct way to input the correct root path? I tried adding pause at the very end to see why its not doing anything, but I haven't been able to see why its not working.
  • @inazma That would be two commands: c: and on the next line CD \Users\sfb11\onedrive\desktop\image
  • Option tokens=1* is get 1st token and get remainder as 2nd token. You only need the 1st token before the dash. Use delims=- as tokens=1 is a default option and you do not need the remainder. View for /? for help.
  • When working with batch files I use ss64.com very often as reference document.