修改 Robocopy 脚本以读取附加到文件夹中文件的数字,添加一个,并重命名正在复制的文件

Modify Robocopy script to read number appended to files in folder, add one, and rename file that is being copied

所以我在任务计划程序上设置了一个非常简单的脚本,每天一次将屏幕截图从一个文件夹移动到另一个文件夹。这很好用,但是当截屏时,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%

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

提前致谢!

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 以删除文件夹。

Windows 10 个 64 位

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

如果在没有足够间隔的情况下创建源屏幕截图,将会发生这种情况:

echo 工作时删除它。

命令:

@rem Count, increment, move w/ rename with Windows 10 64-bit CMD, for, set, and move 
cmd /q /v /e
pushd "%HOME%\Pictures\SkyLinesScreenshots"
FOR /f %g in ('dir /a-d ^| find "File(s)"') do set z=%g
cd "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Screenshots"
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, increment, move w/ rename with Windows 10 64-bit CMD, for, set, and move
@echo off
setlocal enableextensions 
pushd "%HOME%\Pictures\SkyLinesScreenshots"
FOR /f %%g in ('dir /a-d ^| find "File(s)"') do set z=%%g
cd "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Screenshots"
FOR /f "delims=" %%g in ('DIR /b /a-d') do (
   set zname=%%g
   Call :increment 
) 
popd
exit /b 

:increment
set /a z+=1 > nul
echo move "%zname%" "%HOME%\Pictures\SkyLinesScreenshots\screenshot%z%.png" 
exit /b

结果: