我想在多个可用文件夹中创建一个文件夹 [英] I want to create a folder in multiple folders available

查看:114
本文介绍了我想在多个可用文件夹中创建一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多个可用文件夹中创建一个文件夹(通过VBScript)

I want to create a folder in multiple folders available (by VBScript)

示例:

I拥有多个文件夹:abc,xyz,ijk ...等等。

I have multiple folders: abc, xyz, ijk ... etc.


  • 我想在所有文件夹abc中创建一个文件夹为 ABC ,xyz,tyu,ijk..etc

  • 然后将每个文件夹abc,xyz,tyu,ijk ..中的所有文件 jpg移动到刚创建的每个文件夹的 ABC文件夹中

  • 检查每个文件夹,查看文件夹是否为 ABC,是否为空

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


推荐答案

回答写为答案的问题...

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

MoveFile 子动作是常规动作,即复制文件和如果成功,则删除源。优于使用内置的fso.MoveFile函数,因为它不会覆盖现有文件。

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.

总而言之,在当前目录的每个子文件夹中,查看子文件夹\大是否存在。如果是这样,则回显文本,否则创建文件夹并回显文本。然后,对于该子文件夹中的每个文件,将其移动到子文件夹\big文件夹中,覆盖现有文件,如果复制成功,则删除源文件。您可以添加一些东西以在移动前检查扩展名(仅定位到某些文件类型),或者如果文件已经存在(不覆盖现有文件)则退出sub。

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).

这篇关于我想在多个可用文件夹中创建一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆