【发布时间】:2017-05-02 10:25:24
【问题描述】:
我有这个脚本,它可以 100% 工作,但仅适用于单个项目
我想循环脚本并从 txt 文件中获取内容 您会看到,我的 scipt 搜索特定文件并将其复制到与该文件同名的现有文件夹中。 所以我想要的是从 2 个 txt 文件中获取文件夹的名称和文件的名称并循环脚本
我已经设法从 txt 文件中获取内容,但是如果我在我的 txt 文件中添加带有新值的第二行,我将无法循环脚本。 我总是得到错误:
Get-ChildItem:无法将“System.Object[]”转换为类型 参数“Filter”需要“System.String”。指定的方法是 不支持d。
好的,这是我的脚本:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
# Setup source and destination paths
$Src = '\\192.168.0.216\home\'
$Dst = 'C:\TEST\120629B\'
# Wildcard for filter
$Extension = '120629B.jpg'
# Get file objects recursively
Get-ChildItem -Path $Src -Filter $Extension -Recurse |
# Skip directories, because XXXReadMe.txt is a valid directory name
Where-Object {!$_.PsIsContainer} |
# For each file
ForEach-Object {
# If file exist in destination folder, rename it with directory tag
if(Test-Path -Path (Join-Path -Path $Dst -ChildPath $_.Name))
{
# Get full path to the file without drive letter and replace `\` with '-'
# [regex]::Escape is needed because -replace uses regex, so we should escape '\'
$NameWithDirTag = (Split-Path -Path $_.FullName -NoQualifier) -replace [regex]::Escape('\'), '-'
# Join new file name with destination directory
$NewPath = Join-Path -Path $Dst -ChildPath $NameWithDirTag
}
# Don't modify new file path, if file doesn't exist in target dir
else
{
$NewPath = $Dst
}
# Copy file
Copy-Item -Path $_.FullName -Destination $NewPath
}
好的,这就是我所做的更改和工作,但只使用一条记录
$Src = '\\192.168.0.216\home\'
$Dst = Get-Content 'C:\TEST\path.txt'
# Wildcard for filter
$Extension = Get-Content 'C:\TEST\file.txt'
【问题讨论】:
标签: loops powershell