Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So far I was using cygwin for my well known Unix scribbles to achieve some quick results on file system and file content manipulations.

Since a while, I consider migrating to a pure Windows environment for such tasks. Currently, I'm struggling with the following Bash command line (is there a similar commandline to achieve the same)?

The following bash command line searches all XML files in the file tree from the current directory and creates a compressed TAR file (analogous to a ZIP file on Windows). The XML files are stored in the TAR file with their relative path and attributes. With this TAR, I could extract the stored XML files at some other location and, if I wished, with the original file and directory attributes:
find . -name '*.xml' | xargs tar -czvf data.tgz


Or the following bash command line bases on two environment variables $DEV and $BACKUP; they point to a development *source* directory and to a development *backup* directory respectively. The command line creates a mirror of the development *source* file tree at the *backup* location (replacing existing files and the files' and sub-directories' attributes are maintained):

(cd $DEV; tar -cf - . ) | (cd $BACKUP; tar -xf - )


Is there any equivalent Powerstell command line that achieves similar results? Replacement: tar by zip or similar. But it is important to have the same relative file sub-tree in the zip file as in the original file sub-tree.

Thanks in advance

Andi
Posted
Updated 18-Dec-12 22:43pm
v3
Comments
Andreas Gieriet 2-Jan-12 9:48am    
No answer since more than a year... :-(
Anyone knows of a better forum to ask such a question?

Thanks

Andi
Sergey Alexandrovich Kryukov 16-Dec-12 23:08pm    
Hi Andi,
I think there are people knowing bash and knowing PowerShell, but very few in the intersection of these sets. I, for example, avoid bash, even developing for Linux; last time I did massing Linux administration I switched to Python, as it looks way more logical to me. And I would probably help with PowerShell, but...

In view of the above explanation, I think your mistakes was: you should have explained what you want to achieve instead of giving bash sample. A person who would know how to do it in PowerShell might simply not able to understand you bash code...

—SA
Andreas Gieriet 19-Dec-12 4:51am    
Hello Sergey,
thanks for the hint to explain the purpose of the bash command lines in the question. I've updated the question accordingly.
I'm comming from a SunOS/Solaris/HP-UX/Ultrix/... Unix background (far back those days ;-) where simple archiving is daily business and easy to achieve by a single-liner.
I'm working on Windows platform now for a while, but I still wonder how "Windows" people perform such simple task "natively", i.e. by intrinsic OS means, without the need to download helpers and utilities?
Cheers
Andi

By the way, found that Microsoft official documentation on PowerShell is prohibitively bad, even though v.3.0 is finally made well usable; and Intellisense is quite good. The best book I saw so far this one: http://www.pavleck.net/powershell-cookbook/[^].

The problem with tar is: you would need some code which unpacks it. I, for example, use a plugin to TotalCommander. If you know, say, a stand-along command-line utility, consider the problem solved. If you have some .NET code, it can be easily loaded by PowerShell and used through Reflection. (I don't mean using cmdlets; of course, such code could also be re-worked into a cmdlet and registered in GAC to make it immediately accessible by PowerShell.)

Good luck,
—SA
 
Share this answer
 
Comments
Andreas Gieriet 19-Dec-12 4:57am    
Hello Sergey,
I'm not sticking on TAR. Any way to *archive* easily a file tree with OS means is fine. I mean *archive*, i.e. so that I could restore with the very same original attributes like group and user ownership, access rights, etc. It not even need to be Powershell - cmd.exe is fine too.
Cheers
Andi
PS: I understand that there are several 3rd party tools that overcome the lack of decent file management on Windows. Thanks for the references!
Sergey Alexandrovich Kryukov 19-Dec-12 11:26am    
[@SA! Look here]
Let me see again. I've marked this comment for myself, to save the URL later today and look again — too busy at the moment.
—SA
Andreas Gieriet 19-Dec-12 13:14pm    
Oops! Now both solutions are gone...! Why? I did not vote yet - but that was about to come...
In any case: thanks for coming back to my old question and giving feedback to it!
Much appreciated!
Thanks
Andi
Sergey Alexandrovich Kryukov 19-Dec-12 16:42pm    
Thank you for the note; must be a glitch. I restored it. You know, I working at PowerScript right not, came up with a wild project small: enable a real OOP scripting style based on the approach of metaclasses. If you don't have an opportunity to create classes (with native PowerScript, without C# or external add-on libraries, you can only use available classes, at least if you want it in a reasonably simple way),.. so, if you don't have them — model them.
—SA
Sergey Alexandrovich Kryukov 19-Dec-12 18:02pm    
All right — I'm done, please see the updates to my restored answer. I hope it will be quite enough to solve this and other problems.
—SA
By the way, found that Microsoft official documentation on PowerShell is prohibitively bad, even though v.3.0 is finally made well usable; and Intellisense is quite good. The best book I saw so far is this one: http://www.pavleck.net/powershell-cookbook/[^].

The problem with tar is: you would need some code which unpacks it. I, for example, use a plugin to TotalCommander. If you know, say, a stand-along command-line utility, consider the problem solved. If you have some .NET code, it can be easily loaded by PowerShell and used through Reflection. (I don't mean using cmdlets; of course, such code could also be re-worked into a cmdlet and registered in GAC to make it immediately accessible by PowerShell.)

[EDIT #1]

Solution restored. I'll try to provide some more help…

[EDIT #2]

Please see my last comments.

I suggest you use PowerScript, mostly because it is, well,.. powerful, and, most importantly, much, much easier to develop and debug, with advent of v.3.0, where the Power Shell ISE is million time better than previous, with really working Intellisense (!), in particular.

Then, I would suggest you use 7-zip, as the best compression package I know. Please see:
http://en.wikipedia.org/wiki/7-zip[^],
http://www.7-zip.org/[^].

It support a number of formats, and, importantly, is good enough to work in command line mode.

Now, how you use it via PowerScript? Through the cmdlet with expertly chosen options :-). Consider you have your executable (such as compression software executable module) and the list of arguments it requires in the variables $filePath and $arglist. Then, you script it this way:
$filePath = "some file name here, absolute or relative file name..."

$arglist = "some argument", "some other argument", "some more arguments"
# could be just one string with all arguments, to keep it simple

# now, start it:
Start-Process -NoNewWindow -wait -FilePath $filePath -ArgumentList $arglist


This approach may require some temporary storage for temporary files. First, you can use .NET API to find out the "Special Folder", the one associated with the current user and reserved for temporary storage, etc.

You can see what are the special folders:
[Enum]::GetValues([Environment+SpecialFolder])


In particular you can do:
$filename = [System.IO.Path]::GetTempFileName()

and use one in this way:
[Environment]::GetFolderPath("System")
# or
[Environment]::GetFolderPath("LocalApplicationData") # per user

and then, under this directory, there is a user's "temp" sub-directory.

Something like that. Not really difficult. Best thing, debugging is good enough.

I think these recommendation will be quite enough to solve the problem.

Good luck,
—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 19-Dec-12 18:02pm    
5'ed!
Sergey Alexandrovich Kryukov 19-Dec-12 18:04pm    
Thank you, Espen.
How could you be so fast? Please see my comments to the question — yet another topic for the article. I already have amazing results.
—SA
Espen Harlinn 19-Dec-12 18:25pm    
>>How could you be so fast?
Well, what's the three first letters of my name?
>> yet another topic for the article.
Looking forward to it :-D
Sergey Alexandrovich Kryukov 19-Dec-12 18:48pm    
"Esp" or "Har", or what? What would it mean, sorry?
—SA
Espen Harlinn 20-Dec-12 6:20am    
It was a joke: http://en.wikipedia.org/wiki/Extrasensory_perception
Appears it was lame ...
Apart from what Sergey mentions - you have a few other options:


All of them are free and allows you to continue using your bash scripts on Windows.

If you go with the first one, check out the SUA Community[^] - as they provide valuable tools and other information.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Andreas Gieriet 20-Dec-12 12:45pm    
Hello Espen,
Thanks for your suggestions and links to the respective Unix services on Windows.
I have plenty of experience on Cygwin, some of MinGW, and none on "Windows Services for Unix". Interesting link. Will investigate. My 5!
Cheers
Andi
Sergey Alexandrovich Kryukov 21-Dec-12 0:28am    
My 5 for useful references, but those things are not so natural on Windows. I really recommend to check out new PowerShell.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900