简体   繁体   中英

batch file not adding folder structure correctly with spaces?

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM