Solved

Copying, and Renaming files in a windows directory

Posted on 2008-10-07
6
257 Views
Last Modified: 2010-04-16
COPY "[source file]" "[destination file or folder]"
   e.g.   COPY "C:\myfolder\testfile.txt"    "C:\myfolder\10-1-08.txt"
 I am using a .BAT file with the code above.

I would like to be able to copy all the files in the directory and rename them.

COPY "C:\myfolder\array"    "C:\myfolder\array1"

e.g. testfileA.txt and testfileB.txt are in the directory before running the .bat file
one the .bat file is run, the directory will have testfileA1.txt and testfileB1.txt

Is this possible?

thanks
0
Comment
Question by:derekstattin
  • 3
  • 2
6 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 22663605
Paste the script below into a text file with a .cmd extension.  Customize the value of the folder variable with the folder containing the files.  Running the script will echo rename commands to be run.  Once you've tested it successfully, remove the ECHO from line 6 to rename the files.


@echo off

setlocal
 

set folder=c:\files
 

for /F "tokens=*" %%G in ('dir "%folder%" /A:-D /B') do ECHO ren "%folder%\%%G" "%%~nG1%%~xG"
 

pause

Open in new window

0
 
LVL 11

Expert Comment

by:loftyworm
ID: 22664055
I have used this
COPY "C:\myfolder\*.*    "C:\myfolder\*1.*
0
 

Author Comment

by:derekstattin
ID: 22664729
loftyworm: I did not have any luck with your solution

Shift-3: I got it to work, but I would like to retain the old files in the folder and create new copies with new names, e.g. with the extra 1 added at the end of the name.

The script that you gave me renames the files, rather than copying then renaming.

my e.g was a bit confusing
e.g. testfileA.txt and testfileB.txt are in the directory before running the .bat file
one the .bat file is run, the directory will have testfileA1.txt and testfileB1.txt  . . . as well as retaining testfileA.txt, and textfileB.txt

sorry for the confusion,
 thanks for the help


0
Why won’t your email signature format correctly?

Struggling to get your corporate email signatures to format correctly? Does the logo keep resizing? Is the text appearing too big? What can you do to prevent this? Find out how you can save your signatures today.

 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 22664787
This should copy the files.  Don't remove the echo command from this one.


@echo off

setlocal

 

set folder=C:\Files

 

for /F "tokens=*" %%G in ('dir "%folder%" /A:-D /B') do (

 echo F|xcopy "%folder%\%%G" "%folder%\%%~nG1%%~xG" /C /H /R /Y

)

Open in new window

0
 

Author Comment

by:derekstattin
ID: 22664895
I will post another question. Can you tell me if I am posting to the right experts zone?

I would like to change the 1 to todays date

thanks
0
 

Author Comment

by:derekstattin
ID: 22664932
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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

20 Experts available now in Live!

Get 1:1 Help Now