-1

This is my batch file:

@echo off
copy /Y "C:\Users\Murray\Desktop\My_File_Name_1.ssl" "C:\Program Files (x86)\Program\config\*.ssl"

There is another file in the destination folder with ssl extension but its name is always different. For that reason, I want to copy my file then overwrite the current file in the destination folder. However, I do not want to specify file name in the destination because it will be always different while having always same extension (ssl as in the sample). This batch file copies the file to the destination folder but keeps other file with ssl extension. What am I missing?

After running this is output:

C:\Users\Murray\Documents>mytest.bat

C:\Users\Murray\Documents>SetLocal EnableExtensions

C:\Users\Murray\Documents>Set "SourceFile=C:\Users\Murray\Desktop\My_Test_File.ssl"

C:\Users\Murray\Documents>Set "Destination=C:\Program Files (x86)\Program\config"

C:\Users\Murray\Documents>For %G In ("C:\Users\Murray\Desktop\My_Test_File.ssl") Do If "%~aG" LSS "-" (GoTo :EOF )  Else If "%~aG" GEQ "d" (GoTo :EOF )  Else Set "TargetFile="   & Set "Mask=*%~xG"

C:\Users\Murray\Documents>If "--a--------" LSS "-" (GoTo :EOF )  Else If "--a--------" GEQ "d" (GoTo :EOF )  Else Set "TargetFile="   & Set "Mask=*.ovpn"

C:\Users\Murray\Documents>For /F "EOL=? Delims=" %G In ('C:\Windows\System32\xcopy.exe "C:\Program Files (x86)\Program\config\*.ssl" ? /HL | C:\Windows\System32\findstr.exe /N .') Do If "%~dG" == "1:" (Set "Target=%~nxG" )  Else If "%~dG" == "3:" Set "Target="

C:\Users\Murray\Documents>If "1:" == "1:" (Set "Target=Test_File.ssl" )  Else If "1:" == "3:" Set "Target="

C:\Users\Murray\Documents>If "2:" == "1:" (Set "Target=1 File(s)" )  Else If "2:" == "3:" Set "Target="

C:\Users\Murray\Documents>If Not Defined Target (GoTo :EOF )  Else If "Ai" == "0 " GoTo :EOF

C:\Users\Murray\Documents>Copy /Y "C:\Users\Murray\Desktop\My_Test_File.ssl" "C:\Program Files (x86)\Program\config\Test_File.ssl"
        1 file(s) copied.

Result: Although it says one file copied, in the destination folder I still see Test_File.ssl instead of My_Test_File.ssl which is expected to copy to the destination.

Murray
  • 57
  • 7
  • 2
    You can't use a * in the destination like that. Suppose there were more than one .ssl file. How would it know which one to overwrite? If there is always only 1 .ssl file, just delete it first and then copy. – RGuggisberg Aug 25 '21 at 20:21
  • 4
    `for %%I in ("%ProgramFiles(x86)%\Program\config\*.ssl") do copy /Y "%UserProfile%\Desktop\My_File_Name_1.ssl" "%%~I"` (if there are more than one `*.ssl` files in the destination, each of them is overwritten with the source file) – aschipfl Aug 25 '21 at 20:36
  • I tried the code with a batch file echo off, nothing happened. Where am I wrong? – Murray Aug 25 '21 at 21:13
  • @Murray, please copy and paste (as text) the output that appeared when the .bat file script was run with ECHO OFF. That will show what was actually done. – lit Aug 25 '21 at 21:57
  • but it does not move the file? – Murray Aug 25 '21 at 23:08
  • Why should the `copy` command move a file? – aschipfl Aug 26 '21 at 10:33
  • I mean, nothing happens. It does not move, nor copy does. – Murray Aug 26 '21 at 10:51
  • Your output has been modified before you posted it, and you have not modified it properly, which shows me clearly that what you are doing does not fulfil the criteria of your question or our solutions. It is not possible that `%Mask%` would be defined as `*.ovpn` if your input file was really named `My_Test_File.ssl`. Our code and your question was supposed to copy a `.ssl` extension file to another location overwriting the single `.ssl` extension file in that location. Also the first two characters of your filename are clearly showing as `Ai`, not `My`, so you certainly have made changes. – Compo Aug 26 '21 at 12:40
  • 1
    You said you wanted to overwrite the DESTINATION FILE. That is the code you were given. If you didn't want that then your whole question doesn't make any sense. What you really wanted to do was delete any file in the destination and copy in the new one. – Squashman Aug 26 '21 at 17:33

2 Answers2

2

As your task appears to have changed from copying a file whilst overwriting an existing one, to a straight copy and delete procedure, the following adaptation of my other answer is a little less code:

@Echo Off
SetLocal EnableExtensions

Set "SourceFile=C:\Users\Murray\Desktop\My_File_Name_1.ssl"
Set "Destination=C:\Program Files (x86)\Program\config"

For %%G In ("%SourceFile%") Do If "%%~aG" Lss "-" (GoTo :EOF
) Else If "%%~aG" GEq "d" (GoTo :EOF) Else Set "Mask=*%%~xG"
For /F "EOL=? Delims=" %%G In ('%SystemRoot%\System32\xcopy.exe
 "%Destination%\%Mask%" ? /HL') Do Set /A "Quantity=%%G" 2>NUL
If %Quantity% Equ 1 Del /A /F "%Destination%\%Mask%" 2>NUL && (
    Copy "%SourceFile%" "%Destination%" 1>NUL)
Compo
  • 36,585
  • 5
  • 27
  • 39
  • That works like a charm @Compo Thank you very much for all your efforts. Tested and works flawlessly. Thank you. – Murray Aug 26 '21 at 22:37
1

The following code will only copy the %SourceFile% to the %Destination%, if the %SourceFile% is an existing file, and if the %Destination% contains only one file with the same extension as the %SourceFile%.

@Echo Off
SetLocal EnableExtensions

Set "SourceFile=C:\Users\Murray\Desktop\My_File_Name_1.ssl"
Set "Destination=C:\Program Files (x86)\Program\config"

For %%G In ("%SourceFile%") Do If "%%~aG" Lss "-" (GoTo :EOF
) Else If "%%~aG" GEq "d" (GoTo :EOF) Else Set "TargetFile=" & Set "Mask=*%%~xG"
For /F "EOL=? Delims=" %%G In ('%SystemRoot%\System32\xcopy.exe
 "%Destination%\%Mask%" ? /HL ^| %SystemRoot%\System32\findstr.exe /N .'
) Do If "%%~dG" == "1:" (Set "Target=%%~nxG"
) Else If "%%~dG" == "3:" Set "Target="
If Not Defined Target (GoTo :EOF) Else If "%Target:~,2%" == "0 " GoTo :EOF
Copy /Y "%SourceFile%" "%Destination%\%Target%"

You may modify only the variable values on lines 4 and 5 for testing my answer, nothing else must be changed or added.

Please note, as with your previously answered question, because your provided destination path is in a default protected Windows directory, you will probably need this script to Run elevated/as administrator, in order to have the required permissions.


[Edit /]

The code above copies your source file to the destination location, whilst renaming it to the unknown name of the file with that extension in the destination, i.e. overwriting it.

If your task, as it seems now, is to copy the source file to the destination, and then delete the file with the same extension but with an unknown name in that destination then you could simply replace the last line:

Copy /Y "%SourceFile%" "%Destination%\%Target%"

with this line:

Copy "%SourceFile%" "%Destination%" && Del /A /F "%Destination%\%Target%"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you very much. I run it as elevated, but it did not copy the file to the destination. Also, the other file in the destination is still there – Murray Aug 26 '21 at 09:13
  • 1
    @Murray, there is nothing wrong with my code, or that of aschipfl in the comments. I would advise you to [follow the advice from lit](https://stackoverflow.com/questions/68929293/copy-a-file-in-a-folder-then-replace-it-with-other-file/68931408?noredirect=1#comment121820460_68929293) also in the comments. Change `Off` to `On` on the first line, then open a Command Prompt window, and type the full path of your batch file, and press the `[ENTER]` key. All commands run will and any output be shown to you, and you can copy and paste them into your question for us to provide you with further advice. – Compo Aug 26 '21 at 09:24
  • Okay. Thank you very much. Solutions work when I run bat files from command prompt as administrator. Then, question is why does bat files not work directly from the bat files itself? I do right click a bat file in documents folder and then choose "Run as administrator". Echo on command says "Access is denied". Is running a bat file from command promt as administrator not the same thing with running bat file itself as administrator directly from bat file's itself? – Murray Aug 26 '21 at 09:51
  • 1
    How are you opening a Command Prompt 'as administrator'? I cannot make a judgement on your above statement without knowing exactly how you're doing it, because you've only explained how you're running the batch file. Generally however there should be no difference whatsover, unless you decided to not use a full path for `%Source%` or `%Destination%` variable values. When you run a batch file as Administrator, the current directory becomes `%SystemRoot%\System32`, which means any non absolute path becomes relative that location. Providing all unmodified screen output would help us to help you. – Compo Aug 26 '21 at 10:05
  • it says, too long to post comment: In Command Prompt C:\Users\Murray\Documents>mytest.bat Although it says at the end of Command prompt that " 1 file(s) copied., nothing copied trully. – Murray Aug 26 '21 at 10:37
  • 1
    @Murray, I specifically said, [copy and paste them into your question](https://stackoverflow.com/questions/68929293/copy-a-file-in-a-folder-then-replace-it-with-other-file/68931408?noredirect=1#comment121829859_68931408). There is an `[edit]` button just below your question for that purpose. If it tells you that a file was copied, then the code and the copy process worked exactly as intended. Any environment, permissions issues, or errors on your part, are outside of the scope of your question or this answer, until you have given us every piece of information we need to reproduce that problem. – Compo Aug 26 '21 at 11:20
  • 1
    @Murray, I have posted a comment under your main question, because it is clear that you've been untruthful with either your task, and / or the provided output. I will not support trying to fix things when you are deliberately changing things which do not ever need changing, in some pointless attempt at remaining secure or private. It's not possible for me or anyone else, based upon your member, account name or profile information, to identify you, your employer, or anything else by providing real paths / filenames. This code doesn't copy `Ai*.ovpn` overwriting a single `.ssl` destination file. – Compo Aug 26 '21 at 12:47
  • I understand. You have done your best. I will try to fix it and let the community know. – Murray Aug 26 '21 at 15:45
  • 1
    There is nothing to fix, and please don't imply that my best is somehow lacking, @Murray. My code is 100% working according to the information you have provided. The only issue is that the information you have provided appears to have been different and the output you've supplied in not the true output. Those I'm confident to say, are totally your fault. If you provide the correct information and true output, I will still try to assist you, however, my answer above, based upon the specifics you submitted in your question, is a completely correct and working solution for your reported task. – Compo Aug 26 '21 at 16:02
  • @Murray, I have added an edit to my answer above, to change the task based upon your statement "in the destination folder I still see Test_File.ssl instead of My_Test_File.ssl". Whist the modification will perform that task, I have also added another answer, which whilst similar, performs that modified task with a little less code. – Compo Aug 26 '21 at 22:09