radix Posted April 8, 2011 Posted April 8, 2011 (edited) 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 offfor /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 Edited April 8, 2011 by radix
allen2 Posted April 8, 2011 Posted April 8, 2011 (edited) Something like this should work@echo offfor /f "delims=; usebackq" %%i in (`dir /b /ad %1`) do (move "%%i"\*.* .) Edited April 8, 2011 by allen2
radix Posted April 8, 2011 Author Posted April 8, 2011 (edited) 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, objStreamSet 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.SubFoldersCheckFolder(subfolder)ShowSubFolders SubfolderNextEnd SubSub CheckFolder(objCurrentFolder)Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFileConst OverwriteExisting = TRUEFor Each objFile In objCurrentFolder.FilesFileName = objFile'=====Destination folder=====objFSO.MoveFile FileName, "C:\Destination\"NextEnd Sub Edited April 8, 2011 by radix
Scr1ptW1zard Posted April 8, 2011 Posted April 8, 2011 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 moveSet "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.
Yzöwl Posted April 9, 2011 Posted April 9, 2011 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 "%%#"
gunsmokingman Posted April 9, 2011 Posted April 9, 2011 Here I wrote this script up1:\ Open Brows For Folder Dialog To Select Copy From Folder2:\ Open Brows For Folder Dialog To Select Copy Too Folder3:\ Moves The Files With A Move Dialog GraphMoveAll.vbsConst MyComp = &H11&Dim Shl :Set Shl = CreateObject("Shell.Application")Dim FldDim B1, B2, Col, Des, Loc, Path, T1, T2, T3'-> Text For Brows For DialogT1 = "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 FromLocation() 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 FunctionRename MoveAll.vbs.txt to MoveAll.vbs to make activeMoveAll.vbs.txt
malayvanti Posted August 4, 2011 Posted August 4, 2011 Sorry to resurrect this thread but I had a question about the script listed above.Dim objFSO, ofolder, objStreamSet 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.SubFoldersCheckFolder(subfolder)ShowSubFolders SubfolderNextEnd SubSub CheckFolder(objCurrentFolder)Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFileConst OverwriteExisting = TRUEFor Each objFile In objCurrentFolder.FilesFileName = objFile'=====Destination folder=====objFSO.MoveFile FileName, "C:\Destination\"NextEnd SubHow 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!
gunsmokingman Posted August 4, 2011 Posted August 4, 2011 Here is your script updated with 3 Sources Folders Ans 3 Destination FoldersI do not know if this will work I only made a guess at this script.Save As MultipleMove.vbsDim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> Source FoldersDim 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 SubRename MultipleMove.vbs.txt to MultipleMove.vbs to make activeUpdated Fixed 3 typo ErrorsMultipleMove.vbs.txt
malayvanti Posted August 5, 2011 Posted August 5, 2011 OK so I tried that script and got error 800A0401 - expected end of statement on Line 1 Char 9.Any ideas?
gunsmokingman Posted August 5, 2011 Posted August 5, 2011 Typo ErrorDim Fso Set :Set Fso = CreateObject("Scripting.FileSystemObject")Should BeDim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
malayvanti Posted August 5, 2011 Posted August 5, 2011 That resolved that error, now I get error 800A03EA, "syntax error" on line 17, char 4.Thank you very much for your help thus far.
gunsmokingman Posted August 5, 2011 Posted August 5, 2011 Typo Error 1Sub ShowSubFolders(Var(0))Change ToShowSubFolders Fso.GetFolder(Var(0))Typo Error 2[objFSO.MoveFile FileName, MoveHereChange ToFso.MoveFile FileName, MoveHere
malayvanti Posted August 5, 2011 Posted August 5, 2011 Almost there!!Error on line 33, char 6 - "File already exists".You have been awesome to troubleshoot this with me, thanks.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now