Jump to content

cmd or vbs script for moving all files from subdirs up one level


Recommended Posts

I have a cmd script who can move all files from subdirs of current dir into current dir. This script work only if subdirs don't have spaces in their names.

@echo off
for /f "tokens=5* skip=2" %%i in ('dir ^| findstr "DIR"
') do (
move %%i\*.* .
)

Can you help with a vbs script if cmd can't do this task ? I can do this easy with AutoIt recursively in subfolders. It's for someone who don't want suspicious exe files :D

Edited by radix
Link to comment
Share on other sites


It works. Thanks. I found a vbs script which can move all files (more than one level recursive) from a folder to another folder (can be the same folder too).

Dim objFSO, ofolder, objStream
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objNet = CreateObject("WScript.NetWork")
Set FSO = CreateObject("Scripting.FileSystemObject")
'=====Source folder=====
SPath = "C:\Source\"
ShowSubfolders FSO.GetFolder(spath)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
CheckFolder(subfolder)
ShowSubFolders Subfolder
Next
End Sub
Sub CheckFolder(objCurrentFolder)
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile
Const OverwriteExisting = TRUE
For Each objFile In objCurrentFolder.Files
FileName = objFile
'=====Destination folder=====
objFSO.MoveFile FileName, "C:\Destination\"
Next
End Sub

Edited by radix
Link to comment
Share on other sites

You did say you want to move all files up one level right?

Here is how I would do that:

@echo off
::Change the environment variable on the next line to the directory you want to move
Set "movePath=D:\Temp\one"
move "%movePath%" "%movePath%\..\.."

This will move the folder "D:\Temp\one" and all sub folders and files to "D:\".

So you will end up with "D:\one" and all sub directories.

Link to comment
Share on other sites

Scr1ptW1zard, my understanding was to move all files within the tree below the current directory up to the current directory, yours appears to move directories too.

FOR /R %%# IN (*.*) DO MOVE "%%#"

Link to comment
Share on other sites

Here I wrote this script up

1:\ Open Brows For Folder Dialog To Select Copy From Folder

2:\ Open Brows For Folder Dialog To Select Copy Too Folder

3:\ Moves The Files With A Move Dialog Graph

MoveAll.vbs


Const MyComp = &H11&
Dim Shl :Set Shl = CreateObject("Shell.Application")
Dim Fld
Dim B1, B2, Col, Des, Loc, Path, T1, T2, T3
'-> Text For Brows For Dialog
T1 = "User Select The Folder That You Want To Move The Files"
T2 = T1 & " From There Old Location"
T3 = T1 & " To There New Location"
'-> Get The Folder You Want To Move From
Location()
Function Location()
Brows(T2)
Loc = Path
B1 = True
Check(Loc)
B1 = False
Destination()
End Function
'-> Get The Folder You Want To Move To
Function Destination()
Brows(T3)
Des = Path
B2 = True
Check(Des)
B2 = False
ChkResults()
End Function
'-> Check To Make Cancel Or Root Drive Not Selected
Function Check(Txt)
If InStr(1,Txt,"cancel",1) Or InStr(1,Txt,"root",1)Then
If MsgBox( _
"User Error : You Have Either Presed Cancel" & vbCrLf & _
"The Move From Or Selected A Root Drive" & vbCrLf & vbCrLf & _
"Did You Want To Select Another Folder",4132,"Error No Location") = 6 Then
If B1 = True Then
B1 = False
Location()
End If
If B2 = True Then
B2 = False
Destination()
End If
Else
MsgBox "User Confirmed Cancel Or" & vbCrLf & _
"Root Drive Selected Error",4128,"Close Script"
WScript.Quit(0)
End If
End If
End Function
'-> Check To See If B1 And B2 = False
Function ChkResults()
If B1 = False And B2 = False Then
Const moveOrRenameTrue = 8, yesToAll = 16
Const displayProgress = 256, noConfirmDirCreate = 512
Dim ObF
Set ObF = Shl.NameSpace(Des)
'-> Where The Files And Folders Are Move Or Copy
ObF.MoveHere(Loc & "\*"), yesToAll
' ObF.CopyHere(Loc & "\*"),moveOrRenameTrue+yesToAll+displayProgress
Else
If MsgBox( _
"If You Are Seeing This Box Then" & vbCrLf & _
"I Really Have No Idea How This" & vbCrLf & _
"Has Happen. But Would You Like" & vbCrLf & _
"To Restart This Script?",4132,"Error Unknown") = 6 Then
Location()
Else
WScript.Quit(1)
End If
End If
End Function
'-> Brows For Folder Dialog To Get Loc And Des
Function Brows(P)
Set Fld = Shl.BrowseForFolder(0,P,16,MyComp)
If Fld Is Nothing Then
Path = "User Cancel Folder Selection"
Else
Set Col = Fld.Self
If Len(Col.Path) = 3 Then
Path = "Root Drive Can Not Be Selected " & Col.Path
Else
Path = Col.Path
End If
End If
End Function

Rename MoveAll.vbs.txt to MoveAll.vbs to make active

MoveAll.vbs.txt

Link to comment
Share on other sites

  • 3 months later...

Sorry to resurrect this thread but I had a question about the script listed above.

Dim objFSO, ofolder, objStream
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objNet = CreateObject("WScript.NetWork")
Set FSO = CreateObject("Scripting.FileSystemObject")
'=====Source folder=====
SPath = "C:\Source\"
ShowSubfolders FSO.GetFolder(spath)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
CheckFolder(subfolder)
ShowSubFolders Subfolder
Next
End Sub
Sub CheckFolder(objCurrentFolder)
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile
Const OverwriteExisting = TRUE
For Each objFile In objCurrentFolder.Files
FileName = objFile
'=====Destination folder=====
objFSO.MoveFile FileName, "C:\Destination\"
Next
End Sub

How can I adapt this to work for multiple folders; i.e. moving from e:\backup\serverA to e:\serverA, e:\backup\serverB to e:\serverB, etc. I tried to add multiple folders to the script by copying from the '==Source folder line down and then changing source/destination folders. It works in that it moves the files out of the subfolders, but it will dump them into the last folder specified in the script, for example, it will move the contents of e:\backup\serverA, e:\backup\serverB .... and put it in e:\serverZ. If serverY is the last server listed in the script, it will dump there. How do I get them to abide by the different destinations in the script?

Hope that makes sense.

Thanks!

Link to comment
Share on other sites

Here is your script updated with 3 Sources Folders Ans 3 Destination Folders

I do not know if this will work I only made a guess at this script.

Save As MultipleMove.vbs


Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Source Folders
Dim Obj, SPath, Var
'-> Source And Destination Array
SPath = Array( _
"e:\backup\serverA = e:\serverA", _
"e:\backup\serverB = e:\serverB", _
"e:\backup\serverC = e:\serverC")
'-> Loop Threw Source And Destination Array
For Each Obj In SPath
'-> Split The Array Item To Get Source And Destination
'-> EG "e:\backup\serverA = e:\serverA"
'-> Source = e:\backup\serverA = Var(0)
'-> Destination = e:\serverA = Var(1)
Var = Split(Obj," = ")
Sub ShowSubFolders(Var(0))
Next
'-> Recursive Threw All Sub Folders
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
call CheckFolder(subfolder,Var(1))
ShowSubFolders Subfolder
Next
End Sub
'-> Move Folders
Sub CheckFolder(objCurrentFolder, MoveHere)
Const OverwriteExisting = True
Dim objFile
For Each objFile In objCurrentFolder.Files
FileName = objFile
'-> Destination Folde
objFSO.MoveFile FileName, MoveHere
Next
End Sub

Rename MultipleMove.vbs.txt to MultipleMove.vbs to make active

Updated Fixed 3 typo Errors

MultipleMove.vbs.txt

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...