gpt4 book ai didi

c - 文件管理中的 Powershell 循环

转载 作者:行者123 更新时间:2023-11-30 17:33:18 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com