Solved

MS DOS- Copy & Rename File

Posted on 2007-11-30
5
1,267 Views
Last Modified: 2012-05-05
I have a text file that is on a different server.  I would like to copy and rename the file to a different location.  The issue that I'm facing is that the file names have the date written after them (with the current date).  

The format of the date is year-month-date (with nothing between them).   I know the basic move and rename commands, but I will need to set a variable for these but don't know how to do this.
0
Comment
Question by:jjrr007
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20385277
Here's some code:
@echo off
 
setlocal
 
call :GETDATEPARTS "%date%"
 
set curDate=%yy%%mm%%dd%
 
echo curDate=%curDate%
 
REM ** Above line is test code. You would place your code here before the goto :EOF command.
 
goto :EOF
 
:GETDATEPARTS
 
set dt=%~1
set tok=1-3
 
if "%dt:~0,1%" GTR "9" set tok=2-4
 
set yyyy=
 
for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)
 
if not "%yyyy%"=="" set yy=%yyyy%
 
if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%
 
goto :EOF

Open in new window

0
 
LVL 1

Author Comment

by:jjrr007
ID: 20385321
You are very good at this.  This code looks excellent!

 I won't be able to test it without being able to add the "txt" extension to the basic copy and rename statements that I have.  Could you please let me know how I can that? I can then test this. Thanks again.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 20385355
If the format of the file is simply YYYYMMYY.txt then you'd say something like this. Of course you'd want to change the drive and directories of the source and destination files as necessary.


@echo off
 
setlocal
 
call :GETDATEPARTS "%date%"
 
set curDate=%yy%%mm%%dd%
 
REM ** Here's your line:
if exist "c:\sourceDir\%curDate%.txt" move "c:\sourceDir\%curDate%.txt" "c:\destDir"
 
goto :EOF
 
:GETDATEPARTS
 
set dt=%~1
set tok=1-3
 
if "%dt:~0,1%" GTR "9" set tok=2-4
 
set yyyy=
 
for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)
 
if not "%yyyy%"=="" set yy=%yyyy%
 
if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%
 
goto :EOF

Open in new window

0
 
LVL 1

Author Closing Comment

by:jjrr007
ID: 31412033
You are  a wonderful programmer.  You did this with great speed. Thank you!!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20385538
Thanks :) I'm glad it worked for you.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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