0

So i would like to copy all the files in a folder to that folder with a different extension. I am on windows 7 and I think the easiest way to do this is with a .cmd script. (i decided against .bat because this seems to be an older solution)

my files follow this pattern

BrettFavre.nunit.config
JohnElway.nunit.config
TimTebow.nunit.config 

I would like for them to look like this

BrettFavre.dll.config
JohnElway.dll.config
TimTebow.dll.config 

my solution was copy *.config *.dll.config

turns out instead of getting things with a .dll.config ending i instead get a .nunit.dll.config ending. Any thoughts, and if you know of a good reference for cmd scripting please let me know. I had trouble figuring out if guides were for .bat or for .cmd .

thanks

Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71

2 Answers2

4

from the command line:

for %A in (*.config) do for %B in ("%~nA") do copy "%A" "%~nB.dll.config"

from within a batch file, double the percents

for %%A in (*.config) do for %%B in ("%%~nA") do copy "%%A" "%%~nB.dll.config"
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I get an error when i try to use the second. THe first statement works perfectly, however the second doesnt because i need to run this as a .cmd file. I have an integration tool (TeamCity) where i can put in custom scripts. the only problem is they have to be .cmd scripts. The error is below. – Frank Visaggio Jun 18 '12 at 19:08
  • The following usage of the path operator in batch-parameter substitution is invalid: %~nA") do copy "%A" "%~nB.dll.config" For valid formats type CALL /? or FOR /? Process exited with code 255 – Frank Visaggio Jun 18 '12 at 19:11
  • I was thinking maybe i could echo your second line into a .bat file and then try and call it from the cmd program that is running – Frank Visaggio Jun 18 '12 at 19:12
  • 1
    I don't understand your problem. The code works fine in both .bat and .cmd files (I tested). There are very few differences between .bat and .cmd, and the differences should not impact you. My guess is either the code was copied incorrectly, or else there is some odd behavior introduced by "TeamCity", whatever that is. – dbenham Jun 18 '12 at 19:50
  • 1
    @BobSinclar - Actually, I thought of a situation that could cause the script to fail. Try adding the line `setlocal enableExtensions` anywhere before the FOR statement. Normally command extensions are enabled by default. But perhaps your environment is non-standard. The `~n` modifier won't work unless extensions are enabled. – dbenham Jun 18 '12 at 19:53
1

Using powershell from the command line (of course you could also put this into a script):

PS> $files = Get-ChildItem *.nunit.config
PS> foreach($file in $files) {
>> $newname = [regex]::replace($file, "nunit", "dll")
>> Copy-Item $file -destination $newname
>> }
>> 
PS> 

You can compress this to a single line command as follows:

gci *.nunit.config | % {copy $_ -dest ($dest = [regex]::replace($_, "nunit", "dll"))}

@Joey makes a good point. You can simplify this by using the -replace operator:

gci *.nunit.config | % {copy $_ -dest ($_ -replace "nunit", "dll")}
David
  • 6,462
  • 2
  • 25
  • 22
  • 2
    PowerShell has a `-replace` operator. You rarely need to use `[regex]::Replace()`. – Joey Jun 16 '12 at 07:45
  • running this from a .cmd doesnt seem to work, thought if i open up a cmd window then use the powershell it works fine. below is what i am trying the bold is what i am putting in .cmd files – Frank Visaggio Jun 18 '12 at 19:22
  • **Powershell gci *.nunit.config | % {copy $_ -dest ($dest = [regex]::replace($_, "nunit", "dll"))}** '%' is not recognized as an internal or external command, operable program or batch file. – Frank Visaggio Jun 18 '12 at 19:23
  • **echo gci *.nunit.config | % {copy $_ -dest ($dest = [regex]::replace($_, "nunit", "dll"))} > gerd.ps1 powershell -File gerd.ps1** the problem with this one is that the code doesnt get echoed into my file gerd.ps1 remains blank. – Frank Visaggio Jun 18 '12 at 19:29
  • I also made sure to Set-ExecutionPolicy unrestricted beforehand – Frank Visaggio Jun 18 '12 at 20:26
  • i also tried to set-executionpolicy remotesigned beforhand but that didnt change anything either – Frank Visaggio Jun 18 '12 at 20:27
  • 1
    Can you run it from a PowerShell window? (i.e. Start->All Programs->Accessories->Windows PowerShell->Windows Powershell). Or, are you constrained to use a cmd window? – David Jun 18 '12 at 20:27
  • for my intended use i am constrained to TeamCity (a build integration tool similar to cruise control) executing some text as a .cmd file. your solution works fine if i can just run it in the powershell window or if i enter powershell in the cmd window before hand. sadly i cant do that , all i do is enter text which will be then run as a .cmd file on windows or a shell-script in Unix like environmental. Hence why I am trying to do what might seem like a silly workaround to echo your second statement into a powershell file and then call it in the same cmd file – Frank Visaggio Jun 18 '12 at 20:32
  • 1
    Check out this discussion, it might help: http://stackoverflow.com/questions/932291/calling-powershell-cmdlets-from-windows-batch-file – David Jun 18 '12 at 20:40