Solved

Adding the date to a file name .CMD funcltion

Posted on 2008-10-07
9
3,376 Views
Last Modified: 2012-08-13
Shift-1 helped me this code.
I would like to add the date rather than a just adding a 1 to the file name

@echo off
setlocal
 
set folder=C:\myfolder
 
for /F "tokens=*" %%G in ('dir "%folder%" /A:-D /B') do (
 echo F|xcopy "%folder%\%%G" "%folder%\%%~nG1%%~xG" /C /H /R /Y
)

e.g.
contained in myfolder
test.txt
test2.txt
after running the .cmd
the folder would contain

test.txt
test2.txt
test-10-7-08.txt
test2-10-7-08.txt

thanks for the help


0
Comment
Question by:derekstattin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 350 total points
ID: 22665078
Try this...

@echo off
setlocal
 
set folder=C:\myfolder
set myDate=%date:~0,2%-%date:~3,2%-%date:~-4% 
for /F "tokens=*" %%G in ('dir "%folder%" /A:-D /B') do (
 echo F|xcopy "%folder%\%%G" "%folder%\%%~nG-%myDate%%%~xG" /C /H /R /Y
)

Open in new window

0
 
LVL 5

Assisted Solution

by:si_shamil
si_shamil earned 150 total points
ID: 22665096
copy & paste the following code to .CMD file

@echo off
setlocal
 
set FOLDER=C:\myfolder
 
for %%i in (%FOLDER%\*) do (
      copy %%i %FOLDER%\%%~ni%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt 
)

Open in new window

0
 
LVL 5

Assisted Solution

by:si_shamil
si_shamil earned 150 total points
ID: 22665143
Ups, mistake

you this instead:
@echo off
setlocal
 
set FOLDER=C:\myfolder
 
cd /d %FOLDER%
 
for %%i in (*.*) do (
	echo %%i %%~ni%date:~-10,2%%date:~-7,2%%date:~-4,4%%%~xi
)
 
cd /d %0\..

Open in new window

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:derekstattin
ID: 22665206
The results I got from sirbounty's code is the creation of test-Tu- 1-2008 .txt when the .cmd file is run on a my folder containing test.txt

Is this because of the time zone? I am tijuana/pacific coast US time zone

I could not get any results from si_shamil code, may I need to change something on my end?

thanks
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 350 total points
ID: 22665220
No, there are usually two methods for displaying the date...one with the day, one without.

Try this adjustment:
@echo off
setlocal
 
set folder=C:\myfolder
set myDate=%date:~4,2%-%date:~7,2%-%date:~-4% 
for /F "tokens=*" %%G in ('dir "%folder%" /A:-D /B') do (
 echo F|xcopy "%folder%\%%G" "%folder%\%%~nG-%myDate%%%~xG" /C /H /R /Y
)

Open in new window

0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 350 total points
ID: 22665222
Or you may just try this one:

@echo off
setlocal
 
set folder=C:\myfolder
set myDate=%date:~-10% 
for /F "tokens=*" %%G in ('dir "%folder%" /A:-D /B') do (
 echo F|xcopy "%folder%\%%G" "%folder%\%%~nG-%myDate%%%~xG" /C /H /R /Y
)

Open in new window

0
 

Author Comment

by:derekstattin
ID: 22665290
Sir Bounty,

Thank you for the two examples. The first example works as expected.

The second example create a new folder

When I  run the .cmd file, a new folder named test-10 is created in myfolder (where test.txt contained)

Thanks for the help, I am going to post another question related to these results
0
 
LVL 5

Assisted Solution

by:si_shamil
si_shamil earned 150 total points
ID: 22665295
sorry I had a typo in the script
you this one:
@echo off
setlocal
 
set FOLDER=C:\myfolder
 
cd /d %FOLDER%
 
for %%i in (*.*) do (
        copy %%i %%~ni%date:~-10,2%-%date:~-7,2%-%date:~-4,4%%%~xi
)
 
cd /d %0\..

Open in new window

0
 

Author Comment

by:derekstattin
ID: 22665467
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to run Invisible a .bat file? 3 71
Powershell pass array of objects to another script 8 108
xcopy loop 13 67
Find newest text file in a folder, windows batch file 5 41
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question