Solved

MS DOS- Copy & Rename File

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A short film showing how OnPage and Connectwise integration works.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now