Solved

Copying, and Renaming files in a windows directory

Posted on 2008-10-07
6
261 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to Uninstall Visual Studio 2015 7 39
Problem to Alipay 10 43
How do I generate a CSR on our root CA server? 1 24
SCCM 2012 R2, Rollback KB updates 4 23
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

856 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