Jump to content

How to copy a single file to every empty folder in a directory tree?


Recommended Posts

I'm trying to figure out how to copy a single file to every empty folder in a large directory tree. I was thinking it might be possible with a batch file but I don't know what the syntax would be for such an operation. If anyone has any ideas I'd appreciate it. Thanks.

Edit: And feel free to move this to whatever forum it best belongs in. I wasn't sure where to post it.

Edited by E-66
Link to comment
Share on other sites


I've got ideas however they're wasted unless we're provided with more information. We cannot even make guesses since you've even created your Topic in an incorrect, (nondescript), Forum.

Due to the possible batch statement moving to 'Programming' Forum.

* You also need to define empty folder, many people incorrectly define it as one containing no files but if it were empty it would also contain no folders|directories

Link to comment
Share on other sites

You need to define empty folder
Sorry about that. I mean folders with no files in them. To rephrase the first sentence of my first post:

I'd like to copy a single file to all of the folders in a large directory tree that currently don't have any files in them. These file-free folders may or may not have other folders within them that also may or may not contain files.

Link to comment
Share on other sites

Try this VBS script it will produce a list of empty folders in a text file.

Just place this script in the parent folder or you can specify a path

Parent Folder Wildcard

ShowSubFolders Fso.GetFolder(".")

Direct Path

ShowSubFolders Fso.GetFolder("C:\SomeFolder")

Save as List_ZeroSizeFolders.vbs

Option Explicit 

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim SubFldr, Ts, Txt
Txt = Fso.GetFolder(".").Path & "\DirectoryList_" & Fso.GetFolder(".").Name & ".txt"
Set Ts = Fso.CreateTextFile(Txt)

Ts.WriteLine Space(1) & Now

ShowSubFolders Fso.GetFolder(".")

Ts.Close
CreateObject("Wscript.Shell").Run("notepad " & Chr(34) & Txt & Chr(34)),1,True

Function ShowSubFolders(Folder)
For Each SubFldr in Folder.SubFolders
'-> Checks To See If Folder Size = 0
If SubFldr.Size = 0 Then
'-> Code To Do The File Copy Here
Ts.WriteLine " Empty Folder " & SubFldr.Path
End If
ShowSubFolders SubFldr
Next
End Function

Link to comment
Share on other sites

That VBS script worked and listed all the folders with no files in them, but I already knew which folders those were. I want to copy the same file to all of those folders (hundreds). That's what I don't know how to go about doing.

Edited by E-66
Link to comment
Share on other sites

Since you have not given very much information this was the best I could do with so little info.
I'm sorry about that. When I first posted I thought I might be able to do this with a simple DOS batch file so I was just looking for the basic syntax and I would do the rest myself. Another mod moved my post to this Programming forum and I'm definitely out of my element here. DOS batch files are the extent of my 'programming' knowledge.

Let's say the path to my parent folder is C:\Home.

The file I want to copy to all the empty (file-free) folders will reside there, so let's say its path is C:\Home\Small.wav.

With that info, could you show me what the code is for the copy procedure? I can change the info to the actual paths myself. Thank you. :D

Link to comment
Share on other sites

Try this VBS script, you must have this path C:\Home\Small.wav or the script will fail.

Option Explicit 
'-> Objects For Script
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Varibles For Script
Dim FileCopy, FileName, SubFldr
'-> Path To File
FileCopy = "C:\Home\Small.wav"
'-> Split To Get Only EG FileName(2) = Small.wav
FileName = Split(FileCopy,"\")
'-> Parent Folder WildCard
ShowSubFolders Fso.GetFolder(".")
'-> Parent Folder Full Path
' ShowSubFolders Fso.GetFolder("E:\ThisFolder\TheNextFolder\ETC")

Function ShowSubFolders(Folder)
For Each SubFldr in Folder.SubFolders
'-> Checks To See If Folder Size = 0
If SubFldr.Size = 0 Then
'-> Code To Do The File Copy Here
Fso.CopyFile FileCopy, SubFldr.Path & "\" & FileName(2),True
End If
ShowSubFolders SubFldr
Next
End Function

Link to comment
Share on other sites

I tested it about a dozen times with different paths and minor variations and it worked perfectly, and for that I thank you very much, but I do have a few questions...

1. Is the '-> at the beginning of a line the VBS way of 'commenting out' a line in the same way you'd use REM at the beginning of a line in a DOS batch file? From looking at the entire script I can see how it might be, but I can also see how it might not be.

2. What is the significance of using E:\ as the path in the following line: ' ShowSubFolders Fso.GetFolder("E:\ThisFolder\TheNextFolder\ETC")

When I actually use the script I don't know what partition I'll be on, but I'll obviously edit it to reflect the correct path to the file I want to copy. If I happen to be on the E: partition will there be any issue with the line above?

Edited by E-66
Link to comment
Share on other sites

  1. No, the ' is the character which acts as REM, the two characters which follow it were simply to aid you in finding the commented lines.
  2. No significance at all, that path is for you to define and was given purley as an example.

Also here's a batch file which should, under normal circumstances, do what you asked.

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
IF [%1]==[] (SET _=%~DP0) ELSE (SET _=%~1)
FOR /D /R "%_%" %%# IN (*) DO CALL :GL_ %%#
GOTO :EOF
:GL_
SET "_=F"
FOR %%# IN ("%*\*") DO GOTO :EOF
COPY "X:\PathTo\FileToCopy.ext" "%*"

You will change "X:\PathTo\FileToCopy.ext" in the bottom line to the full name and path of the file you wish to copy. If FileToCopy.ext is located within the same drive as the batch file, you can replace X: with %~D0. If the file is in the same directory as the batch file it can simply state the filename with extension, e.g."FileToCopy.ext".

The batch file will use the directory in which it is located as the top level of the tree it is searching. If you are running the batch file from elsewhere then the intended top level directory is provided to the batch file as a parameter on the command line, e.g. "X:\PathTo\ExampleBatch.cmd" "C:\Users\SomeUser". The first part of the command line is the full location of your batch file and the second part, (parameter), is the top level of the tree you are wishing to traverse in search of file-less directories.

Link to comment
Share on other sites

Also, if you are using XP or higher, some folders may actually appear "empty" but also contain a hidden system file called desktop.ini.
My batch routine was designed to deliberately ignore these since their main purpose is for system use and generally of no consequence to the user.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...