Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
Duplicate folder structure scripts, VBS or something
UserInterface
post Oct 30 2009, 10:23 AM
Post #1
Atomican
Charge




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..)
Go to the top of the page
 
+Quote Post
joeuno
post Oct 30 2009, 11:16 AM
Post #2
Quark
Master




'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?
Go to the top of the page
 
+Quote Post
edmund.tse
post Oct 30 2009, 11:24 AM
Post #3
Atomican
Charge




Let's give this a start:

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


Can someone fill in the rest?

This post has been edited by edmund.tse: Oct 30 2009, 11:25 AM


--------------------
ET
http://www.edmundtse.com/
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 30 2009, 11:50 AM
Post #4
Atomican
Charge




QUOTE (joeuno @ Oct 30 2009, 11:16 AM) *
'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..
Go to the top of the page
 
+Quote Post
pappes
post Oct 30 2009, 12:54 PM
Post #5
Atomican
Guru




>>'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


--------------------
My moma always said I was special
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 30 2009, 01:07 PM
Post #6
Atomican
Charge




QUOTE (pappes @ Oct 30 2009, 12:54 PM) *
>>'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
Go to the top of the page
 
+Quote Post
joeuno
post Oct 30 2009, 02:06 PM
Post #7
Quark
Master




Change to powershell or compile with csc.exe and you're cooking with gas.
CODE
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
CODE
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

This post has been edited by joeuno: Oct 30 2009, 02:22 PM
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 30 2009, 03:06 PM
Post #8
Atomican
Charge




QUOTE (joeuno @ Oct 30 2009, 02:06 PM) *
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
CODE
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.

This post has been edited by UserInterface: Oct 30 2009, 03:27 PM
Go to the top of the page
 
+Quote Post
edmund.tse
post Oct 30 2009, 03:41 PM
Post #9
Atomican
Charge




Alright, I've got it:

So first, we recreate the directory structure according to the current directory. Replace "C:\test" with your desired destination.
CODE
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.
CODE
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 :)

This post has been edited by edmund.tse: Oct 30 2009, 03:43 PM


--------------------
ET
http://www.edmundtse.com/
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 30 2009, 03:53 PM
Post #10
Atomican
Charge




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

This post has been edited by UserInterface: Oct 30 2009, 04:04 PM
Go to the top of the page
 
+Quote Post
edmund.tse
post Oct 30 2009, 03:54 PM
Post #11
Atomican
Charge




A more efficient alternative to step 1 is to use:
CODE
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.

This post has been edited by edmund.tse: Oct 30 2009, 04:29 PM


--------------------
ET
http://www.edmundtse.com/
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 30 2009, 04:05 PM
Post #12
Atomican
Charge




QUOTE (edmund.tse @ Oct 30 2009, 03:41 PM) *
Alright, I've got it:

So first, we recreate the directory structure according to the current directory. Replace "C:\test" with your desired destination.
CODE
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.
CODE
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?

Go to the top of the page
 
+Quote Post
edmund.tse
post Oct 30 2009, 04:09 PM
Post #13
Atomican
Charge




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.


--------------------
ET
http://www.edmundtse.com/
Go to the top of the page
 
+Quote Post
ozacube
post Oct 30 2009, 07:10 PM
Post #14
Atomican
Champion




http://ss64.com/nt/robocopy.html

using robocopy - it's a free MS program, you can download it if necessary.

something like ...
"robocopy /MIR /CREATE srcdir destdir"
Go to the top of the page
 
+Quote Post
edmund.tse
post Oct 30 2009, 08:47 PM
Post #15
Atomican
Charge




I think robocopy copies the file contents as well.


--------------------
ET
http://www.edmundtse.com/
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 30 2009, 09:02 PM
Post #16
Atomican
Charge




@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!
Go to the top of the page
 
+Quote Post
joeuno
post Oct 30 2009, 09:10 PM
Post #17
Quark
Master




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

This post has been edited by joeuno: Oct 30 2009, 09:10 PM
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 30 2009, 09:14 PM
Post #18
Atomican
Charge




QUOTE (edmund.tse @ Oct 30 2009, 05:09 PM) *
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.
Go to the top of the page
 
+Quote Post
UserInterface
post Oct 31 2009, 12:31 PM
Post #19
Atomican
Charge




QUOTE (joeuno @ Oct 30 2009, 10:10 PM) *
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
Go to the top of the page
 
+Quote Post
edmund.tse
post Oct 31 2009, 01:38 PM
Post #20
Atomican
Charge




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.
CODE
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.
CODE
for /f "delims=" %i in ('dir /a:-d /b /s') do @echo. &gt; "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.


--------------------
ET
http://www.edmundtse.com/
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 6th September 2010 - 12:33 PM