MSFN Forum: How to copy a single file to every empty folder in a directory tree? - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to copy a single file to every empty folder in a directory tree? Rate Topic: -----

#1 User is offline   E-66 

  • Member
  • PipPip
  • Group: Members
  • Posts: 263
  • Joined: 15-August 06
  • OS:XP Pro x86
  • Country: Country Flag

  Posted 19 November 2009 - 04:01 PM

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.

This post has been edited by E-66: 19 November 2009 - 04:03 PM



#2 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 19 November 2009 - 04:16 PM

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

#3 User is offline   E-66 

  • Member
  • PipPip
  • Group: Members
  • Posts: 263
  • Joined: 15-August 06
  • OS:XP Pro x86
  • Country: Country Flag

Posted 19 November 2009 - 04:40 PM

View PostYzöwl, on Nov 19 2009, 05:16 PM, said:

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.

#4 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 19 November 2009 - 05:20 PM

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

Quote

ShowSubFolders Fso.GetFolder(".")


Direct Path

Quote

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



Save as List_ZeroSizeFolders.vbs

Quote

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



#5 User is offline   E-66 

  • Member
  • PipPip
  • Group: Members
  • Posts: 263
  • Joined: 15-August 06
  • OS:XP Pro x86
  • Country: Country Flag

Posted 19 November 2009 - 07:06 PM

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.

This post has been edited by E-66: 19 November 2009 - 07:07 PM


#6 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 20 November 2009 - 10:34 AM

I guess you missed this area of the code, since there was no info I left it blank.

Quote

'-> Code To Do The File Copy Here
			Ts.WriteLine " Empty Folder " & SubFldr.Path


Since you have not given very much information this was the best I could do with so little info.

#7 User is offline   E-66 

  • Member
  • PipPip
  • Group: Members
  • Posts: 263
  • Joined: 15-August 06
  • OS:XP Pro x86
  • Country: Country Flag

Posted 20 November 2009 - 04:59 PM

View Postgunsmokingman, on Nov 20 2009, 11:34 AM, said:

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

#8 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 20 November 2009 - 05:40 PM

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

Quote

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



#9 User is offline   E-66 

  • Member
  • PipPip
  • Group: Members
  • Posts: 263
  • Joined: 15-August 06
  • OS:XP Pro x86
  • Country: Country Flag

Posted 20 November 2009 - 07:19 PM

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?

This post has been edited by E-66: 21 November 2009 - 06:12 AM


#10 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 22 November 2009 - 01:38 PM

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

#11 User is offline   Tripredacus 

  • K-Mart-ian Legend
  • Group: Super Moderator
  • Posts: 6,988
  • Joined: 28-April 06
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 24 November 2009 - 02:27 PM

Also, if you are using XP or higher, some folders may actually appear "empty" but also contain a hidden system file called desktop.ini.

#12 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 24 November 2009 - 03:22 PM

View PostTripredacus, on Nov 24 2009, 08:27 PM, said:

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.

#13 User is offline   E-66 

  • Member
  • PipPip
  • Group: Members
  • Posts: 263
  • Joined: 15-August 06
  • OS:XP Pro x86
  • Country: Country Flag

Posted 24 November 2009 - 04:53 PM

Thank you, Yzöwl, for your effort. The VB script version worked perfectly but I'll give yours a try as well.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy