AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1840442
Accepted
Maslie
Maslie
Asked: 2024-04-26 16:56:01 +0800 CST2024-04-26 16:56:01 +0800 CST 2024-04-26 16:56:01 +0800 CST

使用powershell导出excel数据进行robocopy

  • 772

我手上有一个 300 行的 excel 表,其中包含用于 robocopy 的源路径和目标路径,为了避免将所有路径复制粘贴到 .bat 文件上,我尝试制作此 powershell 脚本:

# Import the Excel module
Import-Module ImportExcel

# Path to the Excel file
$excelPath = 'classeur.xlsx'

# Import data from the Excel file and check for null values
$data = Import-Excel -Path $excelPath
if ($data -eq $null) {
    Write-Host "No data was imported from the file. Please check if the file is empty."
    return
}

# Display imported data for verification
Write-Host "Imported data preview:"
$data | Format-Table -AutoSize

# File to store the source and destination paths
$outputFile = 'robocopy.txt'
if (Test-Path $outputFile) {
    Remove-Item $outputFile
}

# Iterate over each row of data
foreach ($row in $data) {
    $source = $row.Source
    $destination = $row.Destination

    # Check if either source or destination is empty
    if ([string]::IsNullOrWhiteSpace($source) -or [string]::IsNullOrWhiteSpace($destination)) {
        Write-Host "Empty source or destination found, skipping..."
    } else {
        # Format the line to write to the file
        $lineToWrite = "Source: `"$source`" - Destination: `"$destination`""
        Write-Host "Writing to file: $lineToWrite"
        $lineToWrite | Out-File -FilePath $outputFile -Append -Encoding UTF8
    }
}

Write-Host "All lines processed. Paths are stored in $outputFile"

当然,我复制了 excel 文件并重新构建它,以方便脚本读取

构造示例

来源 目的地
源文件路径 目的地 filapath
源文件路径 目的地 filapath

我期待的结果如下

SET _source1="Filepath to source"

SET  _destsge1="Filepath to destination"

SET _source2="Filepath to source"

SET  _destsge2="Filepath to destination"

ETC...

我目前得到的结果

Empty source or destination found, skipping...
Empty source or destination found, skipping...
Empty source or destination found, skipping...
Empty source or destination found, skipping...

我检查了什么:

  • powershell正确读取了excel,就是这样。
  • 我的PS版本是5.1.22621.2506
  • 以管理员身份执行脚本。
  • Excel 版本:版本 2403(内部版本 17425.20146)

我想指出的是,txt 文件和 excel 文件位于具有脚本的同一文件夹中。

有人可以知道为什么脚本无法读取数据我找不到任何特定于此问题的内容:

我使用的来源:https://www.sharepointdiary.com/2021/03/import-excel-file-in-powershell.html# :~:text=To%20import%20an%20Excel%20file%2C%20follow%20these %20steps%3A,%20Excel%20文件。%20For%20example%3A%20Import-Excel%20-Path%20C%3APathToExcelFile.xlsx

https://www.it-connect.fr/comment-manipuler-des-fichiers-excel-avec-powershell/

microsoft-excel
  • 1 1 个回答
  • 29 Views

1 个回答

  • Voted
  1. Best Answer
    Maslie
    2024-04-26T20:02:13+08:002024-04-26T20:02:13+08:00

    在@MT1的帮助下,问题是elseif的使用。

    相反,最好使用两个 if 来进行此配置。

    这里更新的脚本允许从 Excel 获取数据并相应地准备 robocopy 命令。请注意,您必须手动将日志文件输入到 Robocopy.txt

    Excel 文件的第一行必须有源,目标有标题,以便脚本获取信息

    # Import the Excel module
    Import-Module ImportExcel
    
    # Path to the Excel file
    $excelPath = 'classeur.xlsx'
    
    # Import data from the Excel file and check for null values
    $data = Import-Excel -Path $excelPath
    if ($data -eq $null) {
        Write-Host "No data was imported from the file. Please check if the file is empty."
        return
    }
    
    # Display imported data for verification
    Write-Host "Imported data preview:"
    $data | Format-Table -AutoSize
    
    # File to store the source and destination paths and robocopy commands
    $outputFile = 'robocopy.txt'
    if (Test-Path $outputFile) {
        Remove-Item $outputFile
    }
    
    # Writing initial setup commands to the file
    $initialCommands = @(
        "chcp 1252 > nul",
        "@ECHO OFF",
        "SETLOCAL"
        "SET _LogFile=Robocopy.log"
        "SET _options=/E /XX /SEC /COPYALL /PURGE /R:2 /W:5 /LOG:%_LogFile% /TEE /V"
        "SET _options2=/E /XX /SEC /COPYALL /PURGE /R:2 /W:5 /LOG+:%_LogFile% /TEE /V"
    )
    $initialCommands | Out-File -FilePath $outputFile -Encoding UTF8
    
    # Initialize a counter to create numbered variable names
    $counter = 1
    
    # Array to store ROBOCOPY commands
    $robocopyCommands = @()
    
    foreach ($row in $data) {
        $source = $row.Source
        $destination = $row.Destination
    
        # Check if the source is empty
        if ([string]::IsNullOrWhiteSpace($source)) {
            Write-Host "Empty source found, skipping..."
            continue
        }
    
        # Check if the destination is empty
        if ([string]::IsNullOrWhiteSpace($destination)) {
            Write-Host "Empty destination found, skipping..."
            continue
        }
    
        # Format the line to write to the file with SET command, including quotes around the paths
        $sourceLine = "SET `_source$counter=`"$source`""
        $destinationLine = "SET `_destst$counter=`"$destination`""
        $sourceLine | Out-File -FilePath $outputFile -Append -Encoding UTF8
        $destinationLine | Out-File -FilePath $outputFile -Append -Encoding UTF8
    
        # Determine which options variable to use
        $optionsVar = if ($counter -eq 1) { "_options1" } else { "_options2" } #Option 1 erase the content of the log file while option 2 write next to it
        # Prepare the ROBOCOPY command using the determined options variable and store it in the array
        $robocopyCommands += "ROBOCOPY %_source$counter% %_destst$counter% %$optionsVar%"
    
        # Increment the counter for the next pair of variables
        $counter++
    }
    
    # Append ROBOCOPY commands to the file after all SET commands
    $robocopyCommands | Out-File -FilePath $outputFile -Append -Encoding UTF8
    
    Write-Host "All lines processed. Paths and robocopy commands are stored in $outputFile"
    
    • 1

相关问题

  • 带有“和”运算符的 Excel 数据透视表

  • 如何对整列使用 Excel 的 LENGTH 函数?

  • Excel 数组(2 个变量)

  • 如何从 WSL 打开 office 文件

  • VBA根据文件名重命名工作表

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve