I'm trying to figure this out need some help. I need to make a program that copy's my Log file and rename with date and time to different directory. Prior to loading the program not sure how to do it if any help would be great.
Here's a batch file that will copy all log files to an archive folder appending the date to each:
#Echo Off
#For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
#For %%a in ("*.log") do copy %%a "archive\%%~na_%All%.LOG"
Related
I have to seperate codes.
This creates a directory with only the date, but i don't know how to put the time on the end.
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir c:\%date:/=%
And I have this to copy the files:
robocopy "%appdata%\saves" "C:\Users\redfi\OneDrive\Savesbackup" /e /xf
They both work individually, but I want to put them in one batch.
I want it to create the directory with the current date and time, and then copy the saves in it. So I can restore older saves if I want.
Thank you!
I managed to figure it out. You might need to edit the date formats.
echo off
set CUR_YYYY=%date:~0,4%
set CUR_MM=%date:~5,2%
set CUR_DD=%date:~8,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME=%CUR_YYYY%.%CUR_MM%.%CUR_DD%_%CUR_HH%.%CUR_NN%
mkdir C:\Users\redfi\OneDrive\Minecraftbackup\%SUBFILENAME%
robocopy "%appdata%\.minecraft\saves" "C:\Users\redfi\OneDrive\Minecraftbackup\%SUBFILENAME%" /e /xf
I used the following to create a folder with a date and timestamp
#echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a-%%b)
set mydir="%mydate%-%mytime%"
mkdir %mydir%
Instead of created a folder named 2020-5-19-11-30 AM, I get one folder named 2020-5-19 and another folder called AM.
Any idea? I just want to run a batch file that will create a date and time including am or pm for a folder.
I am trying to rename some MP4 files based on file size of mp4 files in another directory. I want to name all files with identical sizes to same name. Meaning if the file size of the source file matches the size of file in the comparison directory, the source file is renamed to whatever the compared file is named. Because both directories need to be read recursively I'm thinking it would be easier to make a list for comparison with the info in it in 2 columns by using the DIR /s /b echo %%~zs>>filesizelist.txt command giving me a list like
123456789 movie.mp4
987654321 movie2.mp4
Then pass all source mp4s to the batch file and if %%~za matches a value in first column then ren the file to the
corresponding filename. Is this the best path? I tried to script it to work on the fly and that was both a no-go and the source of my 3 day headache(plus the reference list rarely changes and is obviously easily updated). Can someone please assist me with the script?
I do some test with my mp4, and the code works, and for you perform your test, you w´ll need change/put this 2 line above with the path to your folder/directory (one to keep and another to compare), by replacing in the line code is like this:
`set "_target_to_keeped=C:\Users\ecker\Videos\Target"`
`set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"`
You need add the folder where are files to keep and files to rename (if size+name match) on same lines where are the 2 lines code up in this test (sorry not explain in good English, my English is not help me). By now, is late 01:53, i need sleep... yep! so, have nice code!
#echo off && setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0"
set /a _cnt_in_looping= 1 - 1
set /a _cnt_files_size= 1 - 1
set "_target_to_keeped=C:\Users\ecker\Videos\Target"
set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"
cd /d "!_target_to_keeped!"
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4" 2^> nul ^| find "" /v /c') do set _cnt_in_looping=%%i
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4"') do (
set "_file_now_keep=%%i"
set "_file_now_keeped=%%~zi %%i"
call :_to_compare_:
)
set /a _total_files_renamed=!_cnt_in_looping! - !_cnt_files_size!
set /a _total_files_n_chang=!_total_files_renamed! - !_cnt_in_looping! * -1
echo/Total of files renamed = !_total_files_renamed!
echo/Total of files n chang = !_total_files_n_chang!
endlocal
goto :_end_of_file_:
:_to_compare_:
if not exist "!_file_now_keep!" exit /b
for /f "tokens=*" %%I in ('^<nul dir /o-d /on /b "!_file_now_keep!"') do (
set "_file_now_compare=%%~zI %%I"
set "_path_now_compare=%%~dpI"
if "!_file_now_compare!" == "!_file_now_keeped!" (
rename "!_path_now_compare!\%%I" "%%~zI %%I"
echo/ rename "%%~I" "%%~zI %%I"
if ["!errorlevel!"]==["0"] call set /a _cnt_files_size=!_cnt_files_size! + 1
timeout /t 10
)
)
exit /b
:_end_of_file_:
I would like to robocopy a directory and it's subdirectories to another directory. If a file at source is newer then I would like to make a copy of this file by adding a date/time stamp at end of the filename at destination and do the copy to destination.
I do not see any switches in the robocopy to do this. Can someone guide me how to do this.
Robocopy doesn't have a renaming switch, but you can use the rename command on the resulting files to add timestamps. Here's an example batch file:
#echo off
for /f "tokens=1-3 delims=. " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
dir Directory1\ /b > list
robocopy Directory1\ Backup\
for /f %%f in (list) do rename Backup\%%f %%~nf%mydate%_%mytime%%%~xf
Note that you will need to change the delimiters of the date depending on which country standard you follow. You can get that by executing date /t
I've been tasked with updating an existing windows scheduled task. The task simply calls a windows console based FTP command using an existing script text file using the -s:ftpScript.txt syntax.
The problem is that the filename has changed and will now be based on the current date such as filename20110101.txt.
How can I get the -s:ftpScript.txt to understand that there is a dynamic filename now required? Do I have to recreated the "ftpScript.txt" file dynamically each time the task fires to then include a new static file containing the current date based filename?
I've now since followed my initial suggestion and it works perfectly. I've posted the following below in case it helps anyone else;
echo Generating New FTP Request
set request=Request.txt
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET month=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET day=%%B
FOR /F "TOKENS=3,4 DELIMS=/ " %%A IN ('DATE /T') DO SET year=%%B
set /A day=%day%-1
set yesterday=%year%%month%%day%
set file=<filename>
(
echo open <server>
echo <pass>
echo get %file%%yesterday% %file%%yesterday%
) > %request%
ftp -i -s:%request%