Batch file to put files in folder (only subfolders)

Kevin Huffman

I have a batch file that currently puts every file in a folder with the name of the file. However it only works for that folder I am in. I would like to have one batch file at the top of my directory that will only execute files 2 subfolders down. Example of directory:

/Parent folder **This is where I want my batch file**
  /Subfolder 1
    /Subfolder 1A/file.jpg **Level I want batch file to execute**
    /Subfolder 1B/file.mp4 **Level I want batch file to execute**
    /Subfolder 1C/file.mp3 **Level I want batch file to execute**
  /Subfolder 2
    /Subfolder 2A/file.jpg **Level I want batch file to execute**
    /Subfolder 2B/file.mp4 **Level I want batch file to execute**
    /Subfolder 2C/file.mp3 **Level I want batch file to execute**

This is my current batch file:

@echo off
for %%a in (*.*) do (
    md "%%~na" 2>nul
    move "%%a" "%%~na"
)
dbenham

Use a FOR loop with the /D option to iterate folders, repeating for as many levels as necessary. Use PUSHD at each step so that you don't have to keep track of the full path.

@echo off
for /d %%a in (*) do (
  pushd "%%a"
  for /d %%b in (*) do (
    pushd "%%b"
    for %%c in (*) do (
      md "%%~nc" 2>nul
      move "%%c" "%%~nc"
    )
    popd
  )
  popd
)

You can use %~dp0 to guarantee you start at the parent folder of the batch script:

@echo off
for /d %%a in ("%~dp0*") do (
  pushd "%%a"
  for /d %%b in (*) do (
    pushd "%%b"
    for %%c in (*) do (
      md "%%~nc" 2>nul
      move "%%c" "%%~nc"
    )
    popd
  )
  popd
)

The code becomes tedious as you add more levels. You can use recursion to write more generic code that can easily be changed to work at any subfolder depth. Simply change the depth value as needed. Note how you can re-use letters within nested FOR loops.

@echo off
setlocal disableDelayedExpansion
set depth=2

:iterateFolders
setlocal
set /a depth-=1
for /d %%F in (*) do (
  pushd "%%F"
  if %depth% gtr 0 (
    call :iterateFolders
  ) else for %%F in (*) do (
    md "%%~nF" 2>nul
    move "%%F" "%%~nF"
  )
  popd
)
exit /b

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Batch - Counting lines in files in current folder and subfolders

From Dev

Delete old files in a folder excluding subfolders with a batch

From Dev

Windows Batch to Overwrite Existing files in folder and subfolders

From Dev

Recursively rename files in subfolders Windows batch file

From Dev

Windows batch file: converting all files in subfolders

From Dev

Copy files specified files with subfolders to another folder[BATCH]

From Dev

Move multiple files from subfolders to one folder using CMD or Batch

From Dev

Windows batch file to move files to subfolders based on file name

From Dev

How to make nutch crawl files and subfolders - it only crawls the index of the folder

From Dev

Batch file to move all subfolder files up then delete empty subfolders?

From Dev

How to clear but not delete all files in all subfolders with exception of the batch file?

From Dev

Batch file to find .ts files in subfolders, and then move them to a directory?

From Dev

How to access Folder/Subfolders/Files using nested loops for file conversions?

From Dev

Batch File how to run all files in folder

From Dev

batch files folder and text file creation

From Dev

Batch file to delete files from wildcard folder

From Dev

Batch File to rename multiple files and move to folder

From Dev

Batch Files: List file names and folder names

From Dev

Batch file to rename files within a folder

From Dev

Batch File: Copying files to an unknown folder name

From Dev

Batch script to rename files in subfolders

From Dev

Git ignore subfolders but not the files in a folder

From Dev

Move Multiple Files from Subfolders to Single Folder using Windows batch script

From Dev

Batch file: Copy all files and folder except one folder

From Dev

count folders and subfolders with batch file

From Dev

Windows Batch file to sort subfolders

From Dev

run batch file in all subfolders

From Dev

How to copy specific subfolders if existing to new folders in a backup folder using a batch file?

From Java

Batch File; List files in directory, only filenames?

Related Related

  1. 1

    Batch - Counting lines in files in current folder and subfolders

  2. 2

    Delete old files in a folder excluding subfolders with a batch

  3. 3

    Windows Batch to Overwrite Existing files in folder and subfolders

  4. 4

    Recursively rename files in subfolders Windows batch file

  5. 5

    Windows batch file: converting all files in subfolders

  6. 6

    Copy files specified files with subfolders to another folder[BATCH]

  7. 7

    Move multiple files from subfolders to one folder using CMD or Batch

  8. 8

    Windows batch file to move files to subfolders based on file name

  9. 9

    How to make nutch crawl files and subfolders - it only crawls the index of the folder

  10. 10

    Batch file to move all subfolder files up then delete empty subfolders?

  11. 11

    How to clear but not delete all files in all subfolders with exception of the batch file?

  12. 12

    Batch file to find .ts files in subfolders, and then move them to a directory?

  13. 13

    How to access Folder/Subfolders/Files using nested loops for file conversions?

  14. 14

    Batch File how to run all files in folder

  15. 15

    batch files folder and text file creation

  16. 16

    Batch file to delete files from wildcard folder

  17. 17

    Batch File to rename multiple files and move to folder

  18. 18

    Batch Files: List file names and folder names

  19. 19

    Batch file to rename files within a folder

  20. 20

    Batch File: Copying files to an unknown folder name

  21. 21

    Batch script to rename files in subfolders

  22. 22

    Git ignore subfolders but not the files in a folder

  23. 23

    Move Multiple Files from Subfolders to Single Folder using Windows batch script

  24. 24

    Batch file: Copy all files and folder except one folder

  25. 25

    count folders and subfolders with batch file

  26. 26

    Windows Batch file to sort subfolders

  27. 27

    run batch file in all subfolders

  28. 28

    How to copy specific subfolders if existing to new folders in a backup folder using a batch file?

  29. 29

    Batch File; List files in directory, only filenames?

HotTag

Archive