将随机文件夹存储在批处理脚本中?

时间:2016-08-18 04:26:28

标签: windows batch-file

我有一个脚本可以自动将NAPS2(扫描程序)最新扫描结果备份到另一个驱动器,并根据其时间戳重命名它... 问题是,NAPS2使用随机文件夹名称作为其扫描结果的父文件夹...例如 " C:\ Users \用户名\应用程序数据\漫游\ NAPS2 \恢复\ 4j4v.fbmv \ scanresult001.jpg"

这里是代码,我如何指向该随机文件夹?

SET ROOTDIR=C:\Users\Operator\AppData\Roaming\NAPS2\recovery\
for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i >NUL
SET RANDOMDIR=%ROOTDIR%%LATEST_DIR%
for /F "delims=" %%a in ('dir  /b /od "%RANDOMDIR%\*.jpg"') do set Youngest=%%a
echo Backing up %Youngest%

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set mydate=%YYYY%%MM%%DD%@%HH%%Min%%Sec%

copy "%Youngest%" "D:\Backup\%mydate%.jpg"

输出

C:\>for /F "delims=" %a in ('dir /b /od "C:\Users\Operator\AppData\Roaming\NAPS2
\recovery\jfwkui0s.pmd \*.jpg"') do set Youngest=%a
File Not Found

C:\>echo Backing up
Backing up

C:\>for /F "delims=" %a in ('wmic OS Get localdatetime | find "."') do set dt=%a


 :\>set dt=20160818125756.950000+420

C:\>set YYYY=2016

C:\>set MM=08

C:\>set DD=18

C:\>set HH=12

C:\>set Min=57

C:\>set Sec=56

C:\>set mydate=20160818@125756

C:\>copy "" "D:\Backup\20160818@125756.jpg"
The system cannot find the path specified.

我认为这可能是因为随机文件夹有"。"在它..我试过"新文件夹"它有空间而且也不起作用

1 个答案:

答案 0 :(得分:0)

为此,请使用https://stackoverflow.com/a/30311479/6550457

中显示的方法

您只想对目录这样做,而不是a-d使用ad

    SET ROOTDIR=C:\Users\username\AppData\Roaming\NAPS2\recovery\
    for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i>NUL
    echo Latest Dir = %LATEST_DIR%

因此,在您的脚本中,您可以将其用作:

    SET ROOTDIR=C:\Users\username\AppData\Roaming\NAPS2\recovery\
    for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i>NUL
    SET RANDOMDIR=%ROOTDIR%%LATEST_DIR%
    echo Backing up %RANDOMDIR%

    for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
    set YYYY=%dt:~0,4%
    set MM=%dt:~4,2%
    set DD=%dt:~6,2%
    set HH=%dt:~8,2%
    set Min=%dt:~10,2%
    set Sec=%dt:~12,2%
    set mydate=%YYYY%%MM%%DD%@%HH%%Min%%Sec%

    copy "%RANDOMDIR%" "E:\Backup\%mydate%.png"