How to get the most recent file using a batch file? - batch-file

I have a list of zip files with date and time appended like yyyymmdd_hhmmss_Demos.zip. Now how to get the most recently added zip file in the source dir. I need to copy this file in the target using copy command.
I found some info about forfiles, but do not have an idea on how to get it done for seconds.

You can use
pushd D:\a
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
copy "%newest%" D:\b
popd

set Path="D:\hello\abc\old"
for /f "tokens=*" %%a in ('dir /A:-D /B /O:-D /S %Path%') do set NEW=%%a&& goto:n
:n
echo %NEW%

pushd \\ryap\CONTROL_DATOS
for /f "tokens=*" %%a in ('dir \\ryap\CONTROL_DATOS /b /od') do set newest=%%a
Xcopy/Y "\\ryap\CONTROL_DATOS\%newest%" "D:\TXT_SOURCES\"
popd

Below snippet will extract the date and customize as per your needs
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
:: Pad digits with leading zeros e.g Sample_01-01-21.csv
set yy=%year:~-2%
set datestr=%day%-%month%-%yy%
Alternate way:
set datestr=%date:~0,2%-%date:~3,2%-%date:~6,2%

Related

Batch file - copy two last modified files

I would like to write a .bat file to move the two last modified files of a specific extension *.bak in directory a to a different directory.
I used this line to copy files:
robocopy D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups *.bak /S
I'm new with this and have no idea how to tweak this to get the result I need.
Thanks
not tested:
#echo off
for /f "tokens=* delims=" %%# in (' dir /a:-d /o:-d /t:a /b "D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups\*.bak"') do (
if not defined last set "pre_last=%%~f#"
set "last=%%~f#"
)
copy /y "%last%" "c:\new_dir"
copy /y "%pre_last%" "c:\new_dir"
#echo off
setlocal EnableDelayedExpansion
cd "D:\DailyBackup\IDMRObjects\SQLBackups"
set copied=0
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W *.bak') do (
copy "%%a" "other\dir"
set /A copied+=1
if !copied! equ 2 goto break
)
:break

to rename multiples file in multiple folders using batch

I have to write a batch file such that,it has to select the files from various folders as per the date passed in and copy those files to destination and rename the file(ie.if filename is like abcd_yyyymmdd then it will rename as abcd)
Please find below my batch file,it copies all respective files from source to destination as per the date specified,but the renaming is not done.
#ECHO OFF
SET date="20150211"
SET AAH_PATH=D:\ABC\Data\IN\ABCD
COPY D:\ABC\Data\IN\ABCD\*_%date%.txt D:\ABC\Data\File\*.txt
SET FOLDER_PATH=D:\ABC\Data\File
pushd %FOLDER_PATH%
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b" "%%a.txt"
popd
SET SIE_PATH=D:\ABC\Data\IN\SIE
COPY D:\ABC\Data\IN\SIE\*_%date%.txt D:\ABC\Data\NEWFILE\*.txt
SET SIE_DEST=D:\ABC\Data\NEWFILE
pushd %SIE_DEST%
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b" "%%a.txt"
popd
SET UB_ABF=D:\ABC\Data\IN\UB\ABF
COPY D:\ABC\Data\IN\UB\ABF\*_%date%.txt D:\ABC\Data\UB\*.txt
SET UB_BCL=D:\RBC\Data\IN\UB\BCL
COPY D:\RBC\Data\IN\UB\BCL\*_%date%.txt D:\ABC\Data\UB\*.txt
SET UB_DEST=D:\ABC\Data\UB
pushd %UB_DEST%
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b" "%%a.txt"
popd
Could someone help me fix this problem
Try add the file extension to the source path in each for /f ren loop...
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b.txt" "%%a.txt"

Partial path known..need to search for file type inside

There is a particular folder that begins with a name such as SS followed by random characters. The names would be different every time and the only thing we are sure of is the folder begins with SS. How do we look if this folder has a .txt file inside in batch programming.
An idea :
#echo off
for /f "delims=" %%a in ('dir /b/ad ^|find /i "SS"') do set $Dir=%%a
dir /b/a-d *.txt %$dir%>nul
if %errorlevel% equ 0 echo File(s) found in "%$DIR%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\ss*" 2^>nul'
) DO (
FOR /f "delims=" %%h IN (
'dir /b /a-d "%sourcedir%\%%a\*.txt" 2^>nul'
) DO (
ECHO "%sourcedir%\%%a\%%h"
)
)
GOTO :EOF
should solve your problem - you need to change sourcedir to suit your system, obviously.
The code below check if the folder contain any .txt file:
#echo off
set "filePath="
for /D %%a in (SS*) do if exist "%%a\*.txt" do set "filePath=%%a"
if defined filePath echo File exists in folder %filePath%
If you want to check for a particular .txt file, just change *.txt by the appropriate name.

change datetime text on files

I have this format on jpg files but i need the time and date like this which I am not able to get this.
present format
1UYK08HJ_20140403165858071_SYPTE1-PC.jpg
1YK0BHJX_20140403165902791_SYPTE1-PC.jpg
1YK08HJX_20140403165959270_SYPTE1-PC.jpg
IDYKDBH_20140403170236634_SYPTE1-PC.jpg
required format is
1UYK08HJ_SYPTE1-PC_2014_04_03_16_58_58_071.jpg
1YK0BHJX_SYPTE1-PC_2014_04_03_16_59_02_791.jpg
1YK08HJX_SYPTE1-PC_2014_04_03_16_59_59_270.jpg
IDYKDBH_SYPTE1-PC_2014_04_03_17_02_36_634.jpg
my query is :
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "tokens=1-4 delims=_." %%A in ('dir /b /a-d *_*.jpg') do (
Echo ren %%A_%%B_%%C.%%D %%A_%%C_%%B.%%D
)
popd
The method to separate : 20140403165858071 in 2014_04_03_16_58_58_071
#echo off&cls
setlocal EnableDelayedExpansion
for /f "tokens=1-4 delims=_." %%a in ('dir /b/a-d *.JPG') do (
set $Char=%%b
set $CharF=!$Char:~0,4!_!$Char:~4,2!_!$Char:~6,2!_!$Char:~8,2!_!$Char:~10,2!_!$Char:~12,2!_!$Char:~14!
echo !$CharF!
pause)
You can then adjust yourself the REN with %%a_!$CharF!_%%c_%%d or the way you want it

file exists with condition today's date

I have a folder where files are dumped with timestamps...
filename_ver20130405121320.csv
I wish to create a batch script that makes sure 5 files have been created with todays date.
im guessing i will need to use a for loop with a date limit of today.
FOR /r %foldername% %%g IN (*.csv) DO (
echo %%~nxg
)
using a forfiles statement lists the files, is it possible to use a counter and +=1 every time it displays a filename?
forfiles /S /P %foldername% /m *.csv /d 0
the logic is
if number of files in a foldername is less than 5 where file created is today
echo error! missing files
any help would be much appreciated
date returned on machine as Mon 22/07/2013
use this to set date
:: set date
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET dd=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET mm=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET setDate=%dd%/%mm%/%yyyy%
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET yyyy=2013
SET mm=07
SET dd=22
SET count=0
FOR /f %%g IN ('dir /b /a-d *%yyyy%%mm%%dd%????.csv') DO (
SET filename=%%~ng
SET filename=!filename:~-12,-4!
if "!filename!"=="%yyyy%%mm%%dd%" SET /a count+=1
)
ECHO %count%
GOTO :EOF
I've simply set yyyy,mm,dd to constants, obviously - just poke your date-decoder in as appropriate.
Note that you could prefix the filemask with a directoryname if required - and enclose the entire filemask in "rabbit's ears" if there are spaces or other confounding characters in the resultant mask.
Important: the filemask is merely a primary filter. The dir would list a file named filename_ver2013040512132.csv for instance (1 digit missing...) so the gymnastics with the processing would still be required.
I'm also assuming relatively sane filenames. Likely ! in a filename would cause conniptions.
I came up with this and it seems to work so far
for /f "tokens=2" %%I in ("%date%") do set today=%%I
for /f "tokens=5" %%G in ('dir %foldername% /a-d ^| find "%today%"') do (
set /a fileCounter += 1
echo %%G
)
echo %fileCounter%
This may work (untested): edited to check only the date in yyyymmdd format
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
for /f %%a in ('dir "*ver%datestamp%*.csv" /b /a-d^|find /c /v "" ') do (
if %%a LSS 5 echo files are missing
)

Resources