BOPBOP Posted April 20, 2011 Share Posted April 20, 2011 (edited) ::This takes txt files located in a directory c:\new and while making a copy in the ::directory c:\import it appends the date to the file name in the format _YYYYMMDD::A log file is created but it just says files copied::I would like the log file to list the newly copied files that have the date appended. See code. Thanks!!@Echo Off:startclsset /p input= This program appends date to your filenames, do you want to continue (y/n)?:if /i "%input%" equ "y" goto :AppendDateif /i "%input%" equ "n" goto :ExitScreenecho Invalid choice & pause & goto start@Echo On:AppendDateecho Success! Dates Appended. See log file AppendlogYYYYMMDD.log for details.@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( Set FileDate=%date:~10%%date:~4,2%%date:~7,2%)@For %%a in (c:\new\*.txt) do copy %%a "c:\import\%%~na_%FileDate%.*">>c:\Appendlog%date:~10%%date:~4,2%%date:~7,2%.logpauseexit:ExitScreenecho Dates will NOT be appended to filenamespauseexit Edited April 20, 2011 by Yzöwl code tags added Link to comment Share on other sites More sharing options...
allen2 Posted April 20, 2011 Share Posted April 20, 2011 (edited) I modified a little your code to suit your need. IT will work as long as you're executing it on an english system (because of the date).@Echo Off:startclsset /p input= This program appends date to your filenames, do you want to continue (y/n)?:if /i "%input%" equ "y" goto :AppendDateif /i "%input%" equ "n" goto :ExitScreenecho Invalid choice & pause & goto start:AppendDateecho Success! Dates Appended. See log file AppendlogYYYYMMDD.log for details.For /F "tokens=1,2,3 delims=/ " %%d in ("%date%") do (Set FileDate=%%f%%d%%e)if exist c:\Appendlog%Filedate%.log del /q /F c:\Appendlog%Filedate%.logFor %%a in (c:\new\*.txt) do ((echo copying "%%a" to "c:\import\%%~na_%FileDate%.txt" >>c:\Appendlog%Filedate%.log ) && (copy "%%a" "c:\import\%%~na_%FileDate%.txt" >>c:\Appendlog%Filedate%.log ))pauseexit:ExitScreenecho Dates will NOT be appended to filenamespauseexit Edited April 20, 2011 by allen2 Link to comment Share on other sites More sharing options...
BOPBOP Posted April 20, 2011 Author Share Posted April 20, 2011 Thanks Allen2! That's it!I'll play with the date structure later. I appreciate your help and quick response. Link to comment Share on other sites More sharing options...
BOPBOP Posted April 20, 2011 Author Share Posted April 20, 2011 opps. I know you mentioned the format change in the date but I did not look close enough to the code until just now. I forgot I avoided using %date% because it formatted the date to whatever the computer's setting were. And this batch file will be used on various computersI specifically need the date in the format of my original code or sometthing that gives me YYYYMMDDBut what you supplied gives me something to go on. Thanks! Link to comment Share on other sites More sharing options...
BOPBOP Posted April 20, 2011 Author Share Posted April 20, 2011 (edited) okay. Awesome, I combined your code into mine so I could keep my date format. Plus I like how you also showed me I was repeating code that I had already set in FileDate here it is. Again your help was much appreciated.@Echo Off:startclsset /p input= This program appends date to your filenames, do you want to continue (y/n)?:if /i "%input%" equ "y" goto :AppendDateif /i "%input%" equ "n" goto :ExitScreenecho Invalid choice & pause & goto start@Echo On:AppendDateecho Success! Dates Appended. See log file AppendlogYYYYMMDD.log for details.@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( Set FileDate=%date:~10%%date:~4,2%%date:~7,2%)if exist c:\Appendlog%Filedate%.log del /q /F c:\Appendlog%Filedate%.log @For %%a in (c:\new\*.txt) do ((echo copying "%%a" to "c:\import\%%~na_%FileDate%.txt" >>c:\Appendlog%Filedate%.log ) && (copy "%%a" "c:\import\%%~na_%FileDate%.txt" >>c:\Appendlog%Filedate%.log )) pauseexit:ExitScreenecho Dates will NOT be appended to filenamespauseexit Edited April 20, 2011 by Yzöwl code tags added Link to comment Share on other sites More sharing options...
Yzöwl Posted April 20, 2011 Share Posted April 20, 2011 There are a few things I'd like to mention.The first is this pointless code:@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( Set FileDate=%date:~10%%date:~4,2%%date:~7,2%)when this is all you used:@SET FileDate=%DATE:~10%%DATE:~4,2%%DATE:~7,2%There is no need at all to use the /Q switch with your DEL command and I'd strongly suspect that because your writing the file yourself it will also not be read only and will therefore not need the /F switch.Don't use labels in your file which can be confused as commands i.e. STARTIs there a particular reason why you turned ECHOing back ON?Be aware that your method of creating dates to your preferred format is not going to be the same on all systems.BTW, if your code is used only on systems consistent with the same date order it does not mean that they will all include the day at the beginning. For this reason I'd suggest you use the following idea to formulate the required output.%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%(It works from the back of the date thus ignoring the possibility of something tagged to the beginning.) Link to comment Share on other sites More sharing options...
BOPBOP Posted April 21, 2011 Author Share Posted April 21, 2011 Yzowl, Thanks so much for your input. I’m a novice programmer (probably obvious) and use what tutorials are available to me as well as viewing what others have done in similar cases. This leads to somewhat piecemealed program code.Early on I had a little trouble getting the date to work in the format I needed. I was hung up trying to use %date% and saw someone else who had the same issue. A bit of code was supplied to them which I in turn edited and used for my purpose. So in regards to my pointless code :-) I’m not surprised I might have something in there I don’t really need. I’ll try out what you suggested because I much prefer “cleaner” code (also makes it easier to edit). I’ll have a look at the switches (/q and /f).START is now renamed, good point. Since my last post I’ve been adding other code parameters to this batch file and realized the echo back on was useless. I forgot what I was doing with that earlier. It’s gone.I’ll look into your last point about the date. I need to play with it to see what you mean because I do not want the day at the beginning, all I want is YYYYMMDDSo, I just want to say I am always appreciative of anybody giving me pointers, especially when I am committing programming faux pas. It’s hard enough to figure out how I should edit my code without having extra lines that do nothing :-) thanks again for taking the time to look at the code and offer help. Link to comment Share on other sites More sharing options...
jaclaz Posted April 21, 2011 Share Posted April 21, 2011 About getting DATE this might be of interest:http://www.robvanderwoude.com/datetiment.phpparticularly DATEFMT.BATjaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted April 21, 2011 Share Posted April 21, 2011 So in regards to my pointless code :-) I’m not surprised I might have something in there I don’t really need.The code was pointless because you used a FOR loop to generate three variables, (%%A, %%B and %%C), then proceeded not to use or reference them.I’ll look into your last point about the date. I need to play with it to see what you mean because I do not want the day at the beginning, all I want is YYYYMMDDI'll explain with a batch file you can run to see:@ECHO OFFSETLOCAL ENABLEEXTENSIONS(SET MYDATE=FRI 04/22/2011)ECHO=%%MYDATE%%=%MYDATE%ECHO=ECHO=GET THE LAST FOUR CHARACTERS OF %%MYDATE%%ECHO=%MYDATE:~-4%ECHO=THE YEAR IS DISPLAYED ABOVEECHO=ECHO=GET THE FIRST TWO OF THE LAST TEN CHARACTERS OF %%MYDATE%%ECHO=%MYDATE:~-10,2%ECHO=THE MONTH IS DISPLAYED ABOVEECHO=ECHO=GET THE FIRST TWO OF THE LAST SEVEN CHARACTERS OF %%MYDATE%%ECHO=%MYDATE:~-7,2%ECHO=THE DAY IS DISPLAYED ABOVEECHO=PAUSENow if you try the code again by replacing the date in 'SET MYDATE' with just 04/22/2011 you should get the same results. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now