1.

Solve : Batch File Needed to Move Folders?

Answer»

Hi guy's - I am in deperate need of a batch file that will do the following.

I have several image folders, approx 2000 of them. The file tree is structured like this -

Images/12345/JPEG

The 12345 name changes on each of the 2000 folders. What I need to do is move all the contents of the JPEG folder into the 12345 folder and then delete the JPEG directory. There are existing files in the 12345 directory that will be named the same as to what I am moving, I want to overwrite the existing files with the files that are being moved.

I WOULD appreciate any help you can give.
Thanks!

Kevin

Something like this may do what you are after:

NOTE: Run this against a "Test" folder, just in CASE.

Open a command prompt, change to the Images directory, then type the following and press enter:

for /f %a in ('dir /b /ad') do (move /y %~fa\jpeg\* %~fa\ && rd %~fa\jpeg)
Thanks, I will give it a shot and LET you knowThe batch file worked perfectly. I can't thank you enough. I moved 187,854 files in 4112 folders in about 5 minutes. You have saved me day's if not weeks of work.

One more favor to ask. For some reason or another I had some folders with empty Jpeg sub folders. When the batch file hit those, it obviously didn't do anything, because there were no files to move, so it left the jpeg subfolder in the parent directory. Can that batch file be modified so as to just remove that jpeg folder? I have isolated all of them to another directory. Thanks again for your help.

If you get a minute I always like to learn something from an experience like this. I'd like to have an explanation of the batch file and a comment of what each of the function codes are doing. That way maybe I can write some batch files of my own. Thanks!Typing "for /?" without the quotes, in a command window will display the syntax of the FOR command.
Similarly, "dir /?" and "move /?" will display the syntax of the respective command.

In simple terms, what the command does is this:
"for" loops through the result of the command in parentheses (...)
('dir /b /ad') lists all the subfolders below the current folder, and ASSIGNS each subfolder in turn to the %a variable.
"do (...)" performs the action(s) within the following parentheses (...)

Assuming a folder structure like:
C:\Images\12345\jpeg\file.jpg
C:\Images\12346\jpeg\file.jpg
C:\Images\12347\jpeg\file.jpg

When running the command from the "C:\Images" folder:
"for /f %a in ('dir /b /ad')"
%a will be assigned "12345" during the first loop, then "12346" during the second loop, and "12347" on the final loop.

"%~fa" resolves %a into a fully qualified path, as explained when typing "for /?"
Therefore, during the first loop, "%~fa" resolves to: "C:\Images\12345", and the command "move /y %~fa\jpeg\* %~fa\" resolves to:

MOVE /Y C:\Images\12345\jpeg\* C:\Images\12345\

During subsequent loops, "12345" will be replaced by the current value of "%a"

The rd %~fa\jpeg then removes the "jpeg" folder in each subfolder that was processed. ie

During the first loop, rd %~fa\jpeg resolves to:

RD C:\Images\12345\jpeg

etc.

I am not sure that I understand what you mean by: "I have isolated all of them to another directory."

If you run the same command within the "Images" folder as before, but change the "&&" to a single "&", the empty jpeg folders will be removed.

In other words, change:
for /f %a in ('dir /b /ad') do (move /y %~fa\jpeg\* %~fa\ && rd %~fa\jpeg)
To:
for /f %a in ('dir /b /ad') do (move /y %~fa\jpeg\* %~fa\ & rd %~fa\jpeg)

A double ampersand "&&" means, carry out the second command (in this case the RD - Remove Directory command) ONLY IF the result from the previous command was successful.
A single ampersand "&" means, carry out the first command, then the second command, regardless of the result from the first command.

NOTE: If you don't want to see a bunch of errors displayed, due to the fact that most of the "jpeg" folders were previously removed, use the following command:

for /f %a in ('dir /b /ad') do (move /y %~fa\jpeg\* %~fa\ & rd %~fa\jpeg) >nul 2&1

FYI. A windows batch file is a text file, usually with a .BAT or .CMD extension, and can contain one or more commands to be executed. What has been described above is a COMMAND. This command can be written in a text file with a .bat or .cmd extension, but in this case the "%" signs will need to be doubled-up.Thanks again! That is a great explaination. I really like learning about these things. Hopefully, one day, it might start to sink in a little better.

When I said that I had isolated the file to another directory, all I did was move them out of my "good" images folder. I am going to have to rerun my image formatting program on those files to get them into a jpeg format.

Thanks again you have been a terrific help.Hi,

I have a similar problem, but I can't SEEM to figure out how modify the code oldun for my purposes.

I have a 411 folders that all have two folders within them. Each one of these folders has a unique name. So the directory structure is something like:

bigfolder/1/folder1_1
bigfolder/1/folder2_1

bigfolder/2/folder1_2
bigfolder/2/folder2_2

bigfolder/3/folder1_3
bigfolder/3/folder2_3

Where the 1,2,3 folders are my 411 total folders, and the folder1_1, folder1_2, folder1_3, etc are the two folders within each of the 411 folders.

What I'm trying to do is remove each of these two folders and place them all within the "bigfolder"

Anyone know how to do this?

Many thanks!



Discussion

No Comment Found