c - 文件管理中的 Powershell 循环

标签 c powershell

我有 2000 个 pdb 文件,名称为 1.pdb2.pdb3.pdb4.pdb等等。我必须根据文件中出现的特定字符串将它们分类为 3 个文件夹。如果文件包含一行“REMARK 350 SOFTWARE DETERMINED QUATERNARY STRUCTURE: MONOMERIC”,那么我们必须将该文件从给定文件夹移动到路径为“E:\home\Software\”的文件夹” 我认为我们必须使用从 1 到 200 的 for 循环,打开该文件并检查特定字符串,然后根据该字符串移动该文件。 虽然我对 power-shell 不太熟悉,但我是用 C 编写的程序。但是我发现了一些语法错误,而且我正在处理 1K pdb 文件,所以在这些情况下使用 C 效率很低,尽管我有用 C 编写了代码,但我在给出路径时遇到问题,每种情况都会发生语法错误。 任何人都可以在这两种情况下帮助我吗?

  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
  #define MAX 1024
  #include <stdarg.h>
  main()
 {    
     FILE *fp;
     char line[MAX];
     char var[MAX];
     int j=0;
     char i;


     char inputpath[20]= ("E:/Project/imp/1");
     strcat(inputpath,".pdb");
     fp=fopen(inputpath,"r");
     int num;
     while(i<"EOF")
     {
         fscanf(fp,"%s",var);
         if(strcmp(var,"AUTHOR")==0)
        {
                j++;
               break;

        }
      }
     fclose(fp);

        if (j==1)

        {

           // printf("efg\n");


            system("move E:\\Project\\imp\\1.pdb d:\\" );
        }


    return 0;
}

最佳答案

此 Powershell 2.0+ 代码(替换了正确的路径)应该可以满足您的要求:

$baseFolderPath = 'C:\SourceFolderPath'
$targetFolderPath = 'C:\TargetFolderPath'
$findStr = 'REMARK 350 SOFTWARE DETERMINED QUATERNARY STRUCTURE: MONOMERIC'

# Get the list of .pdb files
$pdbList = Get-ChildItem -Path $baseFolderPath -Filter '*.pdb'

foreach( $pdb in $pdbList )
{
    # Make sure path is no folder
    if( Test-Path -Path $pdb.FullName -PathType Leaf )
    {
        $pdbLines = Get-Content -Path $pdb.FullName
        foreach( $line in $pdbLines )
        {
            # Check if line matches
            if( $line -like $findStr )
            {
                # Move file to target folder
                Move-Item -Path $pdb.FullName -Destination $targetFolderPath
                break
            }
        }
    }
}

关于c - 文件管理中的 Powershell 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23792435/

相关文章:

c - SIGINT 被多个进程忽略

c - x86_64 AT&T 汇编中的旋转函数

Azure PowerShell - Invoke-AzContainerInstanceCommand - 术语 'processÂ' 未被识别为 cmdlet 的名称

powershell - 禁止Outlook弹出窗口允许访问

c - 获取任意数据的函数 : Use void* or char*?

c - 相同的代码 (C) 无法在终端上运行,但可以在 IDE 上运行

c - 解析由逗号分隔的表 txt 文件组成的数组

azure - 如何从 MicrosoftGraphPasswordCredential 对象检索密码

Powershell Invoke-Expression 修改命令

file - Powershell-列出子目录并为每个子目录计数文件