batch file not adding folder structure correctly with spaces? - powershell

The batch file runs a PowerShell script. The issue is the path location has spaces. The batch file is only seeing the first word before the space.
SET targetDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces"
SET archiveDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"
SET siteID=ABC
REM Run the import Powershell script for files.
powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path "%targetDir%" -Filter *.dlu -Site %siteID% -ArchiveDir "%archiveDir%"
so when I run my PowerShell script it gets the parameters from the batch file
param
(
[string] $Path = $(Throw "You must specify a directory path containing the files to import."),
[string] $Filter = $(Throw "You must provide a file pattern/filter (e.g. *.dlu) to be used for locating the files to import"),
[string] $Site = $(Throw "You must provide a site identifier for the import."),
[string] $ArchiveDir = $null # If not provided, the imported files will be deleted rather than archived to another location.
)
but when I have the paths output to the screen I get:
Path= C:\First
Site= ABC
Archive= C:\First
What am I doing wrong?
If I run the .bat file with the hard code it works perfect.
REM SET targetDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces"
REM SET archiveDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"
REM SET siteID=ABC
REM Run the import Powershell script for files.
powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path "\\server.com\xxx\First Folder with spaces\Second folder with spaces" -Filter *.dlu -Site ABC -ArchiveDir "\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"

It's because you are duplicating the quotes. You set the quotes in the SET targetDir= statement, then you also do the quotes again in your call to powershell.exe. The resulting command is:
powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path ""\\server.com\xxx\First Folder with spaces\Second folder with spaces"" -Filter *.dlu -Site ABC -ArchiveDir ""\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive""

#MobyDisk already told what is the root cause of the problem at hand.
You should change your SET command lines in the batch file as follows:
SET "targetDir=\\server.com\xxx\First Folder with spaces\Second folder with spaces"
SET "archiveDir=\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"
Note the moved opening quotation mark ". This change avoids the quotes to become part of the variable values.
(You could simply also remove all the quotes, but then you might run into trouble with some special characters like ^, &. Any % signs in the path need still to be doubled though not to be removed unintentionally.)

Related

Get path for all files in folder exclude 1 file. Command Line

I am trying get path all files and folder in indicated path and except folder Sql as a list. Only folders in the indicated location, no subfolders and their contents.
I am trying this:
powershell Get-ChildItem bin\Release\* -Exclude Sql -Name
But it return only name without path. I need bin\Release\ + Name or full path.
I was trying to:
for /f "usebackq tokens=*" %a in (`dir /b/n "bin\Release\*"`) do echo bin\Release\%~a
But don't know how change echo to list, and how remove folder Sql.
I will be grateful for your help, I cannot cope with it myself.

copy "this folder" to network drive (Robocopy or PowerShell)

Have a folder structure on local drive like this
and a similar structure on network drive like
C:\Documents\Projects\
\\myServer\Storage\Projects\
For each new project I create locally folders like
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I need PowerShell or Batch file. The batch is stored in folder abc and it shall perform
Robocopy C:\Documents\Projects\abc \\myServer\Storage\Projects\abc
(of course some switches are reasonable)
Next project, for example xyz
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I expect
Robocopy C:\Documents\Projects\xyz \\myServer\Storage\Projects\xyz
Each time I create new project I copy my sample folder structure that is supposed to contain the batch so I can quickly do copy for the project to network,
I think this can be solved by using variables for local and server base path or by doing som
I don't know which Robocopy parameters you need, but this could be a start. This assumes that you place this script in each \Projects\abc directory and run it from there.
$DestinationShare = '\\myServer\Storage'
$SourcePath = $PSScriptRoot
$DestinationPath = $PSScriptRoot -replace ".*?(?<path>\\Projects\\.*)","$DestinationShare`${path}"
robocopy $SourcePath $DestinationPath
This could be turned into a function that would have less static coding.
It is going to be simpler to just robocopy the entire Projects directory whenever it is required.
In batch you could do something like this:
#echo off
set "src=%~dp0"
set "dst=\\myServer\Storage"
for /d %%d in (%src%.) do set "name=%%~nd"
robocopy %src% %dst%\%name%\ /mir
%~dp0 is the directory in which the script resides. The for loop then extracts the name of that directory from the full path.

Powershell: how to assign path in the beginning of a script

As a beginner: How can I place a path to folder in Powershell script? I would like to hard-code a path to a folder where the rest of the script would do some data munging. How to do that?
Do you mean?
$path = "C:\Windows"
Get-ChildItem $path
This will let you reference path in cmdlet parameters.
As per your comment:
Set-Location C:\Windows
will change working directory of the script to C:\Windows

Issue with permissions when creating and copy directories

I have created a PowerShell script for copying files to a directory, the script, first creates a folder , or forces a new folder event if it exists. Then copies a directory from another location. After copying, the files I then need to copy the correct web config depending on a value given by the user execturing the script. The issue I am having is I can copy the files, but all the files are set to read-only meaning when I try and copy the correct web.config, the script fails as access is denied.
This is a cut down version of script for simplicity.
$WebApp_Root = 'C:\Documents and Settings\user\Desktop\Dummy.Website'
$Preview_WebApp_Root = 'c:\applications\Preview\'
$Choice = read-host("enter 'preview' to deploy to preview, enter Dummy to deploy to Dummy, or enter test to deploy to the test environment")
if (($Choice -eq 'Preview') -or ($Choice -eq 'preview'))
{
$Choice = 'Preview'
$Final_WebApp_Root = $Preview_WebApp_Root
}
write-host("Releasing Build to " + $Choice +'...')
write-host("Emptying web folders or creating them if they don't exist... ")
New-Item $Final_WebApp_Root -type directory -force
write-host("Copying Files... ")
Copy-Item $WebApp_Root $Final_WebApp_Root -recurse
write-host("Copy the correct config file over the top of the dev web config...")
Copy-Item $Final_WebApp_Root\Config\$Choice\Web.configX $Final_WebApp_Root\web.config
write-host("Copying correct nhibernate config over")
Copy-Item $Final_WebApp_Root\Config\$Choice\NHibernate.config $Final_WebApp_Root\NHibernate.config
write-host("Deployed full application to environment")
Try to use -Force parameter to replace read-only files. From documentation:
PS> help Copy-Item -Par force
-Force [<SwitchParameter>]
Allows the cmdlet to copy items that cannot otherwise be changed,
such as copying over a read-only file or alias.

Need a PowerShell script that copies files from the current dir that the script is run from to another location

I was asked to write a PowerShell script that they can package in with their build updates. They will complete a build that gets dropped to a folder (say \server\build\release1.1.2). We need a script that takes all the files/folders from that folder and copies them to the appropriately named locations.
I need the script to also read the # of the current build from the folder title and create that same # build folder when it copies. Easy enough to that however I need the references to be all dynamic, so when that Release1.1.3 comes out wecan drop the same script into there and it will copy all the files to the appropriate directories (and create them if they don't exist).
This script should get you started. Run it to see an example of the values it produces.
# variable name chosen based on the automatic variable available to PowerShell Modules
$PSScriptRoot = ($MyInvocation.MyCommand.Path | Split-Path | Resolve-Path).ProviderPath
$BuildName = $PSScriptRoot | Split-Path -Leaf
#"
This script file is located at:
$($MyInvocation.MyCommand.Path)
The folder this script file is in is:
$PSScriptRoot
The name of the folder this script file is in is:
$BuildName
To copy files you might do this:
Copy-Item -Path `$PSScriptRoot\* -Destination C:\Install\`$BuildName -Recurse
"#