【问题标题】:How to rename multiple folders using spreadsheet?如何使用电子表格重命名多个文件夹?
【发布时间】:2014-06-14 20:05:29
【问题描述】:

如果我使用的是 Windows 7 并且我有一个电子表格:

1, Ant
2, Brown Bear  (note whitespace)
3, Cat
4, Dinosaur

和文件夹

C:\Directory\1\
C:\Directory\2\
C:\Directory\4\

有什么方法可以放入批处理文件或 vbs 文件或该目录中的任何适当工具,以使用电子表格重命名这些文件夹?最终结果是:

C:\Directory\1 Ant\
C:\Directory\2 Brown Bear\
C:\Directory\4 Dinosaur\

到目前为止,我有一个批处理文件,它将向运行它的目录中的所有文件夹附加一个字符串(在运行它时要小心),但我只是对很多很多项目使用 if 语句。有关如何将其连接到 csv 文件的任何建议?

@echo 关闭

for /f "tokens=1,2,* delims=,(" %%a in ('dir /a:d /b') do (

    if "%%a" == "10" ( ren "%%a" "%%a Elephant" )
    if "%%a" == "11" ( ren "%%a" "%%a Otter")
)

提前致谢。

【问题讨论】:

    标签: windows batch-file directory


    【解决方案1】:

    您的问题中有几个不清楚的地方(例如名称:它们是在电子表格中还是在 .csv 文件中?)所以我做了一些假设。给定一个包含以下内容的 names.csv 文件:

    1, Ant
    2, Brown Bear
    3, Cat
    4, Dinosaur
    

    下面的批处理文件实现你想要的重命名:

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Load the array of equivalences from names.csv file:
    for /F "tokens=1* delims=," %%a in (names.csv) do set "name[%%a]=%%b"
    
    rem Rename the folders:
    cd C:\Directory
    for /F "delims=" %%a in ('dir /A:D /B') do ECHO ren "%%a" "%%a!name[%%a]!"
    

    上一个批处理文件只是显示 ren 命令;如果输出看起来不错,请删除 ECHO 部分以执行命令。

    如果 names.csv 文件的第一行有标题,请在第一个 for /F ... 命令的“令牌”之前插入“skip=1”。

    【讨论】:

      【解决方案2】:

      好吧,如果您可以使用 Powershell,这个小脚本可能会有所帮助:

      $invocation = (Get-Variable MyInvocation).Value
      $directorypath = Split-Path $invocation.MyCommand.Path
      
      Import-Csv "D:\Test\ps\look_up.csv" |`
          ForEach-Object {
              $old = $directorypath + "\" + $_.orig
              if(Test-Path($old)){            
                  $newPath = $directorypath + "\" +$_.orig + " " + $_.new
                  ren $old $newPath
              }
          }
      

      我在与脚本相同的文件夹中有名为 1,2,3 的文件夹和一个包含以下内容的 csv 文件:

      orig, new
      1, buga boo
      2, bla
      3, bu
      4, none
      

      运行脚本后,文件夹被命名

      Name
      ----
      1 buga boo
      2 bla
      3 bu
      

      【讨论】:

        猜你喜欢
        • 2016-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-14
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 2020-12-03
        相关资源
        最近更新 更多