Jump to content
UserInterface

Duplicate folder structure scripts

Recommended Posts

Would someone be able to write me a small script for testing purposes?

 

I would like a script that will copy all the files/folders in a directory and duplicate the files into another folder.

I am not wanting to copy the files so I will try to explain.

 

If i have a folder structure like this

c:\tvshows\show\Season 1\S01E01.avi

 

I would like to run the script against C:\tvshows and have it out put to some thing like C:\Test\TVshows\all folders and directories\S01E01.avi

Except I just want the S01E01.avi to be a dummy file not a copy of the original file the same as if I had right clicked and selected > New > Text Document and changed "New Text Document.txt" to "S01E01.avi"

Then I have a directory that looks the same but is only a few MB in size not GB.

 

hope you can help (and this makes sense..)

Share this post


Link to post
Share on other sites

'tis better to give the full problem rather than your perceived solution. Why do you want placeholder files in this other folder and why do you want such a small size? What will be the use of these files? What end goal are you trying to achieve?

Share this post


Link to post
Share on other sites

Let's give this a start:

 

for /f "usebackq delims==" %i in (`dir /a:d /b /s`) do ...

Can someone fill in the rest?

Edited by edmund.tse

Share this post


Link to post
Share on other sites

'tis better to give the full problem rather than your perceived solution. Why do you want placeholder files in this other folder and why do you want such a small size? What will be the use of these files? What end goal are you trying to achieve?

There is no problem, what I have asked for is what I want.

I wish to make a duplicate of the file names, as I am building Importers that import information based on file structure and file name.

 

Think programs like XBMC

 

That being said I am not going to run these on my main DB in-case something goes wrong, and I am getting sick of copying over hundreds of GB from my NAS just to trial my software.

Because the importer does not play the file, but just looks at path and name, I just want to create a dummy folder. This will save me time and space and will also take out the risk of losing all my current data.

 

I would like to do this via script as I would like to update my dummy folder once a week..

 

I thought this would be simple..

Create a text document, rename it to name of file and make it recurse.

If i am wrong and i am asking to much, then I will do it manually but I have 1000's of files on my NAS, so would really like some help..

Share this post


Link to post
Share on other sites

>>'tis better to give the full problem rather than your perceived solution.

>There is no problem, what I have asked for is what I want.

>[real problem]

 

OK now we know the real problem we can suggest a better solution.

Create another user account with read-only access to the media.

run the program that you are testing from the read-only user account

Share this post


Link to post
Share on other sites

>>'tis better to give the full problem rather than your perceived solution.

>There is no problem, what I have asked for is what I want.

>[real problem]

 

OK now we know the real problem we can suggest a better solution.

Create another user account with read-only access to the media.

run the program that you are testing from the read-only user account

 

Interesting idea, 1000points for thinking out side the box!

Unfortunately, I will be renaming the files once the data is collected to reflect true name should have said before but never expected an answer like that.

 

but it will allow me to start, while I await another solution

Share this post


Link to post
Share on other sites

Change to powershell or compile with csc.exe and you're cooking with gas.

using System;
using System.IO;

namespace MirrorFileNames
{
	class Program
	{
		static void Main(string[] args)
		{
			if (args.Length < 2)
				Console.WriteLine("Fail!");

			var scanRoot = args[0];
			var mirrorRoot = args[1];

			if (!Directory.Exists(scanRoot))
				Console.WriteLine("Ummmm... NO!");

			// Assumes write permissions at the location.
			if (!Directory.Exists(mirrorRoot))
				Directory.CreateDirectory(mirrorRoot);

			MirrorFileNames(scanRoot, scanRoot, mirrorRoot);
			Console.WriteLine("Done.");			
		}
		
		public static void MirrorFileNames(string path, string origPath, string destPath)
		{
			var dir = new DirectoryInfo(path);
			
			foreach (var file in dir.GetFiles())
			{
				var filename = file.FullName.Replace(origPath, destPath);
				var dirName = dir.FullName.Replace(origPath, destPath);
				if (!Directory.Exists(dirName))
					Directory.CreateDirectory(dirName);
				File.Create(filename);
				Console.WriteLine(filename);
			}

			foreach (var subDir in dir.GetDirectories())			
				MirrorFileNames(subDir.FullName, origPath, destPath);			
		}
	}
}
Why do something in script with 2 lines, when you can write a whole app :p

 

e: Call like

mirrorFileNames.exe c:\ d:\test
to have everything on c:\ mirror to d:\test.

eg. c:\tvshows\show\Season 1\S01E01.avi --> d:\output\tvshows\show\season 1\s01e01.avi

Edited by joeuno

Share this post


Link to post
Share on other sites

Change to powershell or compile with csc.exe and you're cooking with gas.

 

Why do something in script with 2 lines, when you can write a whole app :p

 

e: Call like

mirrorFileNames.exe c:\ d:\test
to have everything on c:\ mirror to d:\test.

eg. c:\tvshows\show\Season 1\S01E01.avi --> d:\output\tvshows\show\season 1\s01e01.avi

 

Um, I have never used powershell, I am still working out VBS.

Do I copy to notepad and save as script.ps1 I can try to compile but not really sure about that, I am just starting C# and I know how to do it in there would that work..

 

Sorry to be so useless after the work you just did..

 

I am looking on google now..

 

I am going to try this is c#, I will post back how I go.

Edited by UserInterface

Share this post


Link to post
Share on other sites

Alright, I've got it:

 

So first, we recreate the directory structure according to the current directory. Replace "C:\test" with your desired destination.

for /f "usebackq delims=" %i in (`dir /a:d /b /s`) do @mkdir "C:\test%~pnxi"

Next, we'll create empty files in each of those directory for every file you have.

for /f "usebackq delims=" %i in (`dir /a:-d /b /s`) do @echo. > "C:\test%~pnxi"

Note that this doesn't pick up hidden files.

 

I believe that's all you asked for, in just 2 lines :)

Edited by edmund.tse

Share this post


Link to post
Share on other sites

I tried running as a powershell script but get the following error..

 

File C:\mirrors.ps1 cannot be loaded because the execution of scripts is disabl

ed on this system. Please see "get-help about_signing" for more details.

At line:1 char:15

+ c:\mirrors.ps1 <<<< c:\test c:\output

+ CategoryInfo : NotSpecified: (:) [], PSSecurityException

+ FullyQualifiedErrorId : RuntimeException

 

 

I have right clicked the file to allow, but don't have that option..

 

Fixed that now get

 

The 'using' keyword is not supported in this version of the language.

At C:\mirrors.ps1:1 char:6

+ using <<<< System;

+ CategoryInfo : ParserError: (:) [], ParseException

+ FullyQualifiedErrorId : ReservedKeywordNotAllowed

Edited by UserInterface

Share this post


Link to post
Share on other sites

A more efficient alternative to step 1 is to use:

xcopy /teq . C:\test

Note this has slightly different behaviour than the one I gave earlier.

 

Edit: actually this difference in behaviour makes it not work with step 2.

Edited by edmund.tse

Share this post


Link to post
Share on other sites

Alright, I've got it:

 

So first, we recreate the directory structure according to the current directory. Replace "C:\test" with your desired destination.

for /f "usebackq delims=" %i in (`dir /a:d /b /s`) do @mkdir "C:\test%~pnxi"

Next, we'll create empty files in each of those directory for every file you have.

for /f "usebackq delims=" %i in (`dir /a:-d /b /s`) do @echo. > "C:\test%~pnxi"

Note that this doesn't pick up hidden files.

 

I believe that's all you asked for, in just 2 lines :)

 

Is this just a .bat or using xcopy or some such.

How do I tell it the source directory?

Share this post


Link to post
Share on other sites

With that command, it'll 'clone' everything from your present working directory.

 

You just copy and paste that into your command prompt and hit enter. Putting that 'for' command straight into a bat file doesn't work.

Share this post


Link to post
Share on other sites

@joeuno

I like your option but I can not seem to compile it or use it through powershell. Could you please upload a compiled version to mediafire or something?

 

@edmund.tse

H0LY CR@P! I have no idea what those commands are!

I have been a system admin for 3 years and never seen any of those commands!

 

I would love an explanation if you could!

I was sure I would have to do something like that in powershell/VBS/xcopy never thought I would be able to create dummy files in CMD prompt.

Worked, perfect and quick, just wished I could do it through a .bat file

 

Thanks to everyone for your help, it would have taken me weeks to work out a way to do this.

 

I really can not stress how much you guys helped!

Share this post


Link to post
Share on other sites

http://www.mediafire.com/?sharekey=85bc43d...04e75f6e8ebb871

 

Called mds.exe now. call by mds.exe <origPath> <destPath> e.g. to mirror all files in c: to a F:\output -> mds.exe c:\ f:\output

 

Be sure your user can has permissions to create files and folders or the app won't work. should create a bunch of 0 byte files.

 

e:oh yeah, needs .net 3.5 runtime

Edited by joeuno

Share this post


Link to post
Share on other sites

With that command, it'll 'clone' everything from your present working directory.

 

You just copy and paste that into your command prompt and hit enter. Putting that 'for' command straight into a bat file doesn't work.

 

If I run this against my movies folder and it says "The system cannot find the path specified."

 

Movies are all in one folder e.g. C:\movies\movie.file, C:\movies\movie2.file

I am not that interested in getting a copy of my movies folder, but might come in handy later if you can help.

Share this post


Link to post
Share on other sites

http://www.mediafire.com/?sharekey=85bc43d...04e75f6e8ebb871

 

Called mds.exe now. call by mds.exe <origPath> <destPath> e.g. to mirror all files in c: to a F:\output -> mds.exe c:\ f:\output

 

Be sure your user can has permissions to create files and folders or the app won't work. should create a bunch of 0 byte files.

 

e:oh yeah, needs .net 3.5 runtime

Thanks!

 

Will try it now

Share this post


Link to post
Share on other sites

Since you say you're a sys admin for 3 years, then you should be able to fix the bad path problem. Here's how it works.

 

I'm quoting here a shorter but equivalent version of the command.

 

Step 1.

for /f "delims=" %i in ('dir /a:d /b /s') do @mkdir "C:\test%~pnxi"
Let's start with the 'dir' part, that is used to give me a directory listing.

'/a:d' gives me only directories, and not files.

'/b' gives me the result in bare formatting, so only the path and the name of the directory.

's' includes all subdirectories.

 

So, having done this, I now have a list of all your subdirectories. Now for every item in this list, (the 'for' command), I will set the variable '%i' to that directory, and call the 'mkdir' command. The 'delims=' part makes sure the directory isn't split by spaces.

 

Finally, I 'mkdir' the new path, with is composed of C:\test at the beginning, and then append a special formatting of the directory path. %i would just give me the bare string, but I can extract the path 'p', the name 'n' and extension 'x' in that order, giving me %~pnxi. The reason for doing this is to remove the drive letter at the start so I can put it into the C:\test subdirectory.

 

The @ annotation before the command just suppresses printing of the command, so I don't see anything if everything goes well.

 

Step 2.

for /f "delims=" %i in ('dir /a:-d /b /s') do @echo. > "C:\test%~pnxi"
The for loop here is similar, but instead of looping over the directories, I'm not looping over non-directories. '/a:-d'.

 

The command here is 'echo.' which prints a newline character (I think), and sends that to a file formatted in a way similar to step 1.

Share this post


Link to post
Share on other sites

I think robocopy copies the file contents as well.

The maybe you should learn to read.

 

/CREATE : CREATE directory tree structure + zero-length files only.

Sorry to sound a little harsh, but I think that's pretty poor form considering that the '/CREATE' option was in the command I provided above.

Edited by ozacube

Share this post


Link to post
Share on other sites

Indeed you are correct. My apologies for neglecting the details.

 

How I wish more people realise that it is often more constructive and less offensive to launch attacks on ideas and comments rather than people themselves.

Share this post


Link to post
Share on other sites

Since you say you're a sys admin for 3 years, then you should be able to fix the bad path problem. Here's how it works.

 

I'm quoting here a shorter but equivalent version of the command.

 

Step 1.

for /f "delims=" %i in ('dir /a:d /b /s') do @mkdir "C:\test%~pnxi"
Let's start with the 'dir' part, that is used to give me a directory listing.

'/a:d' gives me only directories, and not files.

'/b' gives me the result in bare formatting, so only the path and the name of the directory.

's' includes all subdirectories.

 

So, having done this, I now have a list of all your subdirectories. Now for every item in this list, (the 'for' command), I will set the variable '%i' to that directory, and call the 'mkdir' command. The 'delims=' part makes sure the directory isn't split by spaces.

 

Finally, I 'mkdir' the new path, with is composed of C:\test at the beginning, and then append a special formatting of the directory path. %i would just give me the bare string, but I can extract the path 'p', the name 'n' and extension 'x' in that order, giving me %~pnxi. The reason for doing this is to remove the drive letter at the start so I can put it into the C:\test subdirectory.

 

The @ annotation before the command just suppresses printing of the command, so I don't see anything if everything goes well.

 

Step 2.

for /f "delims=" %i in ('dir /a:-d /b /s') do @echo. > "C:\test%~pnxi"
The for loop here is similar, but instead of looping over the directories, I'm not looping over non-directories. '/a:-d'.

 

The command here is 'echo.' which prints a newline character (I think), and sends that to a file formatted in a way similar to step 1.

 

Thanks heaps for explaining that, I have pulled bits out and printed them off microsoft site, and will read more in depth over weekend, I really like this way and I have used it for other things already!

 

 

@joeuno

Thanks for compiling that I only started C# 3 weeks ago, I have not got very far into it at all. This file is awesome, I now have it automatically delete my DB from today, then this runs over night to update me with a new DB.

 

Thanks for everyone input, I learned a few things and have a few more to work out :p

Share this post


Link to post
Share on other sites

HI 

@UserInterface thank you for your post - this helps me tremendously.  I have students that need data folders on two different servers. I want to sync the folders, but not the contents. So with your script, I can check the folders on the source server and add any that are missing.

I also need to sync the other way and delete those that have been deleted from the source.

 

Do you have a solution for deleting folders as well - so if the student leaves, the folder on the source server will automatically be deleted. I want to create a script that checks the source server and then deletes the missing folders from the destination server as well.

 

Thanks in advance

Share this post


Link to post
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

×