Please help with batch or VBS file code


  1. Posts : 4
    Windows 7 Ultimate 32bit
       #1

    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.

    John
      My Computer


  2. Posts : 710
    Win7 Pro x64
       #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.

    Edit:
    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 Computer


  3. Posts : 4
    Windows 7 Ultimate 32bit
    Thread Starter
       #3

    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 Thumbnails Attached Thumbnails 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 Computer


  4. Posts : 710
    Win7 Pro x64
       #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.

    Edit:
    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.

    Edit:
    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!

    Edit:
    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.
    Last edited by Trucidation; 28 May 2012 at 01:43.
      My Computer


  5. Posts : 640
    Windows 7 Ultimate x64
       #5

    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.
    Code:
    @echo off
    cd %~dp0
    
    for /f "tokens=*" %%i in ('dir /b /a:d /s') do (call :COPY_FILES "%%i")
    
    pause
    exit
    
    :COPY_FILES
    cd %1
    cd..
    
    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 Computer


  6. Posts : 10,796
    Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
       #6

    Is the name realy ___Random Junk folder? Or do you mean 0a1298fd or any other random name?
      My Computer


  7. Posts : 5,941
    Linux CENTOS 7 / various Windows OS'es and servers
       #7

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

    Copy, Move and Delete files and folders

    cheers
    jimbo
      My Computer


  8. Posts : 4
    Windows 7 Ultimate 32bit
    Thread Starter
       #8

    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 Computer


  9. Posts : 710
    Win7 Pro x64
       #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 Computer


  10. Posts : 640
    Windows 7 Ultimate x64
       #10

    Had a quick look at FileMonkey, looks intresting.

    @Trucidation
    No problem. I enjoyed the challange.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About 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 00:52.
Find Us