Copy filename to NEW filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Robbin2021
Posts: 4
Joined: 19 Aug 2021 07:50

Copy filename to NEW filename

#1 Post by Robbin2021 » 20 Aug 2021 05:25

I would appreciate if you could take the time to point me in the right direction that would permit me to complete my short batch file.

Code: Select all

dir "PHSM-Dates.dat" | find /v "Volume" | find /v "bytes" | find /v "Directory"

REM Do I need to redirect this output to a file?

06-19-2021  06:22           673,279 PHSM-Dates.dat

REM 06-19-2021 is the Month-Day-Year of file: PHSM-Dates.dat

REM Month-Day-Year is NOT a fixed value - can change daily

REM  06:22 is the time of file: PHSM-Dates.dat

REM time is NOT a fixed value - can change daily

REM I want to copy "PHSM-Dates.dat" to "PHSM-Dates 06-19-2021 06;22.dat" (note the substitution of semi-colon for colon)
I think there is a possibility that I may need to parse the 06-19-2021 filedate...

Thank you for your time and any help you may provide.
Last edited by Squashman on 20 Aug 2021 13:09, edited 1 time in total.
Reason: MOD EDIT:Please use code tags.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy filename to NEW filename

#2 Post by aGerman » 20 Aug 2021 11:15

Your files will get terribly sorted if the date is not in order YYYY MM DD. However, I accept your decision.

Code: Select all

for %%i in ("PHSM-Dates.dat") do set "fullname=%%~fi"
for /f "delims=" %%i in ('WMIC PATH CIM_DataFile WHERE "name='%fullname:\=\\%'" GET LastModified /value') do for /f "tokens=2 delims==" %%j in ("%%i") do set "dt=%%j"
copy "PHSM-Dates.dat" "PHSM-Dates %dt:~4,2%-%dt:~6,2%-%dt:~0,4% %dt:~8,2%;%dt:~10,2%.dat"
Steffen

Robbin2021
Posts: 4
Joined: 19 Aug 2021 07:50

Re: Copy filename to NEW filename

#3 Post by Robbin2021 » 22 Aug 2021 08:26

I thank you for your response. I will have to paste your code and determine if it will perform the task. I can see the file time is going to present some complications because of the colon so I can eliminate the file time as being a requirement for NEW filename,

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy filename to NEW filename

#4 Post by aGerman » 22 Aug 2021 10:42

There's no colon in the new name but a semicolon as required in your initial post. The colons in the code belong to the substring syntax of variable dt.

Steffen

Robbin2021
Posts: 4
Joined: 19 Aug 2021 07:50

Re: Copy filename to NEW filename

#5 Post by Robbin2021 » 23 Aug 2021 06:01

I've made some revisions. While it may not look pretty, I hope it will get the job done.

PHSM.BAT

Code: Select all

@echo off
:: First set an environment variable to the backup dir - makes it easier if you want to change the dir later
SETLOCAL

    SET BACKUPDIR=C:\Totalcmd\PHSM-Calendar
    SET SOURCEDIR=%APPDATA%\PHSM-Calendar
    SET ARCHIVEFILE=C:\Totalcmd\PHSM-Calendar\PHSM-Dates.rar

    for %%i in ("PHSM-Dates.dat") do set "fullname=%%~fi"
    for /f "delims=" %%i in ('WMIC PATH CIM_DataFile WHERE "name='%fullname:\=\\%'" GET LastModified /value') do for /f "tokens=2 delims==" %%j in ("%%i") do set "dt=%%j"

REM  I need to compare the dates of "PHSM-Dates.dat" and "%ARCHIVEFILE%". If the dates are EQUAL then goto end ELSE goto backup
REM  I don't need the time as part of the filename so I eliminated: " %dt:~8,2%;%dt:~10,2%"

       copy "PHSM-Dates.dat" "%BACKUPDIR%\PHSM-Dates %dt:~4,2%-%dt:~6,2%-%dt:~0,4%.dat"
dir "PHSM-Dates.dat" "%BACKUPDIR%\PHSM-Dates %dt:~4,2%-%dt:~6,2%-%dt:~0,4%.dat" | find /v "Volume" | find /v "bytes" | find /v "Directory"
REM  I need FOR to compare the dates of "PHSM-Dates.dat" and "%ARCHIVEFILE%". If the dates are EQUAL then goto end ELSE goto backup.
REM  A more elegant method would be test the dates of the two files and set an errorlevel.
::
::    08-20-2021  16:01           673,558 PHSM-Dates.dat
::
::
::    08-20-2021  16:01           673,558 PHSM-Dates 08-20-2021.dat
::
echo.
echo   Unless you want to create the %ARCHIVEFILE% file, press Ctrl-C now to QUIT!
echo.
pause

:backup
"%PROGRAMFILES%\WinRar\rar.exe" m /m5 /tl /ep "%BACKUPDIR%\PHSM-Dates.rar" "%BACKUPDIR%\PHSM-Dates*.dat"
dir *.rar | find /v "Volume" | find /v "bytes" | find /v "Directory"
echo.
pause

:end
ENDLOCAL
Thank you.
Last edited by aGerman on 23 Aug 2021 10:01, edited 1 time in total.
Reason: code formatting

Robbin2021
Posts: 4
Joined: 19 Aug 2021 07:50

Re: Copy filename to NEW filename

#6 Post by Robbin2021 » 24 Aug 2021 09:13

Hello Steffen (and All)

I think I've found a solution to my PHSM.BAT

Code: Select all

@echo off
:: First set an environment variable to the backup dir - makes it easier if you want to change the dir later
SETLOCAL ENABLEEXTENSIONS

    SET BACKUPDIR=C:\Totalcmd\PHSM-Calendar
    SET SOURCEDIR=%APPDATA%\PHSM-Calendar
    SET ARCHIVEFILE=C:\Totalcmd\PHSM-Calendar\PHSM-Dates.rar

    for %%i in ("PHSM-Dates.dat") do set "fullname=%%~fi"
    for /f "delims=" %%i in ('WMIC PATH CIM_DataFile WHERE "name='%fullname:\=\\%'" GET LastModified /value') do for /f "tokens=2 delims==" %%j in ("%%i") do set "dt=%%j"

FOR %%A IN (%SOURCEDIR%\PHSM-Dates.dat) DO (FOR /F "tokens=1 delims= " %%B IN ("%%~tA") DO SET SDATE=%%B)
FOR %%A IN (%ARCHIVEFILE%) DO (FOR /F "tokens=1 delims= " %%B IN ("%%~tA") DO SET ADATE=%%B)

echo.
dir "PHSM-Dates.dat" | find /v "Volume" | find /v "bytes" | find /v "Directory"
	echo %SOURCEDIR%\PHSM-Dates.dat %SDATE%
dir %ARCHIVEFILE% | find /v "Volume" | find /v "bytes" | find /v "Directory"
	echo %ARCHIVEFILE% %ADATE%
echo.
	if %SDATE%==%ADATE% (
		echo files dates are EQUAL and quiting the batch file
		goto end
	) else (
		echo files dates are not EQUAL! Copying "PHSM-Dates.dat" to "%BACKUPDIR%\PHSM-Dates %dt:~4,2%-%dt:~6,2%-%dt:~0,4%.dat"
	)

       copy "PHSM-Dates.dat" "%BACKUPDIR%\PHSM-Dates %dt:~4,2%-%dt:~6,2%-%dt:~0,4%.dat"
dir "PHSM-Dates.dat" "%BACKUPDIR%\PHSM-Dates %dt:~4,2%-%dt:~6,2%-%dt:~0,4%.dat" | find /v "Volume" | find /v "bytes" | find /v "Directory"
echo.
echo   Unless you want to create / update the %ARCHIVEFILE% file, press Ctrl-C now to QUIT!
echo.
pause

:backup
"%PROGRAMFILES%\WinRar\rar.exe" m /m5 /tl /ep "%BACKUPDIR%\PHSM-Dates.rar" "%BACKUPDIR%\PHSM-Dates*.dat"
dir *.rar | find /v "Volume" | find /v "bytes" | find /v "Directory"
echo.
pause

:end
ENDLOCAL
Now, you could probably "pretty" it up and make it more efficient (which I would welcome) but the above works for me.

Thank you
Last edited by Squashman on 24 Aug 2021 09:37, edited 1 time in total.
Reason: MOD EDIT: PLEASE USE CODE TAGS!!!!!!!!

Post Reply