Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PowerShell
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 4-Oct-10 8:32am
Edited 18-Dec-12 22:43pm
v3
Comments
Andreas Gieriet at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Andreas Gieriet at 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 at 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 at 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 at 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 at 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
Andreas Gieriet at 20-Dec-12 13:14pm
   
Hello Sergey,
7z solves my problem - I was not aware (or forgot ;-)) that 7z has a command line mode. See my comment on solution 3.
Cheers
Andi
Sergey Alexandrovich Kryukov at 21-Dec-12 0:26am
   
All right. So, are you going to give PowerShell a try? So far, this is the best I ever saw in scripting, especially at v.3.0. Highly recommend. You can do about everything, even Windows UI programming...
—SA
Andreas Gieriet at 21-Dec-12 8:21am
   
Hello Sergey,
yes, I'll also investigate the improvements of PSv3.
On the other hand: as I'm working on different customer environments, I'm often bound to what is there, and can often not demand to install other MS/3rd party tools :-(
So, I have to be ready to stick on what Windows provides out-of-the-box...
Cheers
Andi
Sergey Alexandrovich Kryukov at 21-Dec-12 12:23pm
   
Well, this can be a problem. This way, using batch could be much better (I would not have much of a problem with batch for such simple tasks).
 
However, the version 3.0 I've recently installed was suggested by standard Windows Update. I may stabilize and get out-of-box status exactly as it happened with batch language. By the way, do you know that it was also greatly improved compared to what we had in MS-DOS. Yes, it does not make it a serious tool.
 
—SA
Andreas Gieriet at 23-Dec-12 16:20pm
   
I use batch and powershell so far for small and simple tasks, but not for "real" things as I was used to with sh/csh/ksh/bash etc.
E.g. one of my most used powershell single-liner so far is:
gci . "*.cs" -r | %{ select-string -path $_.fullname -patt 'class.*ABC' }
(which is the equivalent of the Unix single-liner find . -name '*.cs' -exec grep 'class.*ABC' '{}' \;).
Cheers
Andi
Sergey Alexandrovich Kryukov at 23-Dec-12 23:14pm
   
:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 Smile | :) . 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
  Permalink  
v3
Comments
Espen Harlinn at 19-Dec-12 18:02pm
   
5'ed!
Sergey Alexandrovich Kryukov at 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 at 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 at 19-Dec-12 18:48pm
   
"Esp" or "Har", or what? What would it mean, sorry?
—SA
Espen Harlinn at 20-Dec-12 6:20am
   
It was a joke: http://en.wikipedia.org/wiki/Extrasensory_perception
Appears it was lame ...
Sergey Alexandrovich Kryukov at 20-Dec-12 10:53am
   
It's just me: never seeing abbreviations, even worse hearing them.
—SA
Andreas Gieriet at 20-Dec-12 13:10pm
   
Hello Sergey,
yes, 7z seems to solve my immediate question.
Thanks for the hint!
My 5!
Andi
Sergey Alexandrovich Kryukov at 21-Dec-12 0:26am
   
You are very welcome.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Comments
Andreas Gieriet at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 499
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,755
1 Sergey Alexandrovich Kryukov 6,951
2 Maciej Los 3,994
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100