I am very new to VB Script and I am in need of VB Script to move specific sub-folder based on folder name structure (i.e. pick-up of exact "abc" sub-folder from "test2" and need to move to "test1") and folder exact structure is as follows
E:\test1\42\5423284\4967957\html
E:\test1\43\5403284\4567051\html
E:\test1\48\7723589\9977457\html
E:\test1\47\5428284\2797932\html
E:\test1\42\5468284\1487651\html
In above, "html" folder does not contain any "abc" sub-folder
I want to move below "abc" sub-folder to above "html" folder
E:\test2\42\5423284\4967957\html\abc
E:\test2\43\5403284\4567051\html\abc
E:\test2\48\7723589\9977457\html\abc
E:\test2\47\5428284\2797932\html\abc
E:\test2\42\5468284\1487651\html\abc
Can any body help on this
Thanks in advance
Based on this answer to your "delete folder" question:
Just provide a method like:
Public Function processFolder(oFolder)
WScript.Echo "looking at", oFolder.Path
processFolder = True
If "abc" = oFolder.Name Then
WScript.Echo "will move", oFolder.Path
WScript.Echo " to", Replace(oFolder.Path, "\df\", "\mf\")
oFolder.Move Replace(oFolder.Path, "\df\", "\mf\")
processFolder = False
End If
End Function
Related
Im looking for some help with an addition to a larger apple script, ive seen lots of similar queries but none that quite fit the bill, so if anyone can help or direct me to an answer it would be a huge help,
I wanting to follow this general premise
`“Choose Name” default answer “”
set ChosenName to text returned of result
set ImagesFolder to (choose folder with prompt “Choose Images Folder:”)`
The bit im struggling with
if the ImagesFolder contains a folder named “Image Set 1” then
look through the folder “Images Set 1” and rename the contents using this logic
if file name conatins 0001_ rename file to ChosenName & “front”
if file name conatins 0002_ rename file to ChosenName & “Back”
if file name conatins 0003_ rename file to ChosenName & “Top”
if file name conatins 0004_ rename file to ChosenNamet & “Bottom”
else
if the ImagesFolder contains a folder named “Image Set 2” then
look through the folder images 2 and rename the content using this logic
if file name conatins 0001_ rename file to ChosenName & “F”
if file name conatins 0002_ rename file to ChosenName & “B”
if file name conatins 0003_ rename file to ChosenName & “T”
if file name conatins 0004_ rename file to ChosenNamet & “B”
(The unqiue characters im using to identify these files are always the last characters if this helps)
Thanks
P
This script does what you need. You need to extend it to also manage the "Image Set 2" folder and its extension name, but It will be quite easy to just duplicate what's inside the Tell "Finder" block.
Because you have multiple folder, I used a sub-routine to process your folder, each time calling new rule. For instance the 1st rule is to process "Image Set 1, search for 0001,0002,0003,0004 and replace each with Front,Back,Top, Bottom.
The rule 2 is to process "Image Set 2, search for 0001,0002,0003,0004 and replace each with F,B,T, B.
The first part build the rules. The script itself is reduced to a loop through each rule, calling the sub-routine "Process_SubFolder" with the 3 variables: sub folder name, current targets and new names.
(*
Define record Rule, made of 3 variables :
NFolderNFolder: the name of sub-folder
NSource : the list of part of file names to be processed
NDest : the list of new names. This list MUST count same number of items as NSource
All rules are added into ListRules
*)
global ChosenName, ImagesFolder -- mandatory to use in the sub-routine
set Rule to {NFolder:"Image Set 1", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"Front", "Back", "Top", "Bottom"}}
set ListRules to {Rule}
set Rule to {NFolder:"Image Set 2", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"F", "B", "T", "B"}}
set ListRules to ListRules & {Rule}
set R to display dialog "Enter a name" default answer ""
set ChosenName to text returned of R
if ChosenName is "" then return -- no name selected, end of script
set ImagesFolder to choose folder with prompt "Choose Images Folder:"
repeat with aRule in ListRules
Process_SubFolder(NFolder of aRule, NSource of aRule, NDest of aRule)
end repeat
-- end of main script
on Process_SubFolder(LFolder, LSource, LDest)
tell application "Finder"
set SubFolder to (ImagesFolder as string) & LFolder
if folder SubFolder exists then
set FileList to every file of folder SubFolder -- get all files of Images Set 1
repeat with aFile in FileList -- loop through each file
set FName to name of aFile
set NewName to ""
-- Manage extension of the file
if name extension of aFile is "" then
set NewExt to ""
else
set NewExt to "." & name extension of aFile
end if
repeat with I from 1 to count of LSource --loop trhough each source of the rule
if FName contains (item I of LSource) then set NewName to ChosenName & (item I of LDest) & NewExt
end repeat
if NewName is not "" then set name of aFile to NewName -- only if name must be changed !
end repeat -- loop through files of LFolder
end if -- folder exists
end tell
end Process_SubFolder
With this structure, you can add as many rules as you want !
Of course, I assume that you will never get twice same names in sub folder ! It is not the case in Image Set 2, where you will have 2 files with new name = ChosenNameB : it will create an error !!
My main goal is to check if a folder exists in a zip file.
For that I'm trying to go through the various files and folders. I use the following code for that:
strFile = "C:\Users\temp.zip"
Set objApp = CreateObject("Shell.Application")
Set objContents = objApp.NameSpace(strFile).Items()
For Each objItem in objContents
WScript.Echo objItem.Name
If objItem.IsFolder Then
GetSubFolders(objItem)
End If
Next
Sub GetSubFolders(objSubItem)
Set objFolder = objSubItem.GetFolder
For Each objItem2 in objFolder.Items()
WScript.Echo objItem2.Name
If objItem2.IsFolder Then
GetSubFolders(objItem2)
End If
Next
End Sub
The problem is I can't seem to figure out, how to check which level I am on.
I thought about this:
levelDepth = Len(strFile) - Len(Replace(strFile, "\", ""))
which tells me how "deep" the zip file is. If I could get the full path of objItem and objItem2, then I could use the same method. Subtract the two from each other and get how deep in the zip file the current directory or file is.
The Path property should give you the full path of an item.
I need to take a list of computers (IP or PC name) that are all on the same domain in CSV format. Scan each computer for a specific folder name. The folder will be arcXXXof. The x's are a hash and change for each PC. If the folder is found it needs to output the folder path to a CSV and append with each computer scanned. My programming is limited and I only really know Java. Since this will be run from a server it will need local administrative privileges to run on the local machines. My manager suggested I use VBS, but I have never written in that before.
My current snag is getting an error "expected then" Here's my loop.
Sub Recurse(strFolderPath)
Dim objFolder
Set objFolder = objFSO.GetFolder(strFolderPath) 'reads Folders pulled from recursion
Dim objSubFolder
dim folderStart 'grabs the first 2 characters of the file name. Should match 'of' if correct folder
Dim folderEnd 'grabs the last 6 (test) characters of the folder name, should match arc.txt if correct
Global checkEnd
set checkEnd = "arc" 'checks for "arc" at ending
Global checkStart
set checkStart = "of" 'used to check if folder name is correct path
For Each objSubFolder in objFolder 'for every Folder scanned
'Scans the name of the Folder, objSubFolder, for an ending of “arc", and beginning of “of” (testing)
set folderName = objSubFolder.name
Set folderEnd = right(folderName, 3)
set folderStart = left(folderName, 2)
dim folderName
if folderName = testFolderName
then WScript.Echo objSubFolder
'If folderEnd = checkEnd and
'If folderStart = checkStart
'Add Folder location to array, set array to next object
'Then fLocations(i) = object.GetAbsolutePathName(objSubFolder) and i = i+1
else
End If
Next
'recursive for searching new folder
For Each objSubFolder in objFolder.Subfolders
Call Recurse(objSubFolder.Path)
Next
OK, you could use a regex to match the name. Define it up front, in your global scope:
Dim re
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^arc\w{3}of$"
I'm using \w, which is equivalent to [a-zA-Z_0-9]. You can change this if you're expecting only digits (\d) or something else for these three chars.
Then, in your Recurse function, test the folder name against it:
For Each objSubFolder in objFolder.SubFolders
' See if this folder name matches our regex...
If re.Test(objSubFolder.Name) Then
' Match. Just display for now...
WScript.Echo objSubFolder.Path
End If
' Test its subfolders...
Recurse objSubFolder.Path
Next
Tip: Remove the On Error Resume Next from your code while you're developing or you might miss all kinds of bugs and cause all kinds of headaches.
First of all, please excuse my shortcomings in presenting my issue as I haven't got much knowledge in VBA. Your help would be kindly appreciated.
I am working on a project that would imply putting the content of three different Excel files from three different sub-folders into one Excel file, and then run some macros in order to process the data they contain. Since I've already set the processing macros, my issue relies in importing the content correctly.
The problem I'm facing is that I don't have the exact names of the files I would like to open, and that they would change each month. Therefore, I can't use the "WorkBooks.Open" command that requires a precise name. However, the files have predictable name formats. For instance, one of the sub-folders will be comprised of files named "XXX-jan2013.xls", another one "january2013-XXX" and the last one "XXX-01/2013".
My goal would be to input the month and year manually, for instance “01/2013”, and then open all the files containing "January”, “jan” or “01" in their names.
Here’s what I have so far, with comments:
Sub ChosenDate()
‘It aims at opening a box in which the desired month would be written manually
Dim InputDate As String
‘These are the indications the user will get
InputDate = InputBox(Prompt:="Please choose a month.", _
Title:="Date", Default:="MM/YYYY")
‘In case the person forgets to write what he’s asked to
If InputDate = "MM/YYYY" Or _
InputDate = vbNullString Then
Exit Sub
‘If he does it correctly, I call the second Sub
Else: Call FilesOpening
End If
End Sub
‘So far, everything works fine
Public Sub FilesOpening()
‘This one aims at opening the chosen files
Dim ThisFile As String
Dim Files As String
‘Defining the folder in which the file is, as it can change from a computer to another
ThisFile = ThisWorkbook.Path
‘Here’s where I start struggling and where the macro doesn’t work anymore
‘If I wanted to open all the files of the folder, I would just write that:
Files = Dir(ThisFile & "\*.xls")
‘You never know…
On Error Resume Next
‘Creating the Loop
Do While Files <> vbNullString
Files = Dir
Set wbBook = Workbooks.Open(ThisWorkbook.Path & "\" & Files)
Loop
End Sub
‘But it doesn’t look inside of sub-folders, neither does it consider the date
Sub DataProcess()
‘This one is fine, except I can’t find a way to name the files correctly. Here’s the beginning:
Windows("I don’t know the name.xls").Activate
Sheets("Rapport 1").Select
Cells.Select
Selection.Copy
Windows("The File I Want To Put Data In.xlsm").Activate
Sheets("Where I Want To Put It").Select
Range("A1").Select
ActiveSheet.Paste
Windows("I don’t know the name.xls").Close
‘How can I get the name?
I hope my statement is understandable.
Thank you very much in advance!
Have a nice day,
E.
You need to build a list of the paths and the expected file masks. You can then loop each matching file and do your stuff.
Sub foo()
Dim request As String: request = "01/2013"
'//make a date
Dim asDate As Date: asDate = "01/" & request
Dim dirs(2) As String, masks(2) As String
dirs(0) = "c:\xxx\dir1\"
masks(0) = "*" & Format$(asDate, "mmmmyyyy") & "*.xls"
dirs(1) = "c:\xxx\dir2\"
masks(1) = "*" & Format$(asDate, "mmmyyyy") & "*.xls"
dirs(2) = "c:\xxx\dir3\"
masks(2) = "*" & Format$(asDate, "mmyyyy") & "*.xls"
Dim i As Long
For i = 0 To UBound(dirs)
GetFiles dirs(i), masks(i)
Next
End Sub
Private Function GetFiles(path As String, mask As String)
Dim file As String
'//loop matching files
file = Dir$(path & mask)
Do Until Len(file) = 0
'//process match
process path & file
file = Dir$()
Loop
End Function
Sub process(filePath As String)
MsgBox "processing " & filePath
'workbook.open
End Sub
As "XXX-01/2013" is not a file name I assumed "XXX-012013".
If its another subdirectory just:
dirs(x) = "c:\xxx\dir3\" & Format$(asDate, "mm") & "\"
masks(x) = "*" & year(asDate) & "*.xls"
I want to create a dir (named with a varible Utilities._Name)located two levels from the exe file,
My exe file is in C:\SGEA\SGEA\bin
How can I do it so I get C:\SGEA\theNewDir without using full path, just relative paths?
If Not System.IO.Directory.Exists(Utilities._Name) Then
System.IO.Directory.CreateDirectory(Utilities._Name)
Else
MessageBox.Show("There is already a dir named: " & Utilities._Name, "SGEA", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
If you have the path to the exe file, you can use the System.IO.Path Class to navigate easily:
Dim folder = Path.GetDirectoryName(theExeFile)
Dim grandparent = Path.GetDirectoryName(Path.GetDirectoryName(folder)) ' Up two directories
Dim newFolder = Path.Combine(grandparent, "theNewDir") ' Use this to create the new folder name cleanly
Utilities._Name = newFolder