1

I'm trying to write a windows batch file that would check which text files are present in folder A and folder B; and if a file is only present in folder A, I want to create a text file with the same name in folder B that would only contain some arbitrary text but not touch the other existing files.

to make things clear

  1. Folder A:

    • file1.txt
    • file2.txt
    • file3.txt
  2. Folder B:

    • file2.txt
    • file3.txt

The code should check and see that file1.txt is missing in folder B and create a file with the same name containing the text "Hello World", but not alter in any way the other files.

I know this is going to probably use the if exist and for functions but I can't get my head around it... so far, this is what I've come up with

for %%a in ("%~dp0\folder1\*.*") do if not exist "%~dp0\folder2\%%a" (
echo Hello World
)>"%~dp0\folder2\%%a"

EDIT Just adding the solution proposed by McDonald's (not the restaurant) to my puzzle. This just works.

for %%a in ("%~dp0\folder1\*.*") do if not exist "%~dp0\folder2\%%~NXa" (
echo Hello World
)>"%~dp0\folder2\%%~NXa"

closed as too broad by DavidPostill Jun 27 '17 at 19:41

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Required commands:: dir, find, if, echo – Biswapriyo Jun 27 '17 at 19:38
  • Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. – DavidPostill Jun 27 '17 at 19:41
  • 1
    for %%a in ("%~dp0\folder1\*.*") do if not exist "%~dp0\folder2\%%~NXa" ( echo Hello World )>"%~dp0\folder2\%%~NXa" ... is the exact code to use perhaps so test it. – Pimp Juice IT Jun 27 '17 at 21:19
  • YES! McDonald's! That is exactly what I wanted to do! You should put this as a definite answer to my question. – Flaver-D Jun 28 '17 at 14:27
  • I do wonder about one thing: I noticed that you used %%~NXa instead of just %%a in my non-working original. What does those switches actually mean? – Flaver-D Jun 28 '17 at 19:15