Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Please help with batch or VBS file code

27 May 2012   #1

Windows 7 Ultimate 32bit
Please help with batch or VBS file code

Hello folks, I really need some help. I have been trying a number of ways but do not have enough experience or knowledge of the code for this. I am trying to create a batch file or VBS file that can change this:

C:\Downloads\Albums\Work Folder\Album - January\Random Junk Subfolder\Multiple .jpg images
C:\Downloads\Albums\Work Folder\Album - February\Random Junk Subfolder\Multiple .jpg images
C:\Downloads\Albums\Work Folder\Album - March\Random Junk Subfolder\Multiple .jpg images
C:\Downloads\Albums\Work Folder\Album - April\Random Junk Subfolder\Multiple .jpg images

To this:

C:\Downloads\Albums\Work Folder\Album - January\Multiple .jpg images
C:\Downloads\Albums\Work Folder\Album - February\Multiple .jpg images
C:\Downloads\Albums\Work Folder\Album - March\Multiple .jpg images
C:\Downloads\Albums\Work Folder\Album - April\Multiple .jpg images

Of course, I am working with WAY more than just 4 folders. I am looking to basically get rid of the Random Junk Subfolder and move the multiple jpegs up. Icing on the cake would be to include the deletion of the Random Junk Subfolder if possible.

I have, in the past, created some VERY BASIC .bat files, but I do not know enough knowledge of the code to perform this type of task. Thank you greatly in advance for any help you can provide.


My System SpecsSystem Spec
28 May 2012   #2


Are there many Random Junk Subfolders? You could simply go into each one, sort by file type, select the .jpgs then cut-paste them elsewhere.

My bad, you said "way more than 4". If they're all under the same folder though, you could use search (include subfolders in results) and do the same thing. Then it's basically one huge cut-paste.
My System SpecsSystem Spec
28 May 2012   #3

Windows 7 Ultimate 32bit

No, that wont work...the images need to be moved up one directory only, which is still not the same folder. What you are suggesting would put all the images in one giant folder. I still need them separate, just in the directory one level up (i.e., Album - January, Album - February, Album - March, etc.). Thank you ,though.

Here's a graphic. I'd like to remove the highlighted folders and move the jpgs up one level:

Attached Images
Please help with batch or VBS file code-file-tree-01.jpg Please help with batch or VBS file code-file-tree-02.jpg 
My System SpecsSystem Spec

28 May 2012   #4


Gotcha. Hmm, looks like a batch file might be able to do it, but I can't quite grasp how the folder parameters should look like. Seems to me you either need to supply all the random junk folder names, else you'll need to run the batch file for each random junk folder. Gonna think aloud for a sec, feel free to jump in.

move random_junk\*.jpg .
That does it for the stuff in one album. Problem is, this has to be run in each album folder, and you'll need the name of the random junk folder for that album too.

Okay wait, I know, a for loop should work and you can stick the folder names in a plaintext file for it to process. Uhh gimme a few minutes to let this boil.

Okay, you'll need two text files, one containing the names of the album folders (say, "album.txt"), the other one containing the names of the corresponding random junk folder (say, "junk.txt"). They have to match, i.e. line X in album.txt and line X in junk.txt should be the same album/junk folder pair.

A simple "dir /ad/b >album.txt" should do the job for the former, for the latter... well damn, didn't think this one through. Wait a sec. No, change that to "dir /ad/s/b >album.txt". You'll get all the album folders and their junk folders in album.txt, then you'll have to cut-paste out the junk folders into notepad and save that as junk.txt. Still thinking about the for loop.

Alright, the for loop is pretty simple, what we need to do is tell it "take the .jpgs from folder A and move them to folder B", and folders A and B are supplied by those 2 text files right? Here's the format:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]

And here's the specifics:
for /f "tokens=2,3" %%1 in (folder.txt junk.txt) do move .\%%2\%%3\*.jpg .\%%2\

Run that from the parent folder above all the album folders, assuming all your album folders are under one parent folder, and that each album folder contains one random junk folder. If an album folder contains several junk folders then you'll just have to repeat the album folder in album.txt to match how many junk folders it has in junk.txt

Eh, that's the theory behind this anyway. I have not tested this!

Oh yeah, cleanup.
for /f "tokens=2,3" %%1 in (folder.txt junk.txt) do rmdir .\%%2\%%3

rmdir as you know will delete the folders, assuming there's nothing else in there. It will fail (deliberately) if there's still some other stuff in there, dunno maybe if you forgot anything.
My System SpecsSystem Spec
28 May 2012   #5

Windows 7 Ultimate x64

Here you go I think this will work fine, just backup the entire Work Folder just in case. It may not be the most efficient way and could take a while depending on the number of folders and files but it works.

Copy the following into a batch file and place in the Work Folder and run it from there.
@echo off
cd %~dp0

for /f "tokens=*" %%i in ('dir /b /a:d /s') do (call :COPY_FILES "%%i")


cd %1

if NOT "%cd%\" == "%~dp0" xcopy /vq %1\"*.jpg" "%cd%\*.*" && rd /s /q %1
For anyone that wants to know how it works then I'll do my best to explain.

First it runs a DIR cmd for all subfolders in a FOR loop and passes each directory one by one to the COPY_FILES marker/code.

COPY_FILES starts by changing the cmd pointer/prompt to the folder passed by the FOR cmd and then changes up one level to the destination folder. This is to make the XCOPY cmd easier.

The IF statement is because the first folders to be passed will be Album - January, Album - February etc.. and when the CD.. cmd is ran it will move the cmd pointer/prompt into Work Folder. Without the IF statement we would copy any jpg files from Album - January, Album - February etc.. to the Work Folder.

The IF statement checks to see if the current directory is NOT equal to where the batch file is ran from (%~dp0) which needs to be the Work Folder before copying any files.

This is where it will be inefficient because it wil have to ignore a number of folders in the Work Folder before moving on to their subfolders.

Once it's into the subfolders the XCOPY cmd copys all the .jpg files in the directory passed by the FOR cmd (%1) to the current directory the cmd prompt is pointing to. This is why the COPY_FILES first changes the pointer to the destination folder.

If the XCOPY cmd is successful then the directory passed by the FOR cmd (%1) is removed silently. (&& rd /s /q %1)

As is the xcopy cmd only uses the verify and quiet switches but you can modify it for hidden files, read only, overwrite etc... if needed.
My System SpecsSystem Spec
28 May 2012   #6

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1

Is the name realy ___Random Junk folder? Or do you mean 0a1298fd or any other random name?
My System SpecsSystem Spec
28 May 2012   #7

Linux CENTOS 7 / various Windows OS'es and servers

Hi there
review this guys code -- might do the trick

Copy, Move and Delete files and folders

My System SpecsSystem Spec
28 May 2012   #8

Windows 7 Ultimate 32bit

Hey guys, just woke up and saw all these excellent responses. Thanks! While I was waiting for any response and before I went to bed last night, I actually ended up finding a program online called FileMonkey that can perform what I need, lol. I ended backing up my Work Folder and trying it out...worked like a charm. After having the files moved up one level, it left me with a ton of empty folders. I used a program called Remove Empty Directories to scan the Work Folder and it deleted all empty folders. The whole process went by fairly quick too.

Thank you guys a ton for the suggestions though.
My System SpecsSystem Spec
28 May 2012   #9


"FileMonkey", heh. Good to know.

Btw, thanks for the code Duzzy, had a head cold yesterday wasn't thinking straight and just banged out what came to mind lol.
My System SpecsSystem Spec
29 May 2012   #10

Windows 7 Ultimate x64

Had a quick look at FileMonkey, looks intresting.

No problem. I enjoyed the challange.
My System SpecsSystem Spec

 Please help with batch or VBS file code

Thread Tools

Similar help and support threads
Thread Forum
Batch File .bat to copy file from File Sharing & FTP Hosting
Hi all, Any idea how to write Batch File .bat to copy file from File Sharing & FTP Hosting to local directory. Current the File Sharing & FTP Hosting is SMARTFILE. I don't want to use WINSCP as the connector. Thanks
General Discussion
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
Batch file code has problems.
My batch file isnt working. In the middle of running, it skips a big part of code and goes to the last few lines. Help? echo Retrieving data... ping -n 2 -w 3000 > NUL echo -e "2B5sfyZC9a1hCxO" ping -n 2 -w 1000 > NUL echo -e "76BE8Td6rhoaSw2" ping -n 2 -w 1000 >...
General Discussion
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
How to create a batch file that will move files based on the file type
How to create a batch file that will move files based on the file type? My Requirement : I need a batch file to copy all .doc files in all my HardDisk to a removable disk say (Z: drive) . Can anyone make it possible using a batch file?Please...Its urgent...
General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 09:47.
Twitter Facebook