Rename and Move Folders Using VB - vbscript

I have the following script which works great if I need to rename files in a folder but now I want to move and rename folders from one mapped drive to another mapped drive. Can someone help me to modify the script to do so? I am failry new to VB so pardon if I can't firgure this out but it took me a little while to figure this one out and now I am not sure how to modify this script. Thank you in advance!
The folders by default are labeled A.1234, A.5678, and so on and will always have a different number assigned to them. I will keep the numbers in the label as they are PO numbers. So my end result desired is Ack~1234, Ack~5678, and so on.
Dim fso, f, f1, fc, s Set
fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move f1.ParentFolder & "\" & replace(f1.Name, "A.", "Ack~")
Again these folders exist on the root of a mapped drive and need to move to another mapped drive with the new names. If more info is needed please do not hesitate to ask.
UPDATE
I modified the script below to give an idea of what I'm looking to do.
Dim fso, objFol
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Z:\")
Set objFolders = objFol.Folders
For each folder in objFolders
fso.Movefolder folder, "Y:\" & Replace(fso.Name, "A.", "Ack~")
Next
This give me an error stating it does not support "Folder". There will be any number of folders in the Z drive and I need to move them all to the Y drive. Sorry if I didn't explain properly in the previous post.

The move command is taking a string, for which you're providing the parent folder of the file, so the file isn't moving. I think it'll be sufficient to provide a different folder location. For example:
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move "Z:\TargetFolder\" & replace(f1.Name, "A.", "Ack~")
Note: I've not tested this, but if there are problems then just ask
Additional Stuff after update:
The following code will allow you to move folders, and do the replacements you need to the folder name string.
Dim fso, objFol, objMoveFol, strPathBuild
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Y:\Test")
For Each objMoveFol In objFol.SubFolders
'Replace the root folder locations in the path
strPathBuild = Replace(objMoveFol, "Y:\Test", "Z:\TargetFolder\")
'Do the required other fiddle
strPathBuild = Replace(strPathBuild, "A.", "Ack~")
fso.Movefolder objMoveFol, strPathBuild
Next
Make sure you're really careful with this sort of thing, as getting is wrong can be fairly spectacular.

Related

Move files older than x minutes

I want the script to move all files (all file extensions *.*) older than 5 minutes from an INN folder to and ERROR folder. In my example C:\CopyFlow\Directory test\Inn\ to C:\CopyFlow\Directory test\Inn\Error
So I figured how to move files and how to find files older than x-time after looking it up. Howover, putting this together is the issue for me. Does anyone know how I can nail this?
This is what I got so far...
Dim age_threshold
age_threshold = 5
Dim folder_path
folder_path = WScript.Arguments(0)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.getFolder(folder_path)
Dim old_file_not_found
old_file_found = 0
For Each file in f.Files
Dim age
age = DateDiff("n", file.DateLastModified, Now)
If age > age_threshold Then
old_file_found = 1
.MoveFile "C:\CopyFlow\Directory test\Inn\*.*", "C:\CopyFlow\Directory test\Inn\Error"
Exit for
end if
Next
WScript.Quit
I'm used to batch, so this is a little bit greek to me (source http://www.evalesco.com/check-any-file-older-x-minutes-directory).
Now where do I set (dim?) my INN and ERROR folder in this script? And I'm pretty sure the if age followed by .movefile is wrong, so I probably need a little correction there.
Update Whats missing in the image is a backslash after error (\error\) in the move.file line.
You can't call methods without an object providing the method, so .MoveFile should be fso.MoveFile. However, in its current form the script would move all files from C:\CopyFlow\Directory test\Inn if any of the files in the folder passed as argument to the script is older than 5 minutes.
What you need to do is pass C:\test\inn as the argument to the script, and move only those files that actually are older:
If age > age_threshold Then
file.Move "C:\test\inn\error\"
End If

Using VBScript to examine properties of files within a zip file

I'm trying to use VBScript to examine the contents of several hundred .zip files. Essentially what I want to do is run through each .zip and find all of the files wihtin that zip file. For each one of these files within the zip, I want to record some information about it to an Oracle database. That information being: file name and file modified date.
So far, my solution has been extracting each zips folder structure to a temp folder then running through the temp folder with an fso object. However, this has been proven to be very slow.
Is there a way to accoplish this without unziping the zip files?
Ouch man. I have never heard of vbscript zip object. But it has been a long time since I have done vbscript. Is there anyway you can avoid it?
I did some googling for you. I did find this: http://www.example-code.com/vbscript/zip_List.asp Chilkat has done a lot of stuff I thought not possible. This gives me the impression - that what you are trying to do is not going to be painless.
If given the problem you have I would find a different solution than vbscript. But if you pull-it-off I would vote for you to be mayor of vb land
You can do it in place with Shell Objects. But it will be just as slow, maybe. If just name and date Explorer may get it direct from the zip directory (at the end of the file so the whole file still needs to be read).
This copies items in a folder to another folder. A zip file is a folder so it will copy in and copy out.
To Zip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Unzip (note SrcFolder and DestFolder are reversed)
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Create a blank zip. (I should have used an ADODB binary stream rather than an FSO text stream, but it shouldn't matter)
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 to 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip

File wont move to folder vbs

So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).

Move Folder into different Folder with VBS

I've been trying to make a little VBS that gets all Home Directories on a Server and moves them to a different place. Little Example
C:\homefolders\test_person
C:\homefolders\test_person\old_home
Here is what I got so far, but the moving part doesn't work...
Call ListFolderContents("C:\Windows\System32\Drivers")
Sub ListFolderContents(path)
set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(path)
Msgbox folder.path
For each item in folder.SubFolders
ListFolderContents(item.Path)
Next
set folder = Nothing
set fs = Nothing
End Sub
Assuming there's no problem with permissions:
set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(path)
folder.Move newPath
Cheers

How to search a file in the specified path?

I want to search a file from the path.
For Example,
Path = "C:\Newfolder\"
file name = *.txt, *.fin
I want to get all the *.txt, *.fin file from the new folder.
Use the Scripting.FileSystemObject.
Call it with GetFolder("C:\Newfolder"), then loop through the files in that folder with the
.Files property and filter them on extensions using the GetExtensionName method.
For example:
Dim fso as Object: Set fso = CreateObject("Scripting.FileSystemObject")
Dim f as Object
For Each f in fso.GetFolder("<folderpath>").Files
If fso.GetExtensionName(f.Path) = "txt" Then 'or maybe it's .txt, I'm not sure
' also test for 'fin'
'... do stuff
End If
Next f
Check out the Scripting.FileSystemObject.
In your project add a reference to the "Microsoft Scripting Runtime".
Then you can do something like this:
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim fld As Scripting.Folder
Set fld = fso.GetFolder("d:\temp\newfolder")
Dim i As Integer
Dim ext As String
Dim fl As Scripting.File
For Each fl In fld.files
'get extension
ext = Mid(fl.Name, Len(fl.Name) - 2)
If ext = "txt" Or ext = "fin" Then
'do something with the file
End If
Next fl
This is one of the areas that got so much better with .NET.
Just drop-in a CDirDrill class that does it all for you, in full native VB6. Another excellent solution from Karl Peterson :)
The FileSystemObject is not recommended for a couple of reasons: one, it adds a dependency, and two, it depends on scripting, which may be disabled per policy. I've had bugs because some customer has managed to muck up scrrun.dll on their PC. Eliminate dependencies unless they're really helping you a lot.
BTW this question is a duplicate of this

Resources