Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do that in vbscript??

Status
Not open for further replies.

cooline

MIS
Dec 15, 2011
1
SY
Hi
This is my first thread
I have small question i do not know alot about vbscript
so
How can by vbscript move all files from my directory to folder "first" except one file "index.txt"
and move all folders in the same dircetory to folder "first" except one folder "index"

i tried but thera are mistakes so can u help me please

strPath = objFSO.GetParentFolderName(wscript.scriptfullname)
arrExemptFiles = array("index.txt")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder(strDir)
for each objFile in objFolder.Files
if (isExempt(objFile.Name) = false) then
move objFile, "first"
end if
next


 
1. [red]strDir[/red] is never defined. I assume you me strPath?
2. What is isExempt(). I assume it is a function to see if it the filename exists in the array arrExemptFiles. [blue]Of course, if your goal is to move all files except "index.txt" then there is no need to an array/function. You just have to compare "index.txt" against the filename.[/blue] Same goes for the folders.

Code:
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder([red]strPath[/red])

'files
for each objFile in objFolder.Files
   if ([blue]objFile.Name != "index.txt"[/blue]) then
      objFSO.movefile objFile.Path, strPath & "\first"
   end if
next

'folders
for each objFolder in objFolder.SubFolders
   if ([blue]objFolder.Name != "index"[/blue]) then
      objFSO.movefolder objFolder.Path, strPath & "\first"
   end if
next

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi Try this Code !
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)
Title="Move Files and Folders under condition"
MsgBox strPath,64,Title
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder(strPath)

'files
for each objFile in objFolder.Files
   if (objFile.Name <> "index.txt" and objFile.Name <> wscript.scriptName) then
      MsgBox objFile.Name,64,Title
      objFSO.Movefile objFile.Path, strPath & "\first\"
   end if
next

'folders
for each objFolder in objFolder.SubFolders
   if (objFolder.Name <> "index" and objFolder.Name <> "first") then
     MsgBox objFolder.Name,64,Title
     objFSO.Movefolder objFolder.Path, strPath & "\first\"
  end if
next
MsgBox "Done ! All Files and Folders are moved to " &strPath & "\first\ except The File index.txt and The Folder index !",64,Title
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top