?
Solved

MS DOS- Copy & Rename File

Posted on 2007-11-30
5
Medium Priority
?
1,296 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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