0

I have a script that automatically backup NAPS2 (Scanner program) newest scan result to another drive, and also rename it according its timestamp... Problem is, NAPS2 uses randomized folder name as the parentfolder of its scan result... e.g "C:\Users\username\AppData\Roaming\NAPS2\recovery\4j4v.fbmv\scanresult001.jpg"

Here's the code, how do i point to that randomized folder?

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"

Output

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.

I think it's probably because the randomized folder has "." in it.. I tried "New Folder" which has space in it and it doesnt work as well

1 Answers1

0

To do this use the approach shown in https://stackoverflow.com/a/30311479/6550457

You'll want to do it only for directories though so instead of a-d use 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%

so in your script you'd use it as:

    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"
Community
  • 1
  • 1
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • How do i call the latest_dir? Getting "file not found" with this... But it lists the random folder correctly for /F "delims=" %%a in ('dir /b /od "C:\Users\Operator\AppData\Roaming\NAPS2\recovery\%LATEST_DIR%\*.png"') do set Youngest=%%a copy "C:\Users\Operator\AppData\Roaming\NAPS2\recovery\%LATEST_DIR%\%Youngest%" "E:\Backup\%mydate%.png" – denywinarto Aug 18 '16 at 05:17
  • I've added the usage in my answer. – FloatingKiwi Aug 18 '16 at 05:24
  • for /F "delims=" %%a in ('dir /b /od "%RANDOMDIR%\*.jpg"') do set Youngest=%%a My script also needs youngest function.. but Im getting file not found when running this. .edited the main question – denywinarto Aug 18 '16 at 05:33
  • @denywinarto put code into backticks `\`likethis\`` or no one can read what you write – phuclv Aug 18 '16 at 05:39
  • If youngest is supposed to be the random folder name then just rename latest_dir to youngest. – FloatingKiwi Aug 18 '16 at 05:40
  • youngest is the latest image scan result in jpg format inside that random folder`for /F "delims=" %%a in ('dir /b /od "%RANDOMDIR%\*.jpg"') do set Youngest=%%a echo Backing up %Youngest%` why is this not working? – denywinarto Aug 18 '16 at 05:41
  • `"%RANDOMDIR%\*.jpg"` it's already there :) The error is strange, `C:\Users\username\AppData\Roaming\NAPS2\recovery\randomfolder .jpg File not Found` there's a space after random folder – denywinarto Aug 18 '16 at 05:50
  • Post your current script and the output as an edit and I'll have a look. – FloatingKiwi Aug 18 '16 at 05:55
  • The problem is `%%i >NUL` should be `%%i>NUL` – FloatingKiwi Aug 18 '16 at 06:01