Board logo

标题: [文件操作] 新人求救批处理复制超过一定时长的文件夹和文件 [打印本页]

作者: kangkang    时间: 2021-4-3 20:29     标题: 新人求救批处理复制超过一定时长的文件夹和文件

各位大神,你们好
      本人算是个BAT爱好者吧,之前看过一些大神写的处理命令,很是崇拜!也是我的目标
好了,闲话不多说,我的问题是这样的:
      公司制程会产生一堆数据文件,时间长了很占空间,500G的网盘常爆满,现在公司让部门各考贝到本机电脑硬盘上。
规则:网络上只保留三个月之内的数据文件,三个月之外的文件要我们移走。注意是  文件和文凭夹都考走,超过时间的删除这个命令我搞定了,
但只复制超过一定时长的文件夹和文件 搞不定,研究了下 XCOPY   /D命令 ,只能到指定日期及以后较新的,可我要的是指定日期以前,或是是三个月以前的啊,救指教~~~~
现有代码如下-------
  1. @echo off
  2. echo -----------
  3. echo 处理中
  4. echo -----------
  5. xcopy E:\一键数据副本 /s /b /d:01-03-2021 C:\Users\Administrator\Desktop\个评
  6. echo 请按任意键结束 & pause > nul
复制代码
以上帮忙看下要怎么改呢?
作者: Batcher    时间: 2021-4-3 20:46

回复 1# kangkang


    超过时间的删除,你用的是哪个代码?
作者: thp2008    时间: 2021-4-3 20:53

查询F:\test盘,90天之前产生的文件。
  1. powershell -c "Get-ChildItem -Path 'f:\test' -Recurse -ErrorAction:SilentlyContinue | Where-Object -FilterScript {(((get-date) - ($_.LastWriteTime)).days -gt 90 -and $_.PsISContainer -ne $True)} | Select-Object FullName"
复制代码
查询F:\test盘,所有修改日期在2021-03-25(包含)之后的所有文件。
  1. PowerShell "Get-ChildItem -Path 'F:\test' -Force -Recurse -ea SilentlyContinue | ?{$_.LastWriteTime.ToString('yyyy-MM-dd') -ge '2021-03-25' -and $_.PsISContainer -ne $True} | select FullName"
复制代码
查询F:\test盘,所有修改日期在2021-03-25(包含)之前的所有文件。
  1. PowerShell "Get-ChildItem -Path 'F:\test' -Force -Recurse -ea SilentlyContinue | ?{$_.LastWriteTime.ToString('yyyy-MM-dd') -le '2021-03-25' -and $_.PsISContainer -ne $True} | select FullName"
复制代码
查询F:\test盘,所有修改日期在2021-03-25 仅当天的所有文件。
  1. PowerShell "Get-ChildItem -Path 'F:\test' -Force -Recurse -ea SilentlyContinue | ?{$_.LastWriteTime.ToString('yyyy-MM-dd') -eq '2021-03-25' -and $_.PsISContainer -ne $True} | select FullName"
复制代码
最后的操作,自已参考下面改。
Select-Object FullName   查询并列出文件名
Copy-Item -Destination f:\testOK -Force  复制之些文件到f:\testOK的目录
Move-Item -Destination f:\testOK -Force  移动之些文件到f:\testOK的目录
Remove-Item -Force   删除这些文件

也可以看看我这只问过的。
http://www.bathome.net/thread-58073-1-1.html
作者: kangkangsz    时间: 2021-4-6 10:07

回复 2# Batcher


    forfiles /p "E:\一键数据副本" /s /m *.csv /d -30 /c "cmd /c del @path /q /f"
用的这个,超过30天的就删除



不好意思 ,之前那个帐号,密码忘记了,重新注册了一个
作者: Batcher    时间: 2021-4-6 11:25

回复 4# kangkangsz


    论坛的登陆页面上有一个【找回密码】的功能
作者: kangkangsz    时间: 2021-4-6 17:42

本帖最后由 kangkangsz 于 2021-4-7 09:47 编辑

回复 3# thp2008


    大神,我复制了代码试了下,是把后缀嵊这SP1吗?用不了啊
复制代码后生成PS1文件 ,以POWERSHELL运行,屏幕一闪而过就没了
作者: kangkangsz    时间: 2021-4-20 16:48

DERA ALl:
       另一个大神写了代码,如下请参考。

<# :
cls
@echo off
rem 将指定目录下修改日期在指定日期之前的文件按照原来的目录结构复制/拷贝到另一个目录里
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]:efault))) -Args '%~dp0'"
echo;努力的你,很可爱,加油!
pause
exit
#>
   
$oldfolder='E:\桌面文件';
$newfolder='F:\456';
$agodate=20210601;
if(-not (test-path -liter $oldfolder)){write-host ('"'+$oldfolder+'" not found');exit;};
$files=@(dir -liter $oldfolder -recurse|?{$_ -is [System.IO.FileInfo]});
for($i=0;$i -lt $files.length;$i++){
    if([int]$files[$i].LastWriteTime.toString('yyyyMMdd') -lt $agodate){
        $newpath=$newfolder.trimend('\')+$files[$i].Directory.FullName.Substring($oldfolder.trimend('\').length);
        $newfile=$newpath+'\'+$files[$i].Name;
        if(-not (test-path -liter $newpath)){[void](md $newpath -force);};
        write-host ($files[$i].FullName+' --> '+$newpath);
        cp -liter $files[$i].FullName $newfile -force;
    };
};
若提示 powershell不是外部、内部命令,也不是可运行的程序或批处理文件, 可能是你的电脑系统属于阉割版,建议换台电脑试
若提示 红字,或绿字 可能是代码复制不完整
作者: Batcher    时间: 2021-4-20 17:50

回复 6# kangkangsz


PowerShell代码的运行方法:
http://bbs.bathome.net/thread-31071-1-1.html




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2