Reputation: 51
I need batch script to copy all files created on todays date from one directory to another and rename them all to a default name (ex. NAME54.pdf) and continue counting from destination`s maximum number in name.
Upvotes: 0
Views: 543
Reputation: 116
Here is a script that will take a source and destination directory, and copying the files from the source directory to the destination directory, renaming the scripts to NAMES#.pdf. The files copied will be the files with today's timestamp
@echo off
REM setting up variable expansion for later use
setlocal ENABLEDELAYEDEXPANSION
REM giving the inputted values easy to read variable names
SET source_dir=%1
SET dest_dir=%2
REM checks to see if the directories exist
if not exist "%source_dir%" (
set is_failure=true
)
if not exist "%dest_dir%" (
set is_failure=true
)
REM Instructions given before failure
if "%is_failure%"=="true" (
echo ERROR: The parameters passed were not directories\n\n
echo INSTRUCTIONS
echo.
echo command syntax:
echo script.bat 'path to source directory' 'path to destination directory'
exit /b 1
)
REM finding the NAME#.pdf files, and getting the highest number
SET i=0
for %%x in (%dest_dir%\NAME*.pdf) do (
set "FN=%%~nx"
set "FN=!FN:NAME=!"
if !FN! GTR !i! set i=!FN!
)
REM checking that the file exists
if exist %dest_dir%\NAME%i%.pdf (
set /A i=i+1
)
REM Iterating over the list of files, and copying them to the new name
REM Only taking the files with today's date
FOR /F "tokens=1,2,3,4" %%A IN ('"dir %source_dir% /OD /A-D "') DO (
echo %%A | findstr %DATE%>nul && (
set file_name=%%D
copy /Y %source_dir%\!file_name! %dest_dir%\NAME!i!.pdf
set /A i=i+1
)
)
This should accomplish what you need to do
Upvotes: 0