Get-ChildItem path without specifying the full path - powershell

So i am trying to get-childItem of a specific path.
my command look like this
(Get-ChildItem -Path fes\.policy -Recurse -Directory -Exclude ".*")
and the fullpath is something like this:
path 'C:\Users\(name)\(folder)\(folder)\(folder)\(folder)\governance\fes\.policy
and it works fine when I stand in the \governance\ folder and run the command. But when i am some where else it just add the parameter \fes.policy and says that the path not exsist
Get-ChildItem: Cannot find path C:\Users\(name)\(folder)\(folder)\(folder)\(folder)\governance\src\Tests\fes\.policy' because it does not exist.
which makes sense, but it just dont know how I get it to go back in directories and then put the \fes.policy on the path.
The start of the path is not always going to be the same, so I cant just put the whole path in.

You need to provide either the Fully-qualified path (the path starting from the drive root starting with C:) or the relative path (the path from the current working directory). From what it sounds like, you are trying to use a relative path, which is useful when the folder structure is only partially guaranteed (e.g. FolderA in the example below will always exist, but it's placement under C: may differ from system to system).
.. is a reserved directory "name" meaning the parent folder, similar to how . is used to represent the current folder. You will need to provide the relative path to the file using .. if you are in a different branch of the directory structure than where fes/.policy is located.
Since I don't know your full directory structure and only what you've provided let's take the following example:
Current directory: C:\Users\username\FolderA\FolderB\FolderC
Target file: C:\Users\username\FolderA\governance\src\Tests\fes\.policy
From FolderC, you should be able to locate the file with Get-ChildItem using the following path:
Note: If any of the path has spaces you will want to wrap them in single or double quotes (the latter if you want to expand variables within the path string), or else escape the spaces with `, though quoting is preferred.
Get-ChildItem ../../governance/src/Tests/fes/.policy
Since .. represents the parent folder of the current directory, ../../ represents two parents up, and would put you in FolderA. From there you know governance/ exists and you can craft the rest of the path as you need.

Related

Copying source files and retaining folder structures including parent folders (but not files)

Say I have the following source and target.
source: c:\temp\folder1\folder2
target: e:\backup
I want to have an output as shown below. As you can see, it should only copy the files and its subdirectories from the above source path but it should also copy/create the parent folders (no files) leading to the source path. How can I accomplish this with Powershell or robocopy? If Powershell doesnt handle long path names then I prefer using Robocopy.
source:
c:\temp
\folder1
\file1.txt
\folder2
\file2.txt
\folder3
\file3.txt
output:
e:\backup
\temp
\folder1
\folder2
\file2.txt
\folder3
\file3.txt
If I have understood you right you want to create any missing folders in the output on the fly. In powershell this would be s.t. like this:
Copy-Item c:\temp e:\backup\temp\ -recurse

.gitignore all except one specific file (via a full/fuller file path)

The Goal:
How to use .gitignore to exclude all folders & files except PowerShell $Profile?
The answer should help expand the current exception file list to more files in other subfolders. If possible, reduce wildcards to minimum, and use as specific as possible (full/fuller file path). Why? For instance, Book1.xlsx may exist in multiple subfolders but I want to be able to choose only the specific desired subfolders.
Thanks in advance!
Current status:
On Windows 10 (not Linux Distros):
git init initiated on top level directory C:\. [Please don't suggest to start from other subfolders. Just remain with C:\, as I will include more files to the exception list]
C:\.gitignore containing the below:
# Ignore All
/*
# Exception List [eg. PowerShell's $Profile (please use FULL/FULLER FILE PATH, if possible)]
!.gitignore
!Microsoft.PowerShell_profile.ps1
!Users/John/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
!Users\John\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
!C:/Users/John/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
!C:\Users\John\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
With the above codes, git status successfully returned only .gitignore as 1 untracked file. Microsoft.PowerShell_profile.ps1 remained missing from untrack listing.
I've tried alternative ways (wildcard, partial subfolder name with pattern, etc) but all failed to return the fuller file path PowerShell $Profile.

Look for PS Modules path and make a folder in that path

I'm creating a powershell script that will create a folder for a module in the PS modules folder, then download the module from github. I'm avoiding using the System32 folder, so generally I'd use the ProgramFiles folder, but not everyone has access to that, so the next one would be in the Documents folder for the user running the script. The problem is if that user changed their documents folder from the C drive to another drive, the script will fail, so I'd like to get a command that will take the first path as a string, before the semi colon, when executing
$env:psmodulepath
and use that string to further make module folders.
tl;dr: How do I capture a string starting from 0 up until a semi colon.
Thanks
You can use the -split operator and then take the first element of the array off of the result. The first element in the array is at index 0, which is why you use [0].
# ("one;two;three" -split ";")[0]
one
In your scenario, it would probably be something like this:
($env:psmodulepath -split ";")[0]

Mv file problems when moving file back in path

I am completing Zed Shaw's Learn Python the Hard Way and am stuck on a concept in the appendix command line crash course in Windows PowerShell.
My problem is with the move (mv) command, specifically moving a file farther back in the path (hope that makes sense). Here is what I did:
I created a directory called temp, and within that directory created a .txt file called awesome.txt and another dirctory called newplace. Then, I write the command "mv awesome.txt newplace" and the awesome.txt file is moved to the directory newplace. Great!
The problem is that I want to move the file awesome.txt back to its original place in the directory temp. When I change my working directory to the directory newplace "cd newplace" and then type "mv awesome.txt temp" the file awesome.txt does not move back to the directory temp, but instead converts from a .txt file to a "file" and stays put in the newplace directory.
Folders like this are nested inside each other:
c:\temp
c:\temp\newplace\
When you cd around, you go into a folder (, e.g. cd temp:
c:\temp\ (o_o)
c:\temp\newplace\
And you can only see things in the same folder you are in. So you can move into newplace because that name makes sense where you are. But when you are in newplace
c:\temp\
c:\temp\newplace\ (o_o)
You can't move to temp because you don't know where it is. You don't have an index of every directory name on the entire computer that you can shortcut to, you only have two options: something in the same place you are, or something with an absolute path - a full name of where it is. c:\temp\.
So mv awesome.txt temp tries to put it inside temp where you are -> c:\temp\newplace\temp\ -> that doesn't exist, so it assumes you're moving it to a new name in the same place.
You would need mv awesome.txt c:\temp\ to specify it properly.
Except there's a sneaky shortcut, anywhere you are, there is automagically a path called .. which means the folder one <-- thatway from where I am.
So you could mv awesome.txt ..\ to push it back one level in the path, without needing to know exactly where that is. This is probably what Zed Shaw is expecting.

How to copy a file from source to a destination and recreate the folder structure

I have some files which needs to be copied through deployment process often to a destination. This is my folder folder structure:
SOURCE:
c:\
folder1
sub1
subsub1
file1
file2
I need something where i can tell my "script" something like this
mycopy c:\folder1\sub1\subsub1\file1 h:\
That means that i dont want to
check if folder structure exist
provide on bot sides the complete structure for each file on destination side
I want to
provide the full path and filename on source side
create folder structure if not exist
overwrite file if exist
How can i achieve this?
See the robocopy help page on MS Technet
https://technet.microsoft.com/en-us/library/cc733145%28v=ws.11%29.aspx
In your case you would do something like
robocopy c:\folder1\sub1\subsub1 h:\ file1 <options ...>
Notice that the first two parameters are the source and dest paths only, with no filename at the end
Read the linked help page and test on your desktop ...
Ok it works like this:
You can use Robocopy for this task. Thanks you Rick716 for this direction. I am not marking it as answer cause it is only a direction not the solution.
Lets Assume that we have the following source folder structure:
N:\source\a1\b1\c1\d1\e1\f1
and we want to recreate the hole structure under n:\source within h:\destination. Then we have to use Robocopy in this way:
robocopy N:\source h:\destination /e
The option /e will create the folder structure even create empty folders. Additionally you can append the files which should be copied. For example
file.txt
*.jpg
*.bkp
etc. But these files will be even copied when they exist somewhere within the folder strcuture! For example you have the file n:\source\a1\file.txt and the file n:\source\a1\b1\c1\file.txt then both will be copied by using
robocopy N:\source h:\destination file.txt /e