修改Robocopy脚本以读取附加到文件夹中文件的编号,添加一个,然后重命名要复制的文件

问题描述 投票:-1回答:2

因此,我在任务计划程序上设置了一个非常简单的脚本,每天将屏幕快照从一个文件夹移动到另一个文件夹。没问题,但是在截取屏幕截图时,Steam会自动给它们命名,例如“ Screenshot.png”,“ Screenshot 1.png”,“ Screenshot 2.png”等,具体取决于文件夹中的数量。因此,每天将文件清空到另一个文件夹时,当天拍摄的每组屏幕截图都将从“ Screenshot.png”开始,然后从那里向上移动。结果是,当它们移动时,它们将被覆盖或追加(1)或其他内容(不确定哪个尚未运行!)

因此,我想更改以下批处理文件以在目标文件夹的“ Screenshot xxx.png”中找到最高编号,添加一个,将源文件夹中的文件重命名为此编号,然后进行移动。必须将其放入一个循环中,这样它才能遍历源文件夹中的所有文件。

@echo off

set X=1

set "source=C:\Users\Leigh\AppData\Local\Colossal Order\Cities_Skylines\Screenshots"

set "destination=D:\Leigh\Pictures\SkyLinesScreenshots"

robocopy "%source%" "%destination%" /mov

exit /b

我玩了一个游戏,找到了一个脚本来确定下一个数字应该是哪个,但无法正确使用正则表达式:

@echo off
    setlocal enableextensions disabledelayedexpansion

    setlocal enabledelayedexpansion
    set "nextFile=0" & for /f "delims=" %%a in ('
        findstr  /b /r /x 
                       /c:"\bScreenshot\b\s[0-9]+\.png"
    ') do if %%~na geq !nextFile! set /a "nextFile=%%~na+1"
    endlocal & set "nextFile=%nextFile%"

    echo Next file will be: %nextFile%

如果我做对了,我会将它们整合在一起,解决问题。这很清楚需要做什么,因此任何人都可以给我正确的正则表达式,或者如果愿意的话可以给我更多。

预先感谢!

regex batch-file robocopy
2个回答
0
投票

Windows 10 64位。 PowerShell 5

使用CMD和robocopy将屏幕快照归档到时间戳文件夹(mmddyy)。使用PowerShell删除超过14天的存档。

@echo off
setlocal enableextensions
set "source=C:\Users\Leigh\AppData\Local\Colossal Order\Cities_Skylines\Screenshots"
set "destination=D:\Leigh\Pictures\SkyLinesScreenshots\%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4,2%"
robocopy "%source%" "%destination%" /mov
rem delete archives older than 14 days. Remove -whatif 
powershell -command "get-childitem "D:\Leigh\Pictures\SkyLinesScreenshots" |? {$_.psiscontainer -and $_.lastwritetime -lt (get-date).adddays(-14)} |% {remove-item $_ -recurse -force -whatif}"
exit /b

您可以通过.adddays调整年龄阈值。这是在WhatIf模式下。删除-whatif以实际删除文件夹。


0
投票

Windows 10 64位。

计数,移动/重命名,使用Windows 10 64位CMD递增,进行设置和移动。

满意时,请删除echo

CMD:

@rem Count, move w/ rename, increment with Windows 10 64-bit CMD, for, set, and move 
cmd /q /v /e
pushd "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Screenshots"
FOR /f %g in ('dir /a-d ^| find "File(s)"') do set /a z +=%g 
FOR /f "delims=" %g in ('DIR /b /a-d') do (
   set /a z+=1 > nul
   echo move "%g" "%HOME%\Pictures\SkyLinesScreenshots\screenshot!z!.png" 
) 
popd 
exit /b 

脚本:

@rem Count, move w/ rename, increment with Windows 10 64-bit CMD, for, set, and move 
@echo off 
setlocal enabledelayedexpansion
pushd "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Screenshots"
FOR /f %%g in ('dir /a-d ^| find "File(s)"') do set /a z +=%%g 
FOR /f "delims=" %%g in ('DIR /b /a-d') do (
   set /a z+=1 > nul
   echo move "%%g" "%HOME%\Pictures\SkyLinesScreenshots\screenshot!z!.png" 
)
setlocal disabledelayedexpansion 
popd 
exit /b 
© www.soinside.com 2019 - 2024. All rights reserved.