Solved

MS DOS- Copy & Rename File

Posted on 2007-11-30
5
1,291 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
[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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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