问题
I want to create a folder in multiple folders available (by VBScript)
Example:
I have multiple folders: abc, xyz, ijk ... etc.
- I want to create a folder as "ABC" in all folders abc, xyz, tyu, ijk..etc
- Then move all files "jpg" in each folder abc, xyz, tyu, ijk .. into the folder "ABC" of each folder just created
- Check each folder and see if the folder "ABC" or not, empty or not
strFolder = "/" '<== This place how to automatically create a folder "ABC"
' in the directory available
SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")
'Move file jpg '<== I do not get it
IF objFSO.FolderExists(strFolder) = FALSE THEN
objFSO.CreateFolder strFolder
wscript.echo "Folder Created"
ELSE
wscript.echo "Folder already exists"
END IF
回答1:
To answer the question which was written as an answer...
Dim fso, shl, curdir, folder, file, newfoldername, newfolderpath
Set fso = CreateObject("Scripting.FileSystemObject")
Set shl = CreateObject("WScript.Shell")
curdir = shl.CurrentDirectory
newfoldername = "big"
For Each folder In fso.GetFolder(curdir).Subfolders
newfolderpath = fso.BuildPath(folder.Path, newfoldername)
If Not fso.FolderExists(newfolderpath) Then
fso.CreateFolder newfolderpath
WScript.Echo newfolderpath & " created"
Else
WScript.Echo newfolderpath & " already exists"
End If
For Each file In folder.Files
MoveFile file.Path, newfolderpath
Next
Next
Sub MoveFile(source, destination)
On Error Resume Next
fso.CopyFile source, destination & "\", True ' true = overwrite
If Err Then
WScript.Echo "Error copying " & source & " to " & destination & ": " & Err.Description
WScript.Quit
Else
fso.DeleteFile source, True
End If
On Error GoTo 0
End Sub
The MoveFile sub acts as a regular move, i.e. copies file and then deletes source if successful. Better than using the built-in fso.MoveFile function as that one doesn't handle overwriting existing files.
In summary... on each subfolder in the current directory, see if subfolder\big exists. If so then echo text, else create folder and echo text. Then for each file in that subfolder, move it to the subfolder\big folder, overwriting existing files, and delete the source file if the copy was successful. You could add stuff to check the extension before moving (to target only certain file types), or exit sub if the file already exists (to not overwrite existing files).
来源:https://stackoverflow.com/questions/26258551/i-want-to-create-a-folder-in-multiple-folders-available