0

I want to create a text file suicune in each directory of the tree except the root, but I also get a suicune txt file in the root. I only want it in subfolders.

I have tried using different combinations in the set to skip the root. I have tried googling for a solution to skip the root. I have searched in the /?


REM creates text file suicune 
REM in each directory of the tree
set path="%C:\Users\Username\Desktop\Files%"
cd %path%

for /R %%a in (.) do (
    cd %%a
    echo hi > suicune.txt
)

pause

I expected a suicune.txt file to created only in subfolders of the directory but it's also created in the root directory.

Jaswir
  • 172
  • 11
  • 2
    `path` is a builtin environment variable and you are breaking your environment. Instead use `mypath` Also we do not use `%` in the variable values. Then simply exclude the root dir `do if not "%%~a"=="%mypath%."` – Gerhard May 25 '19 at 14:22
  • 2
    Replace `set path="%C:\Users\Username\Desktop\Files%"` by `set "MyPath="%UserProfile%\Desktop\Files""` for the reasons explained briefly by Gerhard Barnard and extended by answer on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi May 25 '19 at 15:10
  • 2
    There's no need to change directory, or create variables, a single for loop should do what you wanted: `@For /D /R "%UserProfile%\Desktop\Files" %%A In (*)Do @CD.>"%%~A\suicune.txt"`. – Compo May 25 '19 at 15:17
  • @GerhardBarnard do if not check doesn't have any effect here – Jaswir May 26 '19 at 11:03
  • @Compo Thanks, this helped me! needed to use /D infront of /R and use (*) instead of (.) – Jaswir May 26 '19 at 11:14
  • 1
    `echo hi > suicune.txt` also outputs the _space_ before `>` to the file, better use `> "suicune.txt" echo hi` instead... – aschipfl May 26 '19 at 11:26
  • Yes, it will not have any effect if you do not use it correctly. – Gerhard May 26 '19 at 13:48

0 Answers0