powershell脚本重命名目录中的所有文件 [英] powershell script to rename all files in directory

查看:460
本文介绍了powershell脚本重命名目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务:

将有各种.csv文件转储到其中的目录。这些文件可能类似于file1.csv,file2.csv等。

问题:

迭代文件,将其重命名为标准化的 newfilename.csv'。此文件将由外部程序处理,并重命名并移出目录。
我有一个powershell脚本这一点到达:

Task:
Directory that will have various .csv files dumped into it. These could be like file1.csv, file2.csv, etc.
Problem:
Iterate through the files and rename one to a standardized 'newfilename.csv'. This file will then be processed by an external program and get renamed and moved out of the directory. I have a powershell script which does this up to a point:

ent## Set the file location where the log files are
$file = "C:\logs"   

$new = "newfilename.csv"
$chkFile = "C:\logs\newfilename.csv"
$fileExists = Test-Path $chkFile

## Loop through all the .csv files in the log folder
foreach ($file in gci $file -include *.csv -recurse){       
    if($fileExists -eq $True){
        Write-Host "that file exists!"
        Start-Sleep -s 60
    } else{
    Write-Host "file doesn't exist, renaming..."
    rename-item -path $file -newname ("$new")}
    Start-Sleep -s 10   
}

问题是,给定目录中的5个文件,它将通过,重命名第一个文件,然后,通过文件列表,它返回-eq $ True为$ fileExists的false。是文件列表只是不刷新,当它开始回来(IE:是有某种数组在'foreach'开始创建)?我从来没有写过一个PowerShell脚本。我应该使用一些其他的程序来浏览文件吗?

The problem is that, given 5 files in the directory, it will go through, rename the first file and then, going back through the file list, it returns 'false' on "-eq $True" for $fileExists. Is the file list just not refreshing when it starts back through (IE: is there some sort of an array being created at the beginning of the 'foreach')? I've never actually written a PowerShell script. Should I be using some other procedure to go through the files? Is there a "refresh" of the list I should be performing?

推荐答案

测试 $ chkFile 在循环运行之前执行一次。如果你想评估每个文件(例如,其他进程已经完成了它的工作,并移动newfilename.csv在睡眠期间),移动TestPath调用 foreach

The test for whether $chkFile is being done once before the loop runs. If you want to evaluate that for each file (e.g., the other process has already done its work and moved "newfilename.csv" away during the sleep), move the TestPath call inside the foreach.

是,在开始之前对 gci $ file -include * .csv -recurse foreach循环。如果你想每次刷新列表,我会做类似下面的事情,这实际上调用foreach循环多次。

Yes, the gci $file -include *.csv -recurse is evaluated once before starting the foreach loop . If you want to refresh the list each time, I'd do something like the following, which actually calls the foreach loop multiple times.

while($true) {
    $csvFiles = gci $file -include *.csv -recurse

    if ($csvFiles.Length -eq 0) {
         break
    }

    foreach ($file in gci $file -include *.csv -recurse) {       
        if (Test-Path $chkFile) {
            Write-Host "that file exists!"
            Start-Sleep -s 60
        } else {
            Write-Host "file doesn't exist, renaming..."
            rename-item -path $file -newname ("$new")
            Start-Sleep -s 10
        }
    }
}

这篇关于powershell脚本重命名目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆