How to read directory in codeIgniter outside the application folder? - codeigniter

$dir = 'application/';
$map = directory_map($dir);
I am able to read directory inside the application folder with directory_map
but, not able to read system and other folder outside the application folder.....
$dir = '../application/';
$map = directory_map($dir);

FCPATH is a constant that represent a path to CodeIgniter's root directory.
To access system directory, use:
$dir = FCPATH . 'system';
$map = directory_map($dir);

Related

Enumerate ACL of the current directory using power shell

I have been assigned with the following task.
Create a foreachloop. You can use the following template:
$directory-variable-here
foreach ($item in $directory) {
Script block here
}
Above the foreachcondition, set a variable, $directory, to the contents of the current directory.
Replace the script block placeholder with the command to enumerate the ACL of a file, using the $itemvariable in place of the file name.
You'll need to use the following cmdlets:
Get-ChildItem(or any alias of Get-ChildItem, such as lsor dir)
Get-Acl
I need some help to solve this. Secondly what can be used to store the current directory path, I am confused.
So far I have tried the following script but it returns the current directory's content only not for subdirectories.
$dirpath= $PSScriptRoot
foreach ($item in $dirpath){
$var = Get-ChildItem $item
Get-Acl $var
}
I tried a lot and after some searching, I found the solution, here is the script
$mypath= $PSScriptRoot
$var = Get-ChildItem -Recurse -Path $mypath
foreach ($item in $var){
Get-Acl $item.FullName
}

Powershell script to create folders from list of names in a file

I have a text file that contains a list of files in different directories, e.g
C:\FolderOne\Testing.jpg
C:\FolderTwo\Test.jpg
I'd like to turn those names into folders. And if two files exist in one folder, e.g FolderOne, then both those files should go into the same folder.
I understand that the backslashes will also have to be replaced by underscores and the colon replaced by an underscore too.
Thus far, I'm able to get the contents from the file list and copy it into a specified folder. I would like to create a folder for each file in the file list, but the folder name needs to include C_FolderOne and C_FolderTwo and so on....
Any help would be appreciated!
What I have so far:
Param (
[String]$model
)
# Debug line
$model = "DEV2";
[String]$drive = "C:";
#Go to the PG_dump directory
CD "C:\Program Files\PostgreSQL\9.4\bin"
##Execute PG_dump with no password
.\pg_dump.exe --host localhost --port 5432 --username "postgres" --role "postgres" --no-password --format custom --blobs --verbose --file "C:\Test\$date-DEV-$X.backup" "DEV"
#Get the content from the file list
$file_list = Get-Content "$drive\Testing\DEV33.txt"
foreach ($file in $file_list)
{
Copy-Item $file -Destination "C:\Testing"
}
Here's a method to do what you're asking. It'll grab the parent path for the file and create the destination folder.
## Get the content from the file list.
foreach ($file in Get-Content -Path "$drive\Testing\DEV33.txt") {
$name = (Split-Path -Path $file -Parent) -replace '\\', '_' -replace ':'
New-Item -Path "path\to\$name" -ItemType Directory
}

Why are my zip files saving in system32 instead of the directory Im currently in?

I currently have a script that will take files from a directory I am in, copy them to a folder in the same directory, and then zip that into example.zip -- The only problem is that when I try to zip them using:
Add-Type -AssemblyName System.IO.Compression.FileSystem
function ZipFiles( $zipfilename, $sourcedir )
{
# Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false)
}
that zip file $zipfilename is saved to C:\Windows\system32 instead of C:\myDir\env\filesToZip where I am currently at. Any idea why this would be happening instead of creating the zip file in the directory I executed the PowerShell script from?
As PetSerAl hints at, .NET methods will default to the current working directory of the process (not necessarily that of powershell) when resolving relative path names.
Use Resolve-Path to resolve the path using the current location in powershell:
function ZipFiles( $zipfilename, $sourcedir )
{
$sourcepath = Resolve-Path $sourcedir
if($sourcepath.Provider -ne 'FileSystem'){
throw 'File system path expected for $sourcedir'
}
$destinationpath = Resolve-Path $zipfilename
if($destinationpath.Provider -ne 'FileSystem'){
throw 'File system path expected for $zipfilename'
}
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcepath.ProviderPath, $destinationpath.ProviderPath, $compressionLevel, $false)
}

How to tell powershell dir command to look a folder up?

The folder structure is:
--root
--root\source-code\
--root\powershell-scripts\
I need the method below that is inside the \powershell-scripts folder to target files inside \source-code:
function Test($param)
{
dir -Include ASourceCodeFile.txt -Recurse |
% { SomeMethod $_ $param }
}
What am I missing?
The $PSScriptRoot automatic variable contains the path of the directory in which the current script is located. Use Split-Path to find its parent (your --root) and Join-Path to get the path to the source-code folder:
Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'source-code'
$PSScriptRoot was introduced in PowerShell 3.0
A bit late, but maybe still helpful for someone:
Directory structure :
MyRoot\script\scriptrunning.ps1
config:
MyRoot\config.xml
to read the xml file from scriptrunning.ps1:
[xml]$Config = Get-Content -path "${PSScriptRoot}\..\config\config.xml"
if you have a script in --root\powershell-scripts\ and you want to reference something in --root\source-code\ or say get-content you can do this:
cd --root\powershell-scripts\
get-content '..\source-code\someFile.txt'
The ..\ references the parent directory which contains \source-code\ and then you reference or pull in file or scripts from that directory.
this was a trick that I used in vbs that I converted to PS...
$scriptPath = Split-Path $MyInvocation.MyCommand.Path -Parent
$a = $scriptPath.split("``\``") for ($i = 0 ; $i -lt $a.count-1 ; $i++){
$parentDir = $parentDir + $a[$i] <br>
if($i -lt $a.count-2){$parentDir = $parentDir + "``\``"}
}
Write-Output $parentDir

Powershell module and invoking files

I have a script based powershell module (.psm1) and I have it imported into my main script. This module however needs to call a batch file that is located inside its same directory but apparently it is unable to see it. Currently the function in question looks like this:
function MyFunction
{
& .\myBatch.bat $param1 $param2
}
How can I make the function see the batch file?
. is the current working directory, not the directory in which the module resides. The latter can be determined via the MyInvocation variable. Change your function to this:
function MyFunction {
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
$dir = Split-Path $Invocation.MyCommand.Path
$cmd = Join-Path $dir "myBatch.bat"
& $cmd $param1 $param2
}
Try this:
function MyFunction {
& (Join-Path (Split-Path $MyInvocation.MyCommand.Path) 'myBatch.bat') $param1 $param2
}

Resources