标题: [文件操作] 【已解决】求助按时间变量批量复制文件到另一个文件夹 [打印本页]
作者: jave000 时间: 2021-7-11 15:13 标题: 【已解决】求助按时间变量批量复制文件到另一个文件夹
本帖最后由 jave000 于 2022-7-3 15:12 编辑
同类搜索简述:
按创建和修改时间将一批文件,从一个文件夹批量复制到另一个文件夹,并且两个文件夹的路径中间都有一个变量,需要手动输入,以及其中一个路径涉及到桌面,但为了可以分享给同事使用,识别任意电脑名下的桌面路径。
感谢楼下诸位
@powershell -c "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & exit /b
$j = read-host "Project Model Folder"
$C = [Environment]::GetFolderPath("Desktop")
$sour = "\\s1\$j\3\ed\","\\s1\$j\3\pd\"
$dest = "$C\S\$j\"
do
{
$minute = read-host "Minutes of Minutes"
$minute = $minute.trim()
}
while ($minute -match "\D")
Get-ChildItem -Path $sour -File |
Where-Object { ($_.CreationTime -gt (get-date).AddMinutes(-$minute)) -or ($_.LastWriteTime -gt (get-date).AddMinutes(-$minute)) } |
foreach-object {
write-host $_.fullname
copy-item $_.fullname -Destination $dest
}
作者: idwma 时间: 2021-7-11 18:10
本帖最后由 idwma 于 2021-7-11 19:48 编辑
@echo off&setlocal enabledelayedexpansion
set/p XXX=输入XXX具体名称
set/a tn=10000!time:~0,2!*60+10000!time:~3,2!
set/p t=填写分钟数:
set/a t=!tn!-!t!
set "p1=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "p2=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "d=!USERPROFILE!\Desktop\SPR\!XXX!"
for %%a in (%p1% %p2%) do (
for /f "tokens=1,2,4* delims= " %%i in ('dir/a-d "%%a" ^|findstr "^[0-9]"') do (
set tf=%%j
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!date:~0,10!" == "%%i" if "!tf!" gtr "!t!" echo copy "%%a\%%k" "!d!" /y &rem 测试看完输出把前面的echo删了
)
)
pause
作者: newswan 时间: 2021-7-11 20:07
powershell- $sour = "D:\New folder","D:\2"
- $dest = "x:\"
- do
- {
- $minute = read-host "多少分钟之内"
- $minute = $minute.trim()
- }
- while ($minute -match "\D")
-
- Get-ChildItem -Path $sour -File |
- Where-Object {$_.CreationTime -gt (get-date).AddMinutes(-$minute)} |
- copy-item $_.fullname -Destination $dest
复制代码
作者: jave000 时间: 2021-7-12 08:46
回复 2# idwma
没有结果
Missing operator.
作者: jave000 时间: 2021-7-12 09:10
回复 3# newswan
没有结果
作者: idwma 时间: 2021-7-12 15:03
回复 4# jave000
是修改了之后运行的吗
作者: newswan 时间: 2021-7-12 16:06
本帖最后由 newswan 于 2021-7-13 13:45 编辑
回复 3# newswan - $sour = "D:\~","\\PC\Downloads"
- $dest = "D:\xxx"
- do
- {
- $minute = read-host "多少分钟之内"
- $minute = $minute.trim()
- }
- while ($minute -match "\D")
-
- Get-ChildItem -Path $sour -File |
- Where-Object {$_.CreationTime -gt (get-date).AddMinutes(-$minute)} |
- foreach-object {
- write-host $_.fullname
- copy-item $_.fullname -Destination $dest
- }
复制代码
CreationTime 创建时间
LastWriteTime 最后更新时间
作者: jave000 时间: 2021-7-12 17:18
回复 6# idwma
是的
我把中文部分改了英文,因为我们系统只能是英文系统,中文是乱码
然后删除了
&rem 测试看完输出把前面的echo删了
其他没改,出来报错就是那样了
作者: idwma 时间: 2021-7-12 18:12
本帖最后由 idwma 于 2021-7-12 18:14 编辑
回复 8# jave000
%time%变量,小时数是个位时不显示0,换个方法获得当前时间
@echo off&setlocal enabledelayedexpansion
set/p XXX=输入XXX具体名称
set/p t=填写分钟数:
for /f "tokens=1,2 delims=: " %%i in ('time/t') do set/a tn=10000%%i*60+10000%%j
set/a t=!tn!-!t!
set "p1=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "p2=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "d=!USERPROFILE!\Desktop\SPR\!XXX!"
for %%a in (%p1% %p2%) do (
for /f "tokens=1,2,4* delims= " %%i in ('dir/a-d "%%a" ^|findstr "^[0-9]"') do (
set tf=%%j
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!date:~0,10!" == "%%i" if "!tf!" gtr "!t!" echo copy "%%a\%%k" "!d!" /y &rem 测试看完输出把前面的echo删了
)
)
pause
作者: jave000 时间: 2021-7-14 15:11
回复 9# idwma
还是没有反应,是否网盘目录不支持?但网盘目录我们手动复制是有权限的
作者: idwma 时间: 2021-7-14 15:19
回复 10# jave000
手动执行dir \\btssvr9\pds1\!XXX!\3d\eqp_dgn看有结果出来吗
作者: jave000 时间: 2021-7-14 15:32
回复 11# idwma
有的,显示了文件夹内所有文件以及具体到日分秒的列表
作者: idwma 时间: 2021-7-14 15:35
回复 12# jave000
那在提示填写分钟数的时候输个大点的数比如1440看有反应吗
作者: jave000 时间: 2021-7-14 15:58
回复 13# idwma
还是没有,我把三个目录都改成了具体目录,也没结果
作者: idwma 时间: 2021-7-14 16:30
回复 14# jave000
难道英文系统的时间格式显示和中文不一样?,可以发一部分dir/a-d \\btssvr9\pds1\!XXX!\3d\eqp_dgn的结果出来看一下吗
作者: jave000 时间: 2021-7-14 17:21
回复 15# idwma
中间删了很多,不然太长
Volume in drive \\btssvr9\pds1 is btssvr9 d
Volume Serial Number is B410-9C31
Directory of \\btssvr9\pds1\cj10\3d\eqp_dgn
07/09/2021 10:12 AM 19,456 a_b.dgn
02/01/2013 02:23 PM 3,612 E210AY3102.drv
05/17/2021 05:20 PM 613,376 su_v310.dgn
05/17/2021 05:20 PM 1,461 SU_V310.drv
264 File(s) 69,509,689 bytes
0 Dir(s) 242,106,290,176 bytes free
作者: idwma 时间: 2021-7-14 18:13
回复 16# jave000
虽然有5列多了一列,时间的位置一样的没有输出内容奇怪,确定这里面有今天的新文件吗
@echo off&setlocal enabledelayedexpansion
set/p XXX=输入XXX具体名称
set/p t=填写分钟数:
for /f "tokens=1,2 delims=: " %%i in ('time/t') do set/a tn=10000%%i*60+10000%%j
set/a tn=!tn!-!t!
set "p1=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "p2=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "d=!USERPROFILE!\Desktop\SPR\!XXX!"
for %%a in (%p1% %p2%) do (
for /f "tokens=2,5* delims= " %%i in ('dir/a-d "%%a" ^|find "!date:~0,10!"') do (
if "%%i" == "" (echo !date!没有今天的文件) else (echo 发现一个今天的文件%%j)
set tf=%%i
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!tf!" gtr "!tn!" (echo copy "%%a\%%j" "!d!" /y) else (echo 没有!t!分钟内的文件)
)
)
pause
作者: jave000 时间: 2021-7-15 08:51
回复 17# idwma
set "p1=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "p2=\\btssvr9\pds1\!XXX!\3d\pipe_dgn"
还是没有反应,你这个时间区分创建时间和更新时间吗?好像不区分吧
作者: jave000 时间: 2021-7-15 08:53
回复 7# newswan
修改成更新时间后运行成功了,谢谢
作者: idwma 时间: 2021-7-15 14:35
回复 18# jave000
默认的是用的修改时间,还是没反应尴尬了这就不知道了
作者: jave000 时间: 2021-7-15 22:01
回复 20# idwma
我明天试试删除所有表达时间的代码,只是单纯的复制文件看看有没有反应
作者: jave000 时间: 2021-7-16 22:15
回复 jave000
虽然有5列多了一列,时间的位置一样的没有输出内容奇怪,确定这里面有今天的新文件吗 ...
idwma 发表于 2021-7-14 18:13
在家用自己电脑测试了下,以下是运行状态:
中文都改成几个大写字母了
C:\Users\Siren>D:\2.BAT
C:\Users\Siren>setlocal enabledelayedexpansion
C:\Users\Siren>set/p XXX=input
input123
C:\Users\Siren>set/p t=min
min22
C:\Users\Siren>for /F "tokens=1,2 delims=: " %i in ('time/t') do set/a tn=10000%i*60+10000%j
C:\Users\Siren>set/a tn=1000022*60+1000013
C:\Users\Siren>set/a tn=!tn!-!t!
C:\Users\Siren>set "p1=D:\Downloads\!XXX!\789"
C:\Users\Siren>set "p2=D:\Downloads\!XXX!\456"
C:\Users\Siren>set "d=!USERPROFILE!\Desktop\!XXX!"
C:\Users\Siren>for %a in (D:\Downloads\123\789 D:\Downloads\123\456) do (for /F "tokens=2,5* delims= " %i in ('dir/a-d "%a" |find "!date:~0,10!"') do (
if "%i" == "" (echo !date!NO ) else (echo ONE%j )
set tf=%i
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!tf!" GTR "!tn!" (echo copy "%a\%j" "!d!" /y ) else (echo NOT )
) )
C:\Users\Siren>(for /F "tokens=2,5* delims= " %i in ('dir/a-d "D:\Downloads\123\789" |find "!date:~0,10!"') do (
if "%i" == "" (echo !date!NO ) else (echo ONE%j )
set tf=%i
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!tf!" GTR "!tn!" (echo copy "D:\Downloads\123\789\%j" "!d!" /y ) else (echo NOT )
) )
C:\Users\Siren>(
if "22:10" == "" (echo !date!NO ) else (echo ONE )
set tf=22:10
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!tf!" GTR "!tn!" (echo copy "D:\Downloads\123\789\" "!d!" /y ) else (echo NOT )
)
ONE
copy "D:\Downloads\123\789\" "C:\Users\Siren\Desktop\123" /y
C:\Users\Siren>(for /F "tokens=2,5* delims= " %i in ('dir/a-d "D:\Downloads\123\456" |find "!date:~0,10!"') do (
if "%i" == "" (echo !date!NO ) else (echo ONE%j )
set tf=%i
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!tf!" GTR "!tn!" (echo copy "D:\Downloads\123\456\%j" "!d!" /y ) else (echo NOT )
) )
C:\Users\Siren>(
if "22:05" == "" (echo !date!NO ) else (echo ONE- )
set tf=22:05
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!tf!" GTR "!tn!" (echo copy "D:\Downloads\123\456\-" "!d!" /y ) else (echo NOT )
)
ONE-
copy "D:\Downloads\123\456\-" "C:\Users\Siren\Desktop\123" /y
C:\Users\Siren>(
if "22:05" == "" (echo !date!NO ) else (echo ONE- )
set tf=22:05
set/a tf=10000!tf:~0,2!*60+10000!tf:~3,2!
if "!tf!" GTR "!tn!" (echo copy "D:\Downloads\123\456\-" "!d!" /y ) else (echo NOT )
)
ONE-
copy "D:\Downloads\123\456\-" "C:\Users\Siren\Desktop\123" /y
作者: idwma 时间: 2021-7-17 15:37
回复 22# jave000
日期和文件名位置不一样,系统时间12小时制和24小时又不一样,试一下这次的
@echo off&setlocal enabledelayedexpansion
set/p XXX=input
set/p t=min
set tn=!time: =0!
set/a tn=10000!tn:~0,2!*60+10000!tn:~3,2!
set/a t=!tn!-!t!
set "p1=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "p2=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "d=!USERPROFILE!\Desktop\SPR\!XXX!\"
echo !date!|findstr /i "[a-z]">nul && set dt=!date:~4,10! || set dt=!date:~0,10!
for %%a in (%p1% %p2%) do (
for /r "%%a" %%b in (*.*) do (
echo %%~tb | find "PM">nul && (set tf=%%~tb & set/a tf=(10000!tf:~11,2!+12)*60+10000!tf:~14,2!) || (set tf=%%~tb & set/a tf=10000!tf:~11,2!*60+10000!tf:~14,2!)
set df=%%~tb
if "!dt!" == "!df:~0,10!" if "!tf!" gtr "!t!" echo copy "%%b" "!d!" /y
)
)
作者: jave000 时间: 2021-7-17 23:04
回复 23# idwma
C:\Users\Siren>d:\2.bat
C:\Users\Siren>setlocal enabledelayedexpansion
C:\Users\Siren>set/p XXX=input
input123
C:\Users\Siren>set/p t=min
min123
C:\Users\Siren>set tn=!time: =0!
C:\Users\Siren>set/a tn=10000!tn:~0,2!*60+10000!tn:~3,2!
C:\Users\Siren>set/a t=!tn!-!t!
C:\Users\Siren>set "p1=D:\Downloads\!XXX!\789"
C:\Users\Siren>set "p2=D:\Downloads\!XXX!\456"
C:\Users\Siren>set "d=!USERPROFILE!\Desktop\!XXX!"
C:\Users\Siren>echo !date! | findstr /i "[a-z]" 1>nul && set dt=!date:~4,10! || set dt=!date:~0,10!
此时不应有 *60+10000!tf:~14。
C:\Users\Siren>echo %~tb | find "PM">nul && (set tf=%~tb & set/a tf=(10000!tf:~11,2!+12)*60+10000!tf:~14,2!) || (set tf=%~tb & set/a tf=10000!tf:~11,2!*60+10000!tf:~14,2!)
作者: idwma 时间: 2021-7-18 00:58
又踩了两个雷,for /r 的路径不能用上个循环的变量,括号里的括号要转义
@echo off&setlocal enabledelayedexpansion
set/p XXX=input
set/p t=min
set tn=!time: =0!
set/a tn=10000!tn:~0,2!*60+10000!tn:~3,2!
set/a t=!tn!-!t!
set "p1=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "p2=\\btssvr9\pds1\!XXX!\3d\eqp_dgn"
set "d=!USERPROFILE!\Desktop\SPR\!XXX!\"
echo !date!|findstr /i "[a-z]">nul && set dt=!date:~4,10! || set dt=!date:~0,10!
for %%a in ("%p1%" "%p2%") do (
pushd "%%a"
for /r %%b in (*.*) do (
echo %%~tb | find "PM">nul && (set tf=%%~tb & set/a tf=^(10000!tf:~11,2!+12^)*60+10000!tf:~14,2!) || (set tf=%%~tb & set/a tf=10000!tf:~11,2!*60+10000!tf:~14,2!)
set df=%%~tb
if "!dt!" == "!df:~0,10!" if "!tf!" gtr "!t!" echo copy "%%b" "!d!" /y
)
popd
)
作者: newswan 时间: 2021-7-23 02:58
$sour = "D:\~","\\PC\Downloads"
$dest = "D:\xxx"
do
{
$minute = read-host "多少分钟之内"
$minute = $minute.trim()
}
while ($minute -match "\D")
Get-ChildItem -Path $sour -File |
Where-Object { ($_.CreationTime -gt (get-date).AddMinutes(-$minute)) -or ($_.CreationTime -gt (get-date).AddMinutes(-$minute)) } |
foreach-object {
write-host $_.fullname
copy-item $_.fullname -Destination $dest
}
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |