0

I have folder A and B. Folder A has files like: a.mp3 and a.txt and folder B has: b.mp3 and b.txt. What I want to do here is copy and rename the content of the folder A to B so that the files can be overwritten.

Here is an example code on how to overwrite and keep the same file name in folder B:

XCOPY /HECY A\a.txt B\b.txt

ButI don't want to type all the file names to copy and overwrite the files in folder B.

Any help will be appreciated.

Tolga Demir
  • 161
  • 2
  • 11
  • So you want to overwrite `B\b.txt` with `A\a.txt`, and keep the same name? – Bali C Jan 08 '13 at 11:38
  • Yeah, that is what I'm trying to say. :) – Tolga Demir Jan 08 '13 at 11:39
  • You must include important information in the question description, like this one: "I want to overwrite files with names starting from nl_ with sp_", otherwise the answers are incomprensible! Do you suggest that I must read _all_ comments in other answers in order to understand your question? Very bad idea... I suggest you to do this anyway even if the question was solved already. :( – Aacini Jan 08 '13 at 18:13

3 Answers3

1

This should work. It will copy all A\nl_*.* files to B\, renaming the nl to sp and overwriting the files.

setlocal enabledelayedexpansion
for %%a in (A\nl_*.*) do (
set file=%%~nxa
set file=!file:~2!
xcopy /hecyi "%%a" "B\sp!file!"
)
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Thank you, but how can I overwrite nl_test.txt with sp_test.txt? I tried to replace "%%a" "B\b%%~xa" with "%%sp_" "B\nl_%%~xa". Can I also use a variable that has sp_ as a value? I will mark your answer as correct, but help me with this issue first please. – Tolga Demir Jan 08 '13 at 12:03
  • Sorry, I just took your example a bit too literally. Just replace `"B\b%%~xa"` with `"B\%%~nxa"` and it should work with all files. – Bali C Jan 08 '13 at 12:28
  • Hey Bali, thank you again. But I think you still not get the point here. Thanks to you, I can copy the files to folder B, but I want to overwrite files with names starting from nl_ with sp_. So the names stay as is, but the content of the files change after copying the files from folder a to folder B. – Tolga Demir Jan 08 '13 at 12:43
  • :) Ok, so all nl_* files are to be copied into B, right? Presumably B has all sp_* files then, and the nl_ ones should be renamed to sp_ and overwrite them? – Bali C Jan 08 '13 at 13:52
  • If you can help me with that, I will be voting up your answer as well. :) – Tolga Demir Jan 08 '13 at 13:53
  • Thank you Bali, you are the first nicest person who likes to help. I am really frustrated with batch. – Tolga Demir Jan 08 '13 at 14:06
  • Thanks :) Your welcome, I have updated my answer, and just did a little test with it myself, and it works great. Yeah batch can be frustrating, it's hard to debug, but I still love it :) – Bali C Jan 08 '13 at 14:10
  • You are my savior! Bali, I could kiss you right now, omg what a relieve! Can you please explain what !file:~2! and !file! is? I get everything, except for these. – Tolga Demir Jan 08 '13 at 14:21
  • 1
    Haha :D no worries. The `!file:~2!` strips the first 2 characters from the variable, in this case the variable is the filename, which we want to remove the nl part from, so I can put sp in it's place later. The `!file!` bit is just like `%file%` (which now has the nl taken off) but it uses delayed expansion because it's in a for loop. If you use regular `%`'s the variable stays the same until the loop has finished. – Bali C Jan 08 '13 at 14:32
0

Renames a file/directory or files/directories.

RENAME [drive:][path][directoryname1 | filename1] [directoryname2 | filename2] REN [drive:][path][directoryname1 | filename1] [directoryname2 | filename2]

Example

  1. to rename a directory "sampel" to "sample"

rename c:\sampel sample

  1. to rename all text files to files with .bak extension.

rename *.txt *.bak

  1. to rename all files in a particular folder with specific prefix(eg: 1_NEW)

rename * 1_NEW*

4.Rename the file "normal sample.txt" to "example sample.txt". Whenever dealing with a file or directory with a space, it must be surrounded with quotes. Otherwise you'll get the "The syntax of the command is incorrect." error.

rename "normal sample.txt" "example sample.txt"

user1651561
  • 197
  • 3
  • 3
0

You may do that directly with xcopy /HECYI A\nl_*.* B\sp_*.* command:

C:>dir A /b
nl_t.txt
nl_test.txt
nl_test.xyz
nl_testLarge.txt

C:>dir B /b

C:>xcopy A\nl_*.* B\sp_*.*
A\nl_t.txt
A\nl_test.txt
A\nl_test.xyz
A\nl_testLarge.txt
4 File(s) copied

C:>dir B /b
sp_t.txt
sp_test.txt
sp_test.xyz
sp_testLarge.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • OMG, I mean, this took me so much time to understand how the logic of batch is. I just started, so I hope you understand. :) – Tolga Demir Jan 09 '13 at 08:33