Solved

move files to one level up

Posted on 2011-03-04
9
465 Views
Last Modified: 2012-06-21
Hi,

How can I use VBscript to achieve this?

root
  folder1
    subfolder1
       subfile1
       subfile2
    file1
    file2

  folder2
    file1
    file2


the output should be

root
  folder1
    subfile1
    subfile2
    file1
    file2

  folder2
    file1
    file2
0
Comment
Question by:mcrmg
  • 5
  • 4
9 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35039868
This could be done quite easy in a BAT script, would that be okay, or do you require VBS?

~bp
0
 

Author Comment

by:mcrmg
ID: 35039886
either or is fine, the only thing is that I dont know that much of dos, can not modify it later.  LOL
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 35039951
Here's what a basic BAT script would look like, with some comments.  Let me know how scary it looks :-).

I'll also work up a quick VBS version for you to compare to.

@echo off
REM Define base or root directory to work in
set BaseDir=c:\temp\base
REM Loop through all first level folders in base folder1
for /D %%A in ("%BaseDir%\*") do (
  REM Loop through each subfolder to the first level folders
  for /D %%B in ("%%A\*") do (
    REM Move all files from the second level to the first level
    move "%%B\*.*" "%%A"
    REM Try to remove this second level folder now
    rd "%%B"
  )
)

Open in new window

~bp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mcrmg
ID: 35039987
yes, it works very well............thank you very much..........
a quick question, which line is it to delete the subfolders?  thanks
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35040116
he RD command (remove directory) should delete the low level folders, as long as they are empty after moving the files out (meaning no lower subfolders beyond that).

~bp
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35040168
And for comparison, here's aVBS approach.

sBaseFolder = "c:\temp\base"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oBaseFolder = oFSO.GetFolder(sBaseFolder)
For Each oSubFolder In oBaseFolder.SubFolders
   For Each oSubSubFolder In oSubFolder.SubFolders
      oFSO.MoveFile oSubSubFolder & "\*.*", oSubFolder & "\"
      oFSO.DeleteFolder oSubSubFolder
   Next
Next

Open in new window

~bp
0
 

Author Comment

by:mcrmg
ID: 35040210
ok. thanks
0
 

Author Comment

by:mcrmg
ID: 35040232
yes, they are doing exactly same thing..thanks again
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35041226
Welcome, happy to help.

~bp
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question